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最小因子的倍数

最新文章

  1. 【Discuz】-QQ互联登陆提示错误信息:Unknown column &#39;conuintoken&#39; in &#39;field list&#39;
  2. HTML5之WebSocket
  3. ubuntu 下安装sh 文件
  4. Java之this详解
  5. freemarker中判断对象是否为空
  6. POJ 2186 Popular Cows(强连通分量缩点)
  7. SpringSecurity 在MVC 中的简单使用(翻译的,稍加改动)
  8. Linux命令:head命令详解
  9. windows下搭建PHP环境
  10. Ci 自己的分页类【原创】
  11. HibernateTemplate用法
  12. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(20)-多条件模糊查询和回收站还原的实现
  13. openwrt系统之字符设备驱动软件包加载、测试程序加载
  14. 如何用Ettercap实现“中间人攻击”(附下载链接)
  15. 微信小程序+和风天气完成天气预报
  16. Android OpenGL ES 开发(八): OpenGL ES 着色器语言GLSL
  17. react 阻止事件冒泡
  18. MongoDB 最大连接数 设置失效的异常分析
  19. 替换iframe的内容
  20. Eclipse 在线安装properties编辑插件

热门文章

  1. 如何使用Data Lake Analytics创建分区表
  2. jmeter测试APP时如何录制脚本
  3. day39 09-Spring的AOP:基于AspectJ的通知类型
  4. 2019-8-31-C#-标准性能测试高级用法
  5. cloud-music
  6. PLAY2.6-SCALA(六) 异步处理结果
  7. [Linux]jenkins的安装 标签: linux服务器 2016-08-21 20:47 1060人阅读 评论(23)
  8. github.com访问慢解决
  9. Implement strStr() 字符串匹配
  10. Alpha版本第一周作业