当前位置:网站首页>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 82. Remove Duplicate Elements in Sorted List II
- 16、Pytorch Lightning入门
- Behind IDC's No. 1 position, what kind of "video cloud" is Alibaba Cloud building?
- 娄底疾控中心实验室设计理念说明
- 多线程下自旋锁设计基本思想
- Common examples of regular expressions
- LT8911EXB MIPI CSI/DSI转EDP信号转换
- CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
- 娄底石油化工实验设计、建设规划概述
- CV复习:空洞卷积
猜你喜欢

IM即时通讯开发WebSocket从入门到精通

So delicious!Since using this interface artifact, my team efficiency has increased by 60%!

如何让别人看不懂你的 JS 代码?把你当大佬!

神经网络学习-正则化

Data Analysis of Time Series (5): Simple Prediction Method
Database management tool: dynamic read-write separation

Chapter9 : De Novo Molecular Design with Chemical Language Models

面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官

加密游戏:游戏的未来

中科院深圳先进技术院合成所赵国屏院士组2022年招聘启事
随机推荐
Excel function formulas - HLOOKUP function
Chapter9 : De Novo Molecular Design with Chemical Language Models
The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp
Crypto Gaming: The Future of Gaming
讯飞创意组别 全国选拔赛成绩公布说明
StarRocks on AWS 回顾 | Data Everywhere 系列活动深圳站圆满结束
leetcode/两个链表的第一个重合节点
“68道 Redis+168道 MySQL”精品面试题(带解析)
「企业架构」应用架构概述
Excel function formulas - LOOKUP function
查看 CUDA cudnn 版本 & 测试 cuda 和 cudnn 有效性「建议收藏」
wirshark 常用操作及 tcp 三次握手过程实例分析
Configure druid data source "recommended collection"
Hackbar 使用教程
LeetCode 109. Sorted Linked List Conversion Binary Search Tree
dedecms supports one-click import of Word content
Database management tool: dynamic read-write separation
Dining (网络流)
LeetCode 86. Delimited Linked List
关于flask中static_folder 和 static_url_path参数理解