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. 国内首个全域边缘节点服务发布,阿里云助力企业把握5G机遇

    7月24日,阿里云峰会开发者大会在上海世博中心举办.作为2019年首场最受瞩目的云计算开发者大会,阿里云携一众云计算技术大牛与开发者面对面,探讨各自领域的技术干货与前沿趋势.同时,也发布了多项重大重磅 ...

  2. 【转载】【python】python练手项目

    入门篇 1.Python - Python 图片转字符画 50 行 Python 代码完成图片转字符画小工具. &lt;img src="https://pic3.zhimg.com ...

  3. 获取文章,显示时自动换行(word-break与 work-wrap的区别)

    HTML:<div class="na-i"> <span> </span></div>样式:.na-i{ overflow-y: ...

  4. larbin终于编译完成

    1.目前sourceforge不能下载,只能找mirror进行下载了: 2.一些头文件始终找不到,定位了一下,应该是makefile中设置有问题,把CXXFLAGS中的-I-去掉,即可编译成功:

  5. 在linux里如何建立一个快捷方式,连接到另一个目录

    用软链接 用法:ln -s 源目录 目标快捷方式, 比如你要在/etc下面建立一个叫LXBC553的快捷方式,指向/home/LXBC,那就是 ln -s /home/LXBC   /etc/LXBC ...

  6. Same Tree 深度优先

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  7. 【Mysql的那些事】Django数据库配置

    1:安装与配置 1:pip install PyMySQL(或者直接使用Pycharm的setting,点击添加PyMySQL) 2:在Django的工程同名子目录的__init__.py文件中添加如 ...

  8. 从入侵到变现——“黑洞”下的黑帽SEO分析

    概述 由于互联网入口流量主要被搜索引擎占据,网站在搜索引擎中的排名直接影响到市场营销效果,因此SEO服务应运而生.SEO(Search Engine Optimization)全称为搜索引擎优化,是指 ...

  9. Nacos 发布 1.0.0 GA 版本,可大规模投入到生产环境

    经过 3 个 RC 版本的社区体验之后,Nacos 正式发布 1.0.0 GA 版本,在架构.功能和 API 设计上进行了全方位的重构和升级. 1.0.0 版本的发布标志着 Nacos 已经可以大规模 ...

  10. @loj - 2250@ 「ZJOI2017」仙人掌

    目录 @题目描述@ @solution@ @accepted code@ @details@ @题目描述@ 如果一个无自环无重边无向连通图的任意一条边最多属于一个简单环,我们就称之为仙人掌.所谓简单环 ...