Codeforces Round #276 (Div. 1)B. Maximum Value 筛法

D. Maximum Value
 
 

You are given a sequence a consisting of n integers. Find the maximum possible value of  (integer remainder of ai divided byaj), where 1 ≤ 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 integers ai (1 ≤ ai ≤ 106).

Output

Print the answer to the problem.

Sample test(s)
input
3
3 4 5
output
2

题意:给你一个a数组,问你最大的ai%aj是多少(ai>aj);
题解:我们要求ai%aj最大,ai%(aj*k)最大,类似筛法去最大就是了
///1085422276

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';ch=getchar();
    }return x*f;
}
//****************************************
const int  N=200000+50;
#define mod 10000007
#define inf 1000000001
#define maxn 10000

int a[N];
int main() {
    int n=read();
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
      //  H[a[i]]=1;
    }
    sort(a+1,a+n+1);
    a[0]=-1;int ans=-1;
    for(int i=1;i<=n;i++) {
        if(a[i]==a[i-1])continue;
        for(int j=a[i]+a[i];j<=a[n];j+=a[i])
        {
            int tmp=lower_bound(a+1,a+n+1,j)-a;
            ans=max(ans,a[tmp-1]%a[i]);
        }
        ans=max(ans,a[n]%a[i]);
    }
    cout<<ans<<endl;
    return 0;
}
代码
 
原文地址:https://www.cnblogs.com/zxhl/p/4965113.html