给你一个数组a,给你一个k,你可以讲每个数减去不超过k,要求最后的GCD最大,求这个gcd
1 ≤ n ≤ 3·1e5; 1 ≤ k ≤ 1e6
1 ≤ ai ≤ 1e6
显然min(ai)<=k时,答案为min(ai),
否则,每个数都对应一段长度为k的可行区间,答案至少为k。
考虑gcd=g>k,则此时可能的ai,可能为[g,g+k],[2g,2g+k],⋯等,区间数为106g
复杂度为O(n(n+n/2+n/3+...+n/n))=O(nlogn)
#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
const int MA=1E6;
int a[MAXN];
int f[MAXN];
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout);
int n,k;
while(cin>>n>>k) {
For(i,n) a[i]=read();
int ans=k;
sort(a+1,a+1+n);
n=unique(a+1,a+1+n)-a-1;
MEM(f)
For(i,n) f[a[i]]++;
For(i,1e6) f[i]+=f[i-1];
if (a[1]<=k) cout<<a[1]<<endl;
else {
for(int i=a[n];i>k;i--) {
int p=0;
for(int j=i;j<=a[n];j+=i) p+=f[min(MA,j+k)]-f[j-1];
if (p==n) {
ans=i;
break;
}
}
cout<<ans<<endl;
}
}
return 0;
}