以下分数皆表示整除
\[\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\) 求出来,然后数列分块取最小值即可
// 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; }
只要出现一个数反复除或模很多数,基本上就是数论分块了