给你一个数组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 For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
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 MAXN (2234567)
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;
}