当前位置:网站首页>Codeforces Round #276 (Div. 1) B. Maximum Value
Codeforces Round #276 (Div. 1) B. Maximum Value
2022-08-10 12:05:00 【Snow_raw】
Codeforces Round #276 (Div. 1) B. Maximum Value
题意: 给定一个长为 n n n 的序列 { a i } \{ a_i\} { ai} ,求序列中最大的 a [ i ] a[i] a[i] m o d mod mod a [ j ] a[j] a[j] 其中 a i > a j a_i>a_j ai>aj
思路: 考虑到 a i a_i ai 的取值最大为 1 0 6 10^6 106 ,我们可以暴力枚举 a i a_i ai 的倍数,枚举值不超过 2 ∗ 1 0 6 2*10^6 2∗106 即可,看似 O ( n 2 ) O(n^2) O(n2) 的复杂度,实则符合调和级数的性质,即 N N N ∗ * ∗ 1 1 1 + + + N N N ∗ * ∗ 1 2 \dfrac{1}{2} 21 + + + N N N ∗ * ∗ 1 3 \dfrac{1}{3} 31 . . . ... ... = = = N l o g N NlogN NlogN ,然后再通过前缀和操作, O ( n ) O(n) O(n) 预处理出来每个数离当前数倍数最近的数,从而不断更新最大的模数。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2E6+10;
bool vis[N];
int pre[N];
int n;
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
vis[x]=true;
}
for(int i=1;i<N;i++){
if(vis[i])pre[i]=i;
else pre[i]=pre[i-1];
}
int ans=0;
for(int i=2;i<N;i++){
//调和级数
if(vis[i]){
for(int j=2*i;j<N;j+=i){
ans=max(ans,pre[j-1]%i);
}
}
}
cout<<ans<<endl;
return 0;
}
边栏推荐
猜你喜欢
随机推荐
LeetCode 24. Swap nodes in linked list pairwise
「网络架构」网络代理第一部分: 代理概述
Twikoo腾讯云函数部署转移到私有部署
Chapter9 : De Novo Molecular Design with Chemical Language Models
iTextSharp 使用详解
ASP.NET Core依赖注入系统学习教程:ServiceDescriptor(服务注册描述类型)
An enhanced dynamic packet buffer management. The core part of the paper
LeetCode 83. Remove Duplicate Elements in Sorted List
Crypto Gaming: The Future of Gaming
Common examples of regular expressions
camshift实现目标跟踪
Can CLIP also do segmentation tasks?The University of Göttingen proposed a model CLIPSeg that uses text and image prompts to perform three segmentation tasks at the same time, draining CLIP capabiliti
娄底植物细胞实验室建设基本组成要点
如何让别人看不懂你的 JS 代码?把你当大佬!
H264 GOP 扫盲
Excel函数公式大全—LOOKUP函数
制品库是什么?
LeetCode 61. Rotating linked list
搜索--09
解决 idea 单元测试不能使用Scanner









