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. ssh远程钥匙对连接

    1.服务器必须启动ssh服务 2.在客户机执行命令:ssh-keygen -t rsa 两次回车即可 3.在客户机家目录下的.ssh\下生成钥匙对 4.将公钥传输到要连接的服务器主机要连接的用户家目录 ...

  2. FZUOJ-2273 Triangles

     Problem 2273 Triangles Accept: 109    Submit: 360 Time Limit: 1000 mSec    Memory Limit : 262144 KB ...

  3. ES6判断当前页面是否微信浏览器中打开

    1.使用jq判断是否用微信浏览器打开页面 var is_weixin = (function(){return navigator.userAgent.toLowerCase().indexOf('m ...

  4. hihocoder1954 : 压缩树

    传送门 首先求出缩一个点 $x$ 的贡献,就是缩 $x$ 的父亲的贡献加上 $x$ 的子树多减少的深度 假设此时缩父亲的贡献已经考虑过了,那么 $x$ 的子树多减少的深度就是子树的节点数 注意此时要满 ...

  5. Spring boot集成Swagger,并配置多个扫描路径

    Spring boot集成Swagger,并配置多个扫描路径 1:认识Swagger Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目 ...

  6. 输入某人出生日期(以字符串方式输入,如1987-4-1)使用DateTime和TimeSpan类,(1)计算其人的年龄;(2)计算从现在到其60周岁期间,总共多少天。

    http://blog.csdn.net/w92a01n19g/article/details/8764116 using System;using System.Collections.Generi ...

  7. Spring Cloud Stream监听已存在的Queues/Exchanges

    环境准备 rabbitmq已运行,端口5672,控制台web端口15672,用户名密码guest/guest 引入spring cloud stream依赖 compile('org.springfr ...

  8. Ubuntu16.04 启用root权限

    装了Ubuntu 16.04之后想使用超级权限对系统进行操作 使用命令 su - 切换超级用户,提示输入密码,却怎么都不对,网上找的资料说是没有启用root权限,于是根据网上提供的方法启用root权限 ...

  9. lvm分区创建和扩容

    shell> fdisk /dev/xvdb #### 选择磁盘 Command (m for help): m #### 帮助 Command action a toggle a bootab ...

  10. 前端面试题(4)JavaScript

    前端面试题JavaScript(一) JavaScript的组成 JavaScript 由以下三部分组成: ECMAScript(核心):JavaScript 语言基础 DOM(文档对象模型):规定了 ...