http://codeforces.com/contest/355/problem/E求n个数(s[i]-k,i=1...n)的最大公约数
来源:互联网 发布:linux调整home分区大小 编辑:程序博客网 时间:2024/05/17 22:39
Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.
Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).
The seller can obtain array b from array a if the following conditions hold: bi > 0; 0 ≤ ai - bi ≤ k for all 1 ≤ i ≤ n.
Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).
The first line contains two integers n and k (1 ≤ n ≤ 3·105; 1 ≤ k ≤ 106). The second line contains n integers ai (1 ≤ ai ≤ 106) — array a.
In the single line print a single number — the maximum possible beauty of the resulting array.
6 13 6 10 12 13 16
3
5 38 21 52 15 77
7
In the first sample we can obtain the array:
3 6 9 12 12 15
In the second sample we can obtain the next array:
7 21 49 14 77
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=3*100000+10;int s[MAX];int main(){int n,k;while(cin>>n>>k){int gcd=INF,temp;for(int i=0;i<n;++i){scanf("%d",&s[i]);gcd=min(gcd,s[i]);}while(true){//判断gcd是否可以是全部的s[i]-t(t=0~k)的公约数 temp=gcd;for(int i=0;i<n;++i){int num=s[i]/gcd;//最多是gcd的num倍if(num>0 && s[i]-num*gcd<=k)continue;//表示gcd可以是s[i]-t的约数(t=0~k)//gcd将减小,num=s[i]/gcd必须变大才可能满足s[i]-num*gcd<=k,所以由s[i]去确认新的最大可能的gcd gcd=s[i]/(num+1);//s[i]可能可以的最大约数 }if(gcd == temp)break;}//time=n*logn cout<<gcd<<endl;}return 0;}
- http://codeforces.com/contest/355/problem/E求n个数(s[i]-k,i=1...n)的最大公约数
- Codeforces Round #353 (Div. 2) E. Trains and Statistic(求d[i][j]的和的最小值(1<=i<=n,i+1<=j<=n))
- 求N个数的最大公约数
- POJ 2480 Longge's problem [ 求 Σgcd(i,n)(1<=i<=n) ] [欧拉函数]
- Worm.Win32.D.i.s.k.G.e.n(磁.碟.机)的分析
- 求n个数的最小公倍数与最大公约数
- N个数求最大的k个数
- Codeforces Round #181 (Div. 2) E. Empire Strikes Back N!∣∏K,i=1ai!
- n个数的最大公约数
- 求组合数: 求n个数(1....n)中k个数的组合
- 2.9 成为符合A N S I和U n i c o d e的应用程序
- Session和Cookies的区别
- 求n个数的第K小数
- K D E是基于Troll Te c h n o l o g i e s公司的Q t库的
- hdu 3664 1~n排列(ai>i ) 为k个数
- Codeforces 61E Enemy is weak 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组
- R E C U R S I O N
- sed -e -i n 组合
- AngularJS 入门教程 $http is not defined 解决方案
- java 从零开始,学习笔记之基础入门(一)
- vs2010 编译poco
- ext中下拉框的显示对应问题和gird列表显示问题
- 验证码图片识别
- http://codeforces.com/contest/355/problem/E求n个数(s[i]-k,i=1...n)的最大公约数
- 修改linux tcp连接数
- [转载]2014找工作总结-机会往往留给有准备的人@http://blog.csdn.net/xiajun07061225/article/details/12844801
- Java程序员的4大职业方向
- 创建引线对象(Leader)
- 使用iozone测试磁盘性能
- [Android] 文字翻转动画的实现
- IOZONE测试工具使用方法
- Linux内核中的内存都不分页(unpagable)