Codeforces Round #276 (Div. 一) B. Maximum Value(哈兮)
Codeforces Round #276 (Div. 1) B. Maximum Value(哈兮)
You are given a sequence a consisting ofn integers. Find the maximum possible value of
(integer remainder ofai divided byaj), where1 ≤ i, j ≤ n
and ai ≥ aj.
Input
The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).
The second line contains n space-separated integersai (1 ≤ ai ≤ 106).
Output
Print the answer to the problem.
Sample test(s)
Input
3 3 4 5
Output
2
题目大意:求解最大的a[j]%a[i]
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define inf 2000000+10 int ha[inf]; using namespace std; int main() { int n,m,ma=0,i,j;//若求最大的a[j]%a[i]的数只有可能在a[j]倍数小的数中产生 scanf("%d",&n); memset(ha,-1,sizeof(ha)); for(i=0; i<n; i++)//哈兮预处理一下 { scanf("%d",&m); ha[m]=m; } for(i=1; i<inf; i++)//2*10^6在1000ms不会超 if(ha[i]!=i)//当前的哈兮数组为-1,则赋值为最大乡邻的最大数 { ha[i]=ha[i-1]; } int ans=0; for(i=2;i<inf;i++) { if(ha[i]==i) { for(j=i*2-1;j<inf;j+=i)//最大的数在倍数的减-1中产生 { ans=max(ans,ha[j]%i); } } } printf("%d\n",ans); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。