2018CCPC桂林站G Greatest Common Divisor
题目描述
Now you can add all numbers by 1 many times. Please find out the minimum times you need to perform to obtain an array whose greatest common divisor(gcd) is larger than 1 or state that it is impossible.
You should notice that if you want to add one number by 1, you need to add all numbers by 1 at the same time.
输入
Then there are 2×T lines, with every two lines representing a test case.
The first line of each case contains a single integer n (1≤n≤105) described above.
The second line of that contains n integers ranging in [1,109].
输出
For each test case, print Case d: (d represents the order of the test case) first. Then output exactly one integer representing the answer. If it is impossible, print -1 instead.
样例输入
复制样例数据
3
1
2
5
2 5 9 5 7
5
3 5 7 9 11
样例输出
Case 1: 0
Case 2: -1
Case 3: 1
提示
Sample 1: You do not need to do anything because its gcd is already larger than 1.
Sample 2: It is impossible to obtain that array.
Sample 3: You just need to add all number by 1 so that gcd of this array is 2.
题目大意:每次操作都给数组的所有数同时+1,问最少操作几次使得所有数的gcd大于1,或者压根不能使得所有数的gcd大于1。
思路类似于CF的Neko does Maths CodeForces - 1152C 数论欧几里得,不过这题的k是对n个数而言,但思路是一样的。
假设b>=a,我们知道gcd(a+k,b+k)是b-a的因子,那么要想知道所有都+k能不能有gcd>1,那就是得看两两数做差,看他们的差的gcd是不是大于1,但是两两做差O(n2)肯定不行。而我们把所有数排序,然后求相邻两个数的差的gcd,就可以了。因为,像三个数a,b,c,他们的差分别是d1,d2,如果d1和d2不互质,那么d1和d1+d2自然也不互质。得出gcd,我们就枚举gcd的因子就好了。
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int a[N];
int main()
{
int t=,T,n;
scanf("%d",&T);
while(t<=T)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int g=,ans;
for(int i=;i<n;i++)//并不需要去重,因为gcd(0,x)=x
g=__gcd(g,a[i]-a[i-]);
if(g==)
ans=-;
else if(g==)//都是同一个数的时候得特判
{
if(a[]==)
ans=;
else
ans=;
}
else
{
ans=(g-a[]%g)%g;
for(int i=;i*i<=g;i++)//枚举因子,找最小答案
if(g%i==)
{
ans=min(ans,(i-a[]%i)%i);
ans=min(ans,(g/i-a[]%(g/i))%(g/i));
}
}
printf("Case %d: %d\n",t++,ans);
}
return ;
}
gcd
2018CCPC桂林站G Greatest Common Divisor的更多相关文章
- CCPC2018 桂林 G "Greatest Common Divisor"(数学)
UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...
- 2018CCPC桂林站JStone Game
题目描述 Alice and Bob are always playing game! The game today is about taking out stone from the stone ...
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- 845. Greatest Common Divisor
描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...
- hdu 5207 Greatest Greatest Common Divisor 数学
Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...
随机推荐
- 级联-city
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- pt-online-schema-change使用
MySQL ddl 的问题现状 在 运维mysql数据库时,我们总会对数据表进行ddl 变更,修改添加字段或者索引,对于mysql 而已,ddl 显然是一个令所有MySQL dba 诟病的一个功能,因 ...
- 【leetcode】153. 寻找旋转排序数组中的最小值
题目链接:传送门 题目描述 现有一个有序数组,假设从某个数开始将它后面的数按顺序放到了数组前面.(即 [0,1,2,4,5,6,7] 可能变成 [4,5,6,7,0,1,2]). 请找出数组中的最小元 ...
- Python 运算符与数据类型
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...
- C# 32位系统与64位系统调用不同的DLL文件
string dll32 = System.Windows.Forms.Application.StartupPath + @"\System.Data.SQLite-32.DLL" ...
- SqlServer2008 跨服务器同步数据
最近工作中需要跨服务器同步数据,在数据库DB1中的表T1插入数据,同时触发T1的触发器(这里暂不讨论触发器的效率问题),向另一台服务器DB2中的相同的一张表T2插入数据,查看了一些资料说, 需要打开D ...
- EF Core的级联删除
级联删除由DeleteBehavior的枚举值来设置: 行为名称 对内存中的依赖项/子项的影响 对数据库中的依赖项/子项的影响 Cascade 删除实体 删除实体 ClientSetNull 外键属性 ...
- 函数——箭头函数&自执行函数(二)
一.箭头函数是在es6中添加的一种规范,它相当于匿名函数,简化了函数的定义. 1.语法 a.function用var,let,cost来表示: b.参数要写在第一个等号后面: 参数有多个,需要加一 ...
- “System.Reflection.TargetInvocationException”类型的未经处理的异常在 mscorlib.dll 中发生
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#signalR 第一种,权限不够,在项目运行的时候弹出==>解决方法:以管理员权限运行vs ...
- 【TCP】连接管理
TCP连接管理 本节将介绍一条TCP连接是如何建立和拆除的.此处假设客户机A上面的一个进程想要和服务 器B上的一个进程建立一条TCP连接.本文前面介绍的是比较正常的连接和拆除,特殊的会在后面介绍. ...