当前位置:网站首页>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 61. Rotating linked list
- 7. Instant-ngp
- Accumulated and thin hair!Safety Dog has once again obtained the certification of scientific and technological achievements transformation!
- Alibaba Cloud Jia Zhaohui: Cloud XR platform supports Bizhen Technology to present a virtual concert of national style sci-fi
- LeetCode 25. A set of K flipped linked lists
- 16、Pytorch Lightning入门
- Configuration swagger
- 基于PLECS的离网(孤岛)并联逆变器的Droop Control下垂控制仿真
- H264 码率控制
- iTextSharp操作PDF
猜你喜欢
Solve the idea that unit tests cannot use Scanner
多线程下自旋锁设计基本思想
漏洞管理计划的未来趋势
The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp
中科院深圳先进技术院合成所赵国屏院士组2022年招聘启事
CURRENT_TIMESTAMP(6) 函数是否存在问题?
LT8911EXB MIPI CSI/DSI to EDP signal conversion
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
培训机构学习费用是多少呢?
一文详解 implementation api embed
随机推荐
第5章 虚拟存储器
What are the five common data types of Redis?What is the corresponding data storage space?Take you to learn from scratch
Excel function formulas - HLOOKUP function
dedecms supports one-click import of Word content
查看 CUDA cudnn 版本 & 测试 cuda 和 cudnn 有效性「建议收藏」
Color map and depth map to point cloud
第六届”蓝帽杯“全国大学生网络安全技能大赛半决赛部分WriteUp
LeetCode 445. Adding Two Numbers II
An enhanced dynamic packet buffer management.论文核心部分
The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp
日记16
动态规划之最长回文子串
LeetCode 86. Delimited Linked List
mSystems | 中农汪杰组揭示影响土壤“塑料际”微生物群落的机制
娄底石油化工实验设计、建设规划概述
48MySQL数据库基础
AICOCO AI Frontier Promotion (8.10)
啥?他一个人写了个价值100万的软件,却用来开源了!
An enhanced dynamic packet buffer management. The core part of the paper
培训机构学习费用是多少呢?