题目描述
Bob’s father’s birthday is coming,.Because Bob’s father is a math teacher so that Bob wanted to give him an array of positive intergers a of length n as a gift.His father thinks that an array’s beauty is the greatest common divisor of all its elements. Bob wants to give him as beautiful an array as possible. But the shop has only one array a left. So Bob wants to decrease some numbers in the array(no more than K for each number).Bob can obtain array b from array a if the following conditions hold: bi > 0; 0 ≤ ai - bi ≤ k for all 1 ≤ i ≤ n.Help Bob find the maximum possible beauty of the array he will give to his father .
输入
The first line contains two integers n and k (1 ≤ n ≤ 3·1e5; 1 ≤ k ≤ 1e6). The second line contains n integers ai (1 ≤ ai ≤ 1e6) — array a.
输出
In the single line print a single number — the maximum possible beauty of the resulting array.
样例输入
6 1
3 6 10 12 13 16
样例输出
3
题意
一个序列,n个元素,每个数可以向下减少最多k个值,问能得到的序列的最大公共因子是多少。减少一定值以后的数要大于等于0。
思路
找a[i]%ans<=k;这个关系以后这个题基本就可以用暴力写了。
不过最坑的事情还是发生了。
这道题竟然TM的是多组输入。读题神TM能读出是多组输入来。
代码
*#include <bits/stdc++.h>
using namespace std;
int a[1000000];
int main()
{
int n,k,i,j;
while(scanf("%d %d",&n,&k)!=EOF){
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
int h = a[0];
for(i=h;i>=1;i--)
{
int f=0;
for(j=0;j<n;j++)
{
if(a[j]%i>k)
{
f = 1;
break;
}
}
if(f==0)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}