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

给出 n 个数 a[0] ~ a[n-1] ,要你找出 a[i] % a[j] 最大.

处理一个数组x[i]表示 1~i 离i最近的数是什么。就可以过了。

#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int mod = (1e9+);
const int N = ;
bool num[N];
int x[N] , a;
int main()
{
int n ;
cin >> n ;
for( int i = ; i < n ; ++i ) {
cin >> a;
num[a] = ;
}
for( int i = ; i <= 2e6 ; ++i ){
if( num[i] )x[i] = i ;
else x[i] = x[i-] ;
}
int res = ;
for( int i = ; i <= 1e6 ; ++i ) if( num[i] ){
for( int j = * i ; j <= 2e6 ; j += i ) {
res = max( res , ( x[j-] ) % i );
}
}
cout << res << endl;
}

最新文章

  1. HDU 2831 (贪心)
  2. Python中如何把一个UTC时间转换为本地时间
  3. [IOS地图开发系类]2、位置解码CLGeocoder
  4. 暑假集训(1)第四弹 -----Find a way(Hdu2612)
  5. 不可以为null值的自定义类型
  6. Python 使用心得之--变量命名
  7. 关于ARC的介绍和ARC与MRC混编解决
  8. php读取文件使用redis的pipeline(管道)导入大批量数据
  9. 云计算底层技术-虚拟网络设备(Bridge,VLAN)( 转发)
  10. day2 --&gt; pyc 文件
  11. zeromq学习记录(五)vc下多线程
  12. P3302 [SDOI2013]森林(主席树+启发式合并)
  13. Luogu P3384 【【模板】树链剖分】
  14. Python小实验——读&amp;写Excel文件内容
  15. IDEA tomcat部署
  16. HDU 4690 EBCDIC (2013多校 1005题 胡搞题)
  17. Unity经验之谈-DoTween动画结束匿名委托之巨坑
  18. flash读写学习笔记与spi接口及简单测试验证(三)
  19. 【scala】基础类型
  20. SpringMVC HandlerMethodArgumentResolver自定义参数转换器

热门文章

  1. Java_2.Java编程规范
  2. Python 的PIL,可以解决ImportError The _imagingft C module is not installed
  3. vue侦听属性和计算属性
  4. sql 根据身份证判断年龄是否小于18岁
  5. CSS中表示颜色的4种方法
  6. Linux历史命令管理以及用法
  7. go语言从例子开始之Example6.if/else
  8. go语言从例子开始之Example25.通道方向
  9. awk 起始位置和长度和 mf 一致
  10. [javascript模块化]require.js简单使用