当前位置:网站首页>Codeforces Round #276 (Div. 1) B. Maximum Value
Codeforces Round #276 (Div. 1) B. Maximum Value
2022-08-10 12:56: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 的倍数,The enumeration value does not exceed 2 ∗ 1 0 6 2*10^6 2∗106 即可,看似 O ( n 2 ) O(n^2) O(n2) 的复杂度,In fact, it conforms to the properties of harmonic series,即 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 ,And then through the prefix and operation, O ( n ) O(n) O(n) Preprocess each number to the nearest multiple of the current number,Thus continuously updating the largest modulus.
代码:
#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;
}
边栏推荐
猜你喜欢

A detailed explanation of implementation api embed

LT8911EXB MIPI CSI/DSI转EDP信号转换

爱可可AI前沿推介(8.10)
What are the five common data types of Redis?What is the corresponding data storage space?Take you to learn from scratch

【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...

Jenkins修改默认主目录

如何培养ui设计师的设计思维?

LeetCode中等题之颠倒字符串中的单词

LeetCode medium topic search of two-dimensional matrix

Guidelines for Sending Overseas Mail (2)
随机推荐
Guo Jingjing's personal chess teaching, the good guy is a robot
Excel function formulas - LOOKUP function
第5章 虚拟存储器
协程与任务
动态规划之最长回文子串
Codeforces Round #276 (Div. 1) D. Kindergarten
How to do foreign media publicity to grasp the key points
培训机构学习费用是多少呢?
Alibaba Cloud Jia Zhaohui: Cloud XR platform supports Bizhen Technology to present a virtual concert of national style sci-fi
Solution for "Certificate not valid for requested usage" after Digicert EV certificate signing
专有云ABC Stack,真正的实力派!
CV复习:空洞卷积
浮动及其特点
camshift achieves target tracking
tommy's spell
11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%
odps sql 不支持 unsupported feature CREATE TEMPORARY
Codeforces Round #276 (Div. 1) D. Kindergarten
LeetCode中等题之搜索二维矩阵
毕业总结