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. ubuntu18+virtualenv配置

    思路: 1.两条命令安装,加sudo是安装在/usr/local/bin/virtualenv路径下,不加就安装在home/.local下.注意,后面bashrc里,要设置的路径和这个有关系,所以要区 ...

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

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

  3. vue防抖节流函数---组件封装,防止按钮多次点击

    1.vue 封装utils.js /** * @param {function} func 执行函数 * @param {number} time 防抖节流时间 * @param {boolean} ...

  4. MongoDB的使用学习之(二)简介

    原文链接:http://www.cnblogs.com/yxlblogs/p/3681089.html MongoDB 是一个高性能,开源,无模式的文档型数据库,是当前 NoSQL 数据库产品中最热门 ...

  5. MiniUI学习笔记1-表单控件

    1.输入框样式 class="mini-textbox" //普通输入框 class="mini-password" //密码输入框 class="m ...

  6. C++ 编译器的安装(MinGW)

    GNU GNU是一个自由软件工程项目,GNU工程已经开发了一个被称为“GNU”(GNU是“不是UNIX”的缩写)的.对Unix向上兼容的完整的自由软件系统(free software system). ...

  7. 【记录】spring boot 全局捕获异常@ExceptionHandler与@Validated @RequestBody 配合使用

    @ExceptionHandler与@Validated @RequestBody 三者配合使用可以很好的做到入参校验,具体demo如下: 接口 import org.springframework. ...

  8. Mac sublime 安装包的时候出现 unable to download xxx (_ssl.c:548)

    Mac sublime 安装包的时候出现 unable to download xxx前置条件:[本文行文中,所使用的电脑环境为 mac](当然不排除,在其他系统下,依然可以采用这种解决方案) 今天想 ...

  9. 牛客ACM赛 C 区区区间间间

    链接 C 区区区间间间 给定长度为\(n\)序列,求\[\sum_{i=1}^{n} \sum_{j=i}^{n} max-min\] 其中\(max\),\(min\)为区间最大,最小值,\(n\l ...

  10. JS基础入门篇(七)—运算符

    1.算术运算符 1.算术运算符 算术运算符:+ ,- ,* ,/ ,%(取余) ,++ ,-- . 重点:++和--前置和后置的区别. 1.1 前置 ++ 和 后置 ++ 前置++:先自增值,再使用值 ...