A. Bestie
A. Bestie
You are given an array consisting of integers . Friends asked you to make the greatest common divisor (GCD) of all numbers in the array equal to . In one operation, you can do the following:
- Select an arbitrary index in the array ;
- Make , where denotes the GCD of integers and . The cost of such an operation is .
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 .
Input
Each test consists of multiple test cases. The first line contains an integer — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer — the length of the array.
The second line of each test case contains integers — 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 .
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 , so there is no need to perform operations.
In the second test case, select . After this operation, . The cost of this operation is .
In the third test case, you can select , after that the array a will be equal to . The GCD of this array is , and the total cost is .
In the fourth test case, you can select , after that the array a will be equal to . The GCD of this array is , and the total cost is .
In the sixth test case, you can select and , after that the array a will be equal to . The GCD of this array is , and the total cost is .
解题思路
A题,比赛的时候卡了一个小时,哈哈。
先说一个重要的结论:对于一个整数,有。证明如下:
先证。设,那么有,,并且和互质(如果和不互质那么和的最大公约数就不是了)。证明是的公约数:有,即。又因为,因此是和的公约数。下面证是和的最大公约数:反证法,如果存在一个,且满足,,那么可以得到,即和存在一个比更大的公约数,这就与矛盾了,因此是和的最大公约数,即。同理可证。
因此有结论。
因为,因此有,得证。
对于这道题目,假设,要使得最终变为,我们可以选择任意一个下标以及来与求最大公约数,最后得到的结果必然是(因为,因此)。为了使得代价最小,这里取,那么这样就可以保证答案。
下面分类讨论:
- 如果,那么就不用操作,答案为。
- 否则尝试只选择下标,如果有,那么答案为。
- 否则尝试只选择下标,如果有,那么答案为。
- 否则选择下标和下标,那么答案为。
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
,均为正整数, 吗?- 张学涵的回答 - 知乎:https://www.zhihu.com/question/441593398/answer/1703905776
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16820019.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效