1732A.md (2424B)
1 Source: https://codeforces.com/problemset/problem/1732/A 2 3 A. Bestie 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an array $a$ consisting of $n$ integers $a_1, a_2, \ldots, a_n$. Friends asked you to make the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of all numbers in the array equal to $1$. In one operation, you can do the following: 22 23 * Select an arbitrary index in the array $1 \leq i \leq n$; 24 * Make $a_i = \gcd(a_i, i)$, where $\gcd(x, y)$ denotes the GCD of integers $x$ and $y$. The cost of such an operation is $n - i + 1$. 25 26 You need to find the minimum total cost of operations we need to perform so that the GCD of the all array numbers becomes equal to $1$. 27 28 Input 29 30 Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 5\,000$) — the number of test cases. The description of test cases follows. 31 32 The first line of each test case contains a single integer $n$ ($1 \leq n \leq 20$) — the length of the array. 33 34 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array. 35 36 Output 37 38 For each test case, output a single integer — the minimum total cost of operations that will need to be performed so that the GCD of all numbers in the array becomes equal to $1$. 39 40 We can show that it's always possible to do so. 41 42 Example 43 44 Input 45 46 ``` 47 9 48 49 1 50 51 1 52 53 1 54 55 2 56 57 2 58 59 2 4 60 61 3 62 63 3 6 9 64 65 4 66 67 5 10 15 20 68 69 5 70 71 120 60 80 40 80 72 73 6 74 75 150 90 180 120 60 30 76 77 6 78 79 2 4 6 9 12 18 80 81 6 82 83 30 60 90 120 125 125 84 ``` 85 86 Output 87 88 ``` 89 0 90 1 91 2 92 2 93 1 94 3 95 3 96 0 97 1 98 ``` 99 100 Note 101 102 In the first test case, the GCD of the entire array is already equal to $1$, so there is no need to perform operations. 103 104 In the second test case, select $i = 1$. After this operation, $a_1 = \gcd(2, 1) = 1$. The cost of this operation is $1$. 105 106 In the third test case, you can select $i = 1$, after that the array $a$ will be equal to $[1, 4]$. The GCD of this array is $1$, and the total cost is $2$. 107 108 In the fourth test case, you can select $i = 2$, after that the array $a$ will be equal to $[3, 2, 9]$. The GCD of this array is $1$, and the total cost is $2$. 109 110 In the sixth test case, you can select $i = 4$ and $i = 5$, after that the array $a$ will be equal to $[120, 60, 80, 4, 5]$. The GCD of this array is $1$, and the total cost is $3$.