就是一种筛法思想的应用。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #include<stack> #include<queue> #include<cctype> #include<sstream> using namespace std; #define pii pair<int,int> #define LL long long int const int eps=1e-8; const int INF=1000000000; const int maxn=200009; int n,a[maxn],ans=-1; int main() { //freopen("in9.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } if(n==1){printf("0 ");return 0;} sort(a,a+n); for(int i=0;i<n-1;i++) { if(i&&a[i]==a[i-1]) continue; for(int t=a[i]*2;t<=a[n-1];t+=a[i]) { int p=lower_bound(a,a+n,t)-a; ans=max(ans,a[p-1]%a[i]); } ans=max(ans,a[n-1]%a[i]); } printf("%d ",ans); //fclose(stdin); //fclose(stdout); return 0; }