codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2115A.md (1977B)


      1 Source: https://codeforces.com/problemset/problem/2115/A
      2 
      3 A. Gellyfish and Flaming Peony
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 512 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Gellyfish hates math problems, but she has to finish her math homework:
     22 
     23 Gellyfish is given an array of $n$ positive integers $a_1, a_2, \ldots, a_n$.
     24 
     25 She needs to do the following two-step operation until all elements of $a$ are equal:
     26 
     27 1. Select two indexes $i$, $j$ satisfying $1 \leq i, j \leq n$ and $i \neq j$.
     28 2. Replace $a_i$ with $\gcd(a_i, a_j)$.
     29 
     30 Now, Gellyfish asks you for the minimum number of operations to achieve her goal.
     31 
     32 It can be proven that Gellyfish can always achieve her goal.
     33 
     34 Input
     35 
     36 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
     37 
     38 The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) — the length of the array.
     39 
     40 The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \leq a_i \leq 5000$) — the elements of the array.
     41 
     42 It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.
     43 
     44 Output
     45 
     46 For each test case, output a single integer — the minimum number of operations to achieve her goal.
     47 
     48 Example
     49 
     50 Input
     51 
     52 ```
     53 3
     54 
     55 3
     56 
     57 12 20 30
     58 
     59 6
     60 
     61 1 9 1 9 8 1
     62 
     63 3
     64 
     65 6 14 15
     66 ```
     67 
     68 Output
     69 
     70 ```
     71 4
     72 3
     73 3
     74 ```
     75 
     76 Note
     77 
     78 In the first test case, the following is a way that minimizes the number of operations:
     79 
     80 1. Choose $i = 3$ and $j=2$ and replace $a_3$ with $\gcd(a_3,a_2) = \gcd(30, 20) = 10$, then $a$ becomes $[12, 20, 10]$.
     81 2. Choose $i=1$ and $j=3$ and replace $a_1$ with $\gcd(a_1,a_3) = \gcd(12, 10) = 2$, then $a$ becomes $[2, 20, 10]$.
     82 3. Choose $i=2$ and $j=1$ and replace $a_2$ with $\gcd(a_2,a_1) = \gcd(20, 2) = 2$, then $a$ becomes $[2, 2, 10]$.
     83 4. Choose $i=3$ and $j=1$ and replace $a_3$ with $\gcd(a_3,a_1) = \gcd(10, 2) = 2$, then $a$ becomes $[2, 2, 2]$.