Greatest Common Divisor

题目链接

题目描述

There is an array of length n, containing only positive numbers.

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.

输入

The first line of input file contains an integer T (1≤T≤20), describing the number of test cases.

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

输出

Please output T lines exactly.

For each line, output Case d: (d represents the order of the test case) first. Then output the answer in the same line. If there is no way for that, 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;

然后找到gcd的最小质因数g ,操作的次数为距离a[0]最近的g的倍数-a[0]; 即g - a[0] % g;

还有其他各种特判,在代码中说明

代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define scac(x) scanf("%c",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pri3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prl(x) printf("%lld\n",x)
#define prl2(x,y) printf("%lld %lld\n",x,y)
#define prl3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define mst(x,y) memset(x,y,sizeof(x))
#define ll long long
#define LL long long
#define pb push_back
#define mp make_pair
#define P pair<double,double>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define mod 1e9+7
#define INF 0x3f3f3f3f
#define N 1005
const int maxn = 1e5+10;
ll a[maxn],b[maxn];
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
int n;
int main()
{
int t;
sca(t);
int cas = 1;
while(t--)
{
sca(n);
rep(i,0,n) scl(a[i]);
if(n == 1) //如果只有一个数
{
if(a[0] == 1) printf("Case %d: 1\n",cas++);
else printf("Case %d: 0\n",cas++);
continue;
}
sort(a,a+n);
int cnt = 0;
rep(i,1,n)
{
if(a[i] != a[i-1]) //如果数相同就跳过
b[cnt++] = a[i] - a[i-1];
}
if(cnt == 0)//如果所有数都相同
{
if(a[0] == 1) printf("Case %d: 1\n",cas++);
else printf("Case %d: 0\n",cas++);
continue;
}
ll g = b[0];
rep(i,1,cnt) g = gcd(g,b[i]);
if(g == 1) //不可能的情况
{
printf("Case %d: -1\n",cas++);
continue;
}
if(gcd(g,a[0]) > 1) g = gcd(g,a[0]);
rep(i,2,100000)
{
if(g % i == 0)
{
g = i;
break;
}
}
ll ans;
if(a[0] % g) ans = g - a[0] % g;
else ans = 0;
printf("Case %d: %lld\n",cas++,ans);
}
}

upc组队赛17 Greatest Common Divisor【gcd+最小质因数】的更多相关文章

  1. 最大公约数Greatest Common Divisor(GCD)

    一 暴力枚举法 原理:试图寻找一个合适的整数i,看看这个整数能否被两个整形参数numberA和numberB同时整除.这个整数i从2开始循环累加,一直累加到numberA和numberB中较小参数的一 ...

  2. Greatest common divisor(gcd)

    欧几里得算法求最大公约数 If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop. If B = 0 then GCD(A,B) ...

  3. CCPC2018 桂林 G "Greatest Common Divisor"(数学)

    UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...

  4. 2018CCPC桂林站G Greatest Common Divisor

    题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...

  5. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  6. 845. Greatest Common Divisor

    描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...

  7. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

  8. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  9. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

随机推荐

  1. Mock接口数据 = mock服务 + iptable配置

    一.mock接口数据应用场景: 1.测试接口A,A接口代码中调用其他服务的B接口,由于开发排期.测试环境不通等原因,依赖接口不可用 2.测试异常情况,依赖接口B返回的数据格式不对.返回None.超时等 ...

  2. Ubuntu中找不到pip3命令的解决方法

    Ubuntu中找不到pip3命令的解决方法 Ubuntu 有 python2 和 python3. 今天使用 Ubuntu 中的 python3 时,想要安装第三方库却发现 pip 指向的是 pyth ...

  3. 让Elasticsearch飞起来!——性能优化实践干货

    原文:让Elasticsearch飞起来!--性能优化实践干货 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog ...

  4. HTTPS证书转换成PEM格式

    PEM 格式的证书文件(*.pem)一般为以下格式: 注意:PEM 格式证书文件可用 notepad++ 等文本编辑器打开. CER / CRT 格式证书转换为 PEM 格式 对于 CER / CRT ...

  5. 安装Erlang使用RabbitMQ

    首先登陆官网进行下载:https://www.erlang.org/downloads/20.3 本次下载的版本是20.3,rabbitmq准备使用3.7.17版本 现在开始安装 因为是使用c#语言, ...

  6. 线程数设置和CPU数的关系

    一般说来,大家认为线程池的大小经验值应该这样设置:(其中N为CPU的个数) 如果是CPU密集型应用,则线程池大小设置为N+1 如果是IO密集型应用,则线程池大小设置为2N+1(因为io读数据或者缓存的 ...

  7. windows 安装 mysql 5.6

    从官方网站下载安装包:mysql-5.6.33-winx64.zip,解压到d:\java,然后将解压后的bin目录加入系统环境变量Path中,进入mysql根目录,编辑my-default.ini, ...

  8. 【ElicitSearch】启动流程

    一.集群启动流程 1.选举主节点 许多节点启动,集群干的第一件事儿就是选主,之后的的流程由主节点触发. 先确定唯一的.大家公认的主节点:再想办法把最新的及其原数据复制到选举的主节点上. 选主是对Bul ...

  9. Jenkins slave-agent.jnlp 无法安装为服务(install as a service)

    问题: 在部署持续集成环境,配置slave节点时遇到一个问题,slave-agent.jnlp可以正常启动运行(不能正常启动的点这里) 但是在保存为系统服务时,slave-agent.jnlp点击[i ...

  10. Elven Postman

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...