当前位置:网站首页>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;
}
边栏推荐
- Is there a problem with the CURRENT_TIMESTAMP(6) function?
- Deploy the project halfway through the follow-up
- 2022年8月中国数据库排行榜:openGauss重夺榜眼,PolarDB反超人大金仓
- The god-level Alibaba "high concurrency" tutorial - basic + actual combat + source code + interview + architecture is all-inclusive
- 16. Getting Started with Pytorch Lightning
- LeetCode 146. LRU Cache
- 制品库是什么?
- 一文详解 implementation api embed
- Servlet---Solve the problem of Chinese garbled characters in post requests
- IDC第一的背后,阿里云在打造怎样的一朵“视频云”?
猜你喜欢
IM即时通讯开发WebSocket从入门到精通
吃透Chisel语言.36.Chisel实战之以FIFO为例(一)——FIFO Buffer和Bubble FIFO的Chisel实现
【论文+代码】PEBAL/Pixel-wise Energy-biased Abstention Learning for Anomaly Segmentation on Complex Urban Driving Scenes(复杂城市驾驶场景异常分割的像素级能量偏置弃权学习)
制品库是什么?
中科院深圳先进技术院合成所赵国屏院士组2022年招聘启事
国外媒体宣发怎样做才可以把握重点
漏洞管理计划的未来趋势
「网络架构」网络代理第一部分: 代理概述
第5章 虚拟存储器
16. Getting Started with Pytorch Lightning
随机推荐
Twikoo腾讯云函数部署转移到私有部署
Configuration swagger
Excel函数公式大全—HLOOKUP函数
Guo Jingjing's personal chess teaching, the good guy is a robot
吃透Chisel语言.36.Chisel实战之以FIFO为例(一)——FIFO Buffer和Bubble FIFO的Chisel实现
爱可可AI前沿推介(8.10)
娄底植物细胞实验室建设基本组成要点
ArcMAP出现-15的问题无法访问[Provide your license server administrator with the following information:Err-15]
太香了!自从用了这款接口神器,我的团队效率提升了 60%!
So delicious!Since using this interface artifact, my team efficiency has increased by 60%!
Golang分布式应用之etcd
CodeForces - 628D (digital dp)
Threshold-based filtering buffer management scheme in a shared buffer packet switch论文核心部分
Dining (web stream)
The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp
22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
LeetCode 61. Rotating linked list
时间序列的数据分析(五):简单预测法
How to do foreign media publicity to grasp the key points
百度用户产品流批一体的实时数仓实践