本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/16529718.html

题目地址

题目

image

思路

以下分数皆表示整除

max(nmodi)=max(nni×i)=n+max(ni×i)=nmin(ni×i)\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)

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

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 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;
}

总结

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