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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值