Codeforces 485D Maximum Value【思维+数论】
来源:互联网 发布:安卓去软件广告 编辑:程序博客网 时间:2024/06/05 14:20
D. Maximum Value
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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.
Examples
Input
33 4 5
Output
2
题目大意:
给你N个数,让你找到两个数Ai,Aj,使得Ai%Aj的值最大,这里需要保证Ai>Aj;
题解:排序后,枚举每个数a[i],然后再枚举a[i]的倍数k*a[i],找到k*a[i]~(k+1)*a[i]中模上a[i]的最大的数,那个数一定是小于(k+1)*a[i]的最大数,然后取模后与答案比较,取最大值
#include<bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;typedef long long ll;typedef pair<int,int> pii;const int N=2000010,MOD=1e9+7;int a[200020];int pre[N];bool have[N];int main(){ int n;cin>>n; for(int i=0;i<n;i++) scanf("%d",a+i),have[a[i]]=1; sort(a,a+n); n = unique(a,a+n)-a; int mx = a[n-1]; mx<<=1; int last=0; for(int i=1;i<=mx;i++){ if(!have[i]) pre[i] = last; else{ pre[i]=last; last=i; } } int ans=0; int i=a[0]==1; for(;i<n;i++){ for(int j=a[i]+a[i];j<mx;j+=a[i]){ if(j-a[n-1]>=a[i]) break; int ret = pre[j]; if(j-ret >= a[i]) continue; ans = max(ans,a[i]-(j-ret)); } } cout<<ans<<endl; return 0;}
阅读全文
0 0
- Codeforces 485D Maximum Value【思维+数论】
- Codeforces 485D Maximum Value【思维+技巧枚举】
- codeforces 485D. Maximum Value
- codeforces D. Maximum Value
- 51nod 1421 最大MOD值 & codeforces 485D Maximum Value(思维 数学)
- CodeForces-485D Maximum Value(暴力)
- Codeforces Round #276 (Div. 1) B. Maximum Value(数论)
- Codeforces 484B Maximum Value——思维
- Codeforces Round #276 (Div. 2)-D. Maximum Value
- 【Codeforces Round 276 (Div 2)D】【数论 正难则反 枚举倍数 二分 O(nlogn)】Maximum Value n个数大数mod小数找最大余数
- cf 485d Maximum Value 【二分】
- Maximum Value CodeForces
- codeforces 276D D. Little Girl and Maximum XOR(贪心+dp+数论)
- CodeForces 484B Maximum Value
- Codeforces 484B Maximum Value
- codeforces 765D 思维
- codeForces 612D(思维)
- codeforces 673D(思维)
- MATLAB 神经网络NAR时间序列做预测
- python 过滤html方法
- spark streaming restart error: org.apache.spark.SparkException: Yarn application has already ended!
- Electron打包
- Codeforces 246E Blood Cousins Return
- Codeforces 485D Maximum Value【思维+数论】
- 第一课 Python爬虫初识与网络请求
- Python中:self和__init__的含义 + 为何要有self和__init__
- 1811:水仙花数
- DateUtil时间转换工具类
- 拜尔矩阵详解
- H5 单页面解决“前进”,“后退”功能
- 超参数调试
- 读书笔记——简单排序算法