A. Bestie

A. Bestie

You are given an array a consisting of n integers a1,a2,,an. Friends asked you to make the greatest common divisor (GCD) of all numbers in the array equal to 1. In one operation, you can do the following:

  • Select an arbitrary index in the array 1in;
  • Make ai=gcd(ai,i), where gcd(x,y) denotes the GCD of integers x and y. The cost of such an operation is ni+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 1.

Input

Each test consists of multiple test cases. The first line contains an integer t (1t5000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (1n20) — the length of the array.

The second line of each test case contains n integers a1,a2,,an (1ai109) — the elements of the array.

Output

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.

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

Example

input

复制代码
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
复制代码

output

复制代码
0
1
2
2
1
3
3
0
1
复制代码

Note

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

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

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.

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.

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.

 

解题思路

  A题,比赛的时候卡了一个小时,哈哈。

  先说一个重要的结论:对于一个整数n,有gcd(n1,n)=1。证明如下:

  先证gcd(a,b)=gcd(ab,b)。设gcd(a,b)=d,那么有a=d×xb=d×y,并且xy互质(如果xy不互质那么ab的最大公约数就不是d了)。证明dab的公约数:有ab=d×xd×y=d×(xy),即dab。又因为db,因此dabb的公约数。下面证dabb的最大公约数:反证法,如果存在一个e>d,且满足eabeb,那么可以得到ea,即ab存在一个比d更大的公约数,这就与gcd(a,b)=d矛盾了,因此dabb的最大公约数,即gcd(ab,b)=gcd(a,b)=d。同理可证gcd(a,b)=gcd(ab,a)

  因此有结论gcd(a,b)=gcd(ab,b)=gcd(ab,a)

  因为gcd(n,1)=1,因此有gcd(n,1)=gcd(n1,n)=1,得证。

  对于这道题目,假设d=gcd(a1,a2,an),要使得d最终变为1,我们可以选择任意一个下标i以及i1来与d求最大公约数,最后得到的结果必然是1(因为gcd(i1,i)=1,因此gcd(d,i1,i)=gcd(d,gcd(i1,i))=1)。为了使得代价最小,这里in,那么这样就可以保证答案3

  下面分类讨论:

  1. 如果d=1,那么就不用操作,答案为0
  2. 否则尝试只选择下标n,如果有gcd(d,n)=1,那么答案为1
  3. 否则尝试只选择下标n1,如果有gcd(d,n1)=1,那么答案为2
  4. 否则选择下标n和下标n1,那么答案为3

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int gcd(int a, int b) {
 5     return b ? gcd(b, a % b) : a;
 6 }
 7 
 8 void solve() {
 9     int n;
10     scanf("%d", &n);
11     
12     int d = 0;
13     for (int i = 0; i < n; i++) {
14         int x;
15         scanf("%d", &x);
16         d = gcd(d, x);
17     }
18     
19     int ret = 3;
20     if (d == 1) ret = 0;
21     else if (gcd(d, n) == 1) ret = 1;
22     else if (gcd(d, n - 1) == 1) ret = 2;
23     
24     printf("%d\n", ret);
25 }
26 
27 int main() {
28     int t;
29     scanf("%d", &t);
30     while (t--) {
31         solve();
32     }
33     
34     return 0;
35 }
复制代码

 

参考资料

  Codeforces Round #830 (Div. 2) Editorial:https://codeforces.com/blog/entry/108327

  a>b>0a,b均为正整数, gcd(a,b)=gcd(ab,a)吗?- 张学涵的回答 - 知乎:https://www.zhihu.com/question/441593398/answer/1703905776

posted @   onlyblues  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示