CF1732A.Bestie

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array aa consisting of nn integers a1,a2,…,ana_1, a_2, \ldots, a_n. Friends asked you to make the greatest common divisor (GCD) of all numbers in the array equal to 11. In one operation, you can do the following:

  • Select an arbitrary index in the array 1≤i≤n1 \leq i \leq n;
  • Make ai=gcd⁡(ai,i)a_i = \gcd(a_i, i), where gcd⁡(x,y)\gcd(x, y) denotes the GCD of integers xx and yy. The cost of such an operation is n−i+1n - i + 1.

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 11.

给你一个包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n 的数组 aa。朋友们要求你使该数组中所有数的最大公约数(GCD) 等于 11。在一次操作中,你可以执行以下步骤:

  • 任选数组中的一个下标 1≤i≤n1 \leq i \leq n;
  • 将 aia_i 更新为 gcd⁡(ai,i)\gcd(a_i, i),其中 gcd⁡(x,y)\gcd(x, y) 表示整数 xx 和 yy 的最大公约数。该操作的代价为 n−i+1n - i + 1。

你需要求出使整个数组所有元素的最大公约数变为 11 所需的最小总代价。

输入格式

Each test consists of multiple test cases. The first line contains an integer tt (1≤t≤5 0001 \leq t \leq 5\,000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1≤n≤201 \leq n \leq 20) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \leq a_i \leq 10^9) — the elements of the array.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤5 0001 \leq t \leq 5\,000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤201 \leq n \leq 20),表示数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \leq a_i \leq 10^9),表示数组的元素。

输出格式

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 11.

We can show that it's always possible to do so.

对于每个测试用例,输出一个整数——使得数组中所有数字的最大公约数(GCD)变为 11 所需执行的操作的最小总代价。

可以证明,这种情况总是可行的。

输入输出样例

  • 输入#1

    9
    1
    1
    1
    2
    2
    2 4
    3
    3 6 9
    4
    5 10 15 20
    5
    120 60 80 40 80
    6
    150 90 180 120 60 30
    6
    2 4 6 9 12 18
    6
    30 60 90 120 125 125

    输出#1

    0
    1
    2
    2
    1
    3
    3
    0
    1

说明/提示

In the first test case, the GCD of the entire array is already equal to 11, so there is no need to perform operations.

In the second test case, select i=1i = 1. After this operation, a1=gcd⁡(2,1)=1a_1 = \gcd(2, 1) = 1. The cost of this operation is 11.

In the third test case, you can select i=1i = 1, after that the array aa will be equal to [1,4][1, 4]. The GCD of this array is 11, and the total cost is 22.

In the fourth test case, you can select i=2i = 2, after that the array aa will be equal to [3,2,9][3, 2, 9]. The GCD of this array is 11, and the total cost is 22.

In the sixth test case, you can select i=4i = 4 and i=5i = 5, after that the array aa will be equal to [120,60,80,4,5][120, 60, 80, 4, 5]. The GCD of this array is 11, and the total cost is 33.

在第一个测试用例中,整个数组的最大公约数(GCD)已经等于 11,因此无需执行任何操作。

在第二个测试用例中,选择 i=1i = 1。执行该操作后,a1=gcd⁡(2,1)=1a_1 = \gcd(2, 1) = 1。该操作的代价为 11。

在第三个测试用例中,可以选择 i=1i = 1,之后数组 aa 将变为 [1,4][1, 4]。该数组的 GCD 为 11,总代价为 22。

在第四个测试用例中,可以选择 i=2i = 2,之后数组 aa 将变为 [3,2,9][3, 2, 9]。该数组的 GCD 为 11,总代价为 22。

在第六个测试用例中,可以选择 i=4i = 4 和 i=5i = 5,之后数组 aa 将变为 [120,60,80,4,5][120, 60, 80, 4, 5]。该数组的 GCD 为 11,总代价为 33。

输入解题思路,AI测评打分。不知道怎么写?

首页