【牛客网235422 区间最大值】题解

2022/7/28 23:30:35

本文主要是介绍【牛客网235422 区间最大值】题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目地址

题目

image

思路

以下分数皆表示整除

\[\Large\max(n\bmod i)\\\Large=\max(n-\frac n i\times i)\\\Large=n+\max(-\frac n i\times i)\\\Large=n-\min(\frac n i \times i) \]

显然,当 \(\frac n i\) 一定时,\(i\) 越小越好,所以可以把每个 \(\frac n i\) 求出来,然后数列分块取最小值即可

Code

// Problem: 区间最大值
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/30896/A
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
int n, m, i, j, k, T; 
int l, r, ans; 

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	n=read(); T=read(); 
	while(T--)
	{
		l=read(); r=read(); ans=1e9; 
		for(i=l; i<=r; i=(n/(n/i))+1) 
			ans=min(ans, n/i*i); 
		printf("%lld\n", n-ans); 
	}
	return 0; 
}

总结

只要出现一个数反复除或模很多数,基本上就是数论分块了



这篇关于【牛客网235422 区间最大值】题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程