枚举gcd为
g
然后再枚举
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const int N=1000005;
int n,K;
int a[N],c[N];
int main(){
int m=0;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(K);
for (int i=1;i<=n;i++) read(a[i]),m=max(m,a[i]),c[a[i]]++;
for (int i=1;i<=m;i++) c[i]+=c[i-1];
for (int i=m;i>1;i--){
int sum=0;
for (int j=i;j<=m;j+=i)
sum+=c[min(m,min(j+i-1,j+K))]-c[j-1];
if (sum==n)
return printf("%d\n",i),0;
}
printf("1\n");
return 0;
}