O (n2) tle. O (NLOGNLOGN)
#include <cstdio> #include <cstring> #include <cctype> #include <algorithm>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) #define CLR (x,c) memset (X,c, sizeof (x)) int read () {int X=0;char C=getchar (), while (!isdigit (c)) C=getchar (), while (IsDigit (c)) x=x*10+c-' 0 ', c= GetChar (); return x;} const int Nmax=2e5+5;int a[nmax];int Main () {int n=read (); Rep (I,1,n) A[i]=read (); sort (a+1,a+n+1); int Ans=0,tp;rep (i,1, n-1) {for (int j=a[i]+a[i];j<=a[n]*2;j+=a[i]) {tp=lower_bound (a+1,a+n+1,j)-a-1;ans=max (Ans,a[tp]%a[i]);}} printf ("%d\n", ans); return 0; }
1421 Max MoD value title Source: Codeforces Base time limit: 1 second space limit: 131072 KB score: 80 Difficulty: 5-Level algorithm topic collection concern
There is an an an array of n integers. Now you want to find two numbers (can be the same one)ai,aJ Makesai mod aJ Max and ai ≥ aJ.
Input
A single set of test data. The first line contains an integer n, which represents the size of the array A. (1≤n≤2*10^5) The second line has n spaces separated by an integer AI (1≤ai≤10^6).
Output
Output An integer representing the maximum mod value.
Input example
33 4 5
Output example
2
51nod1421 Max MoD Value