UVALive

给定$n,k$$(1leqslant n,kleqslant 10^9)$,计算$sumlimits _{i=1}^nk: mod:i$

通过观察易发现$k\%i=k-left lfloor frac{k}{i} ight floor*i$,因此我们考虑把$left lfloor frac{k}{i} ight floor$的值相同的$i$分成一组直接求和,复杂度为$O(sqrt{n})$。

整除分块原理(选自某dalao博客)

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 typedef long long ll;
 5 ll n,k;
 6 
 7 int main() {
 8     while(scanf("%lld%lld",&n,&k)==2) {
 9         ll ans=0;
10         for(ll l=1,r; l<=n; l=r+1) {
11             ll t=k/l;
12             r=t&&k/t<n?k/t:n;
13             ans+=k*(r-l+1)-t*((l+r)*(r-l+1)/2);
14         }
15         printf("%lld
",ans);
16     }
17     return 0;
18 }
原文地址:https://www.cnblogs.com/asdfsag/p/10356175.html