余数求和

余数求和

 

 题目分析:

(Gleft ( n,m ight ) = sum_{i=1}^{n} k mod i = sum_{i=1}^{n}k-left lfloor k/i ight floor*i = n*k-sum_{i=1}^{n}left lfloor k/i ight floor*i)

 AC_Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 typedef long long ll;
 5 using namespace std;
 6 
 7 ll n,k;
 8 int main()
 9 {
10     scanf("%lld%lld",&n,&k);
11     ll ans=n*k;
12 
13     for(ll l=1,r;l<=n;l=r+1){
14         if( k/l!=0 ) r=min(k/(k/l),n);
15         else r=n;
16         ans -= (k/l)*(r-l+1)*(l+r)/2;
17     }
18 
19     printf("%lld
",ans);
20     return 0;
21 }
原文地址:https://www.cnblogs.com/wsy107316/p/12650577.html