Codeforces Round #276 (Div. 1) B. Maximum Value【数学、思维】

题目链接

题意

给出序列 a [ 1... n ] a[1...n] a[1...n],求最大的 a [ i ] % a [ j ] a[i]\%a[j] a[i]%a[j] a [ i ] > a [ j ] a[i]>a[j] a[i]>a[j]

题解

先标记一下 a [ 1... n ] a[1...n] a[1...n] 的存在,对于 a [ i ] a[i] a[i] 来说,找到 m a x ( a [ i ] % a [ j ] ) max(a[i]\%a[j]) max(a[i]%a[j]) 的方法就是枚举其倍数(枚举倍数的时间是 l o g ( m ) log(m) log(m)),在 ( k − 1 ) ∗ a [ i ] (k-1)*a[i] (k1)a[i] k ∗ a [ i ] k*a[i] ka[i] 之间找最大且存在的值

假如 在 a [ i ] = 5 , k = 3 a[i]=5,k=3 a[i]=5k=3时,即 [10,15) 这个范围,存在11, 13, 14,答案是范围内最大且存在的数——14

但是遍历这个范围,找每个数是否存在的话就变成了O(n),所以预处理一下,把每个空的的位置用上一个数补上,比如 11, 13, 14 就补成 11, 11, 13, 14 ,假如 a [ i ] = 7 , k = 2 a[i]=7, k=2 a[i]=7,k=2时,若存在的是 7, 9,11 就补成 7, 7, 9, 9, 11, 11, 11,这样 i ∗ j − 1 i*j-1 ij1这个位置就是最大且存在的数

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))
#define mid ((l+r)>>1)
#define mem(x,y) memset(x,y,sizeof x)

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
int vis[N],last[N];
#define endl '\n'
signed main(){
	//freopen("C:\\Users\\egoist\\Desktop\\in.txt","r",stdin);
	//freopen("C:\\Users\\egoist\\Desktop\\out.txt","w",stdout);
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n,ans=0; cin>>n;
	for(int i=1,x;i<=n;i++)cin>>x,vis[x]=1;
	for(int i=1;i<=N;i++)last[i]=vis[i]?i:last[i-1];
	for(int i=1;i<=N;i++)//不能枚举a[i],假如a[i]都很小就退化成n^2了
		for(int j=2;(j-1)*i<=N;j++)
			if(vis[i])ans=max(ans,last[i*j-1>N?N:i*j-1]%i);
	cout<<ans<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值