CCPC2018 桂林 G "Greatest Common Divisor"(数学)
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"(数学)的更多相关文章
- 2018CCPC桂林站G Greatest Common Divisor
		题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ... 
- hdu 5207 Greatest Greatest Common Divisor 数学
		Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ... 
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
		定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ... 
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
		Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ... 
- [UCSD白板题] Greatest Common Divisor
		Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ... 
- greatest common divisor
		One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ... 
- 845. Greatest Common Divisor
		描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ... 
- CF1025B Weakened Common Divisor 数学
		Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input st ... 
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
		1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ... 
随机推荐
- 【python小随笔】单例模式设计(易懂版)
			1:单例模式原理 大道理:希望在系统中某个对象只能存在一个,单例模式是最好的解决方案,单例模式是一种常见的软件设置模式,在它的核心结构中只包含一个被称为单例类的特殊类,通过单例模式可以保证系统中的一个 ... 
- 第一次作业:reading and prepare
			这个作业属于哪个课程 课程的链接 这个作业要求在哪里 作业要求的链接 我在这个课程的目标是 理解软件开发流程,更好的开发自己的软件 这个作业在哪个具体方面帮助我实现目标 对"工程" ... 
- Directx11教程(20) 一个简单的水面
			原文:Directx11教程(20) 一个简单的水面 nnd,以前发的这篇教程怎么没有了?是我自己误删除了,还是被系统删除了? 找不到存稿了,没有心情再写一遍了. 简单说一下,本篇教程就是实 ... 
- Leetcode806.Number of Lines To Write String写字符串需要的行数
			我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行.我们给定了一个数组 width ... 
- 通过DataWorks数据集成归档日志服务数据至MaxCompute进行离线分析
			通过DataWorks归档日志服务数据至MaxCompute 官方指导文档:https://help.aliyun.com/document_detail/68322.html但是会遇到大家在分区上或 ... 
- tyvj1017 冗余关系
			描述 Mrs.Chen是一个很认真很称职的语文老师 ......所以,当她看到学生作文里的人物关系描述得非常的麻烦的时候,她非常生气,于是宣布:凡是作文里有冗余关系的,一率罚抄出师表10次...同学们 ... 
- docker 与host互传文件
			docker 的cp命令可以从容器往外复制,也可以从本机复制的容器. docker cp 文件路径 容器id:/容器目录 docker help cp Usage: docker cp [OPT ... 
- LeedCode OJ --- Binary Tree Inorder Traversal
			点击打开题目链接 今天只是写了递归的版本,因为还没想好怎么用迭代来实现,可以写的过程中,有一点是有疑问的,虽然我的代码可以AC. 问题是:主调函数是可以使用子函数中返回的在子函数中定义的vector. ... 
- C# 获取上传文件的文件名和后缀名
			//获得要上传的文件 HttpPostedFile file = Request.Files[]; //获得到文件名 string fileName = System.IO.Path.GetFileN ... 
- 小爬爬1.requests基础操作
			1.requests安装的问题 (1)如果requests没有安装,我们需要先安装这个模块,在cmd安装不了,我们可以在下面的位置,打开的窗体安装requests模块 pip install requ ... 
