UPC备战省赛组队训练赛第十七场

            with zyd,mxl

G: Greatest Common Divisor

题目描述
There is an array of length n, containing only positive numbers.
Now you can add all numbers by many times.
Please find out the minimum times you need to perform to obtain an array whose greatest common divisor(gcd) is larger than or state that it is impossible.
You should notice that if you want to add one number by , you need to add all numbers by at the same time. 输入
The first line of input file contains an integer T (≤T≤), describing the number of test cases.
Then there are ×T lines, with every two lines representing a test case.
The first line of each case contains a single integer n (≤n≤1e5) described above.
The second line of that contains n integers ranging in [,1e9]. 输出
You should output exactly T lines.
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 - instead.

题目描述

样例输入

样例输出
Case :
Case : -
Case :

样例输入输出

题意:

  定义a[]数组存储输入的 n 个数;

  求使得 ∀i∈[1,n] GCD(a[i]+x) > 1 的最小的 x;

  如果不存在这样的x,输出-1;

思路:

  将数组 a 排序,去重;

  ①去重后,如果只有一个元素,输出 (a[1] == 1 ? 1:0);

  ②找到相邻两数差值的GCD记为gcd:

    (2.1)如果 gcd == 1 ,输出 -1

    (2.2)反之,所有数肯定可以通过增加 x 使得所有数变为 gcd 的倍数,当然也可以变为gcd因子的倍数,

      求解出最小的x输出;

AC代码:

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+; int n;
int a[maxn]; int GCD(int _a,int _b)
{
return _a == ? _b:GCD(_b%_a,_a);
}
ll Solve()
{
sort(a+,a+n+);
int t=unique(a+,a+n+)-a;
t--;
if(t == )///情况①
return a[] == ? :; ll gcd=a[]-a[];
for(int i=;i <= t;++i)
gcd=GCD(gcd,a[i]-a[i-]); if(gcd == )///情况(2.1)
return -;
ll ans=(a[]/gcd+(a[]%gcd == ? :))*gcd-a[];///情况(2.2)
for(ll i=;i*i <= gcd;++i)
{
if(gcd%i != )
continue;
ll j=gcd/i;
///找到最小的 curAns 使得所有数 +curAns 都可以变为 i,j 的倍数
ll curAns=(a[]/i+(a[]%i == ? :))*i-a[];
curAns=min(curAns,(a[]/j+(a[]%j == ? :))*j-a[]);
ans=min(ans,curAns);
}
return ans;
}
int main()
{
int test;
scanf("%d",&test);
for(int kase=;kase <= test;++kase)
{
scanf("%d",&n);
for(int i=;i <= n;++i)
scanf("%d",a+i); printf("Case %d: %lld\n",kase,Solve());
}
return ;
}

小结:

 比赛的时候,只考虑了使所有的数都变成gcd的最小的因子的倍数的情况;
并没有考虑到所有数变成gcd的其他因子的倍数使得答案最小;
赛后,吃完午饭美美的睡上了一觉;
午睡刚醒,就看到队友zyd给我发的G题ac的截图;
一脸懵逼的我问了句为啥????
例如 差值为21
21的非1的因子有3,,
所有数都变成3的倍数需要 +
所有数都变成7的倍数需要 +
所有数都变成21的倍数需要 +
答案当然是1啦,所以说,最优解不一定是变成gcd最小因子的倍数

CCPC2018 桂林 G "Greatest Common Divisor"(数学)的更多相关文章

  1. 2018CCPC桂林站G Greatest Common Divisor

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

  2. hdu 5207 Greatest Greatest Common Divisor 数学

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

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

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

  4. upc组队赛17 Greatest Common Divisor【gcd+最小质因数】

    Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...

  5. [UCSD白板题] Greatest Common Divisor

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

  6. greatest common divisor

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

  7. 845. Greatest Common Divisor

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

  8. CF1025B Weakened Common Divisor 数学

    Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...

  9. LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45

    1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...

随机推荐

  1. 重磅发布:阿里开源 Open JDK 长期支持版本 Alibaba Dragonwell

    3 月 21 日北京阿里云峰会,阿里巴巴正式宣布对外开源 OpenJDK 长期支持版本 Alibaba Dragonwell.作为 Java 全球管理组织 Java Community Process ...

  2. img标签src不给路径就会出现边框

    <img/>在src加载失败或没有给的,浏览器会自动给img加上边框. 如下图这样: 产品觉得影响美观,一定要pass掉. 原码是这样: .ctn{ position: relative; ...

  3. BMDP为常规的统计分析提供了大量的完备的函数系统,如:方差分析(ANOVA)、回归分析(Regression)、非参数分析(Nonparametric Analysis)、时间序列(Times Series)等等。此外,BMDP特别擅于进行出色的生存分析(Survival Analysis )。许多年来,一大批世界范围内顶级的统计学家都曾今参与过BMDP的开发工作。这不仅使得BMDP的权威性得到

        BMDP是Bio Medical Data Processing的缩写,是世界级的统计工具软件,至今已经有40多年的历史.目前在国际上与SAS.SPSS被并称为三大统计软件包.BMDP是一个大 ...

  4. jquery鼠标悬停突出显示

    在线演示 本地下载

  5. day39 07-Spring的AOP:自动代理

    带有切点的切面或者是不带有切点的切面配置一个类就要配置一段生成代理的代码,这样太麻烦了. 选中orderDao右键watch JDK动态代理是先创建被代理对象,然后在创建代理对象的时候传入被代理对象. ...

  6. MaxCompute问答整理之6月

    本文是基于本人对MaxCompute产品的学习进度,再结合开发者社区里面的一些问题,进而整理成文.希望对大家有所帮助,下面咱们正式开始. 1.什么是MaxCompute呢?MaxCompute是由阿里 ...

  7. 【Leetcode堆和双端队列】滑动窗口最大值(239)

    题目 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 示例: 输入 ...

  8. 中文乱码在java中URLEncoder.encode方法要调用两次解决

    中文乱码在java中URLEncoder.encode方法要调用两次解决 一.场景: 1.我在客户端要通过get方式调用服务器端的url,将中文参数做utf-8编码,需要在js中两次的进行编码,服务器 ...

  9. jQuery 图片跟着鼠标动

    html默认鼠标样式改成图片时格式为 .ani 图片跟随鼠标挪动 html <div id="mouseImg"> <img src="images/问 ...

  10. oracle函数 TO_DATE(X[,c2[,c3]])

    [功能]将字符串X转化为日期型 [参数]c2,c3,字符型,参照to_char() [返回]字符串 如果x格式为日期型(date)格式时,则相同表达:date x 如果x格式为日期时间型(timest ...