Topic Links:
Http://codeforces.com/problemset/problem/484/B
Test instructions
The maximum value of the remainder of A[i]%a[j] (A[i]>a[j])
Analysis:
The maximum value of the remainder is very obvious a[i] the closer to A[j] the larger the remainder, so we sort all the elements from large to small;
It then enumerates multiples of a[j], finds the maximum value less than a[i] multiples, and then updates the maximum value of the remainder.
The code is as follows:
#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;int a[200010];inline int Mymax (int a,int b) {return a > B? a:b;} inline int bin_search (int l,int r,int val)//binary lookup is smaller than the maximum value of val {int mid; while (l<=r) {mid= (l+r) >>1; if (a[mid]==val) return mid-1; else if (a[mid]>val) r=mid-1; else l=mid+1; } return R;} int main () {int n; while (~SCANF ("%d", &n)) {for (int i=0;i<n;i++) scanf ("%d", &a[i]); Sort (a,a+n); int Mmax = 0; for (int i=0;i<n-1;i++) {if (a[i]==0| | A[i]!=a[i-1]) {//pruning. If you have seen it before, you don't have to check it; int tmp=a[i]+a[i]; while (tmp<=a[n-1]) {int pos=bin_search (0,N-1,TMP); Mmax = Mymax (Mmax,a[pos]%a[i]); Tmp+=a[i]; } Mmax =mymax (Mmax,a[n-1]%a[i]); }} printf ("%d\n", Mmax); } return 0;}
Codeforces 484B Maximum Value (sort + dichotomy)