Harmonic Number (II) LightOJ - 1245
考察:整除分块
原来之前那道余数之和是这道模板题的运用= = GO
和余数之和一样的分块思想.
坑点在之前的素数距离已经遇到过,就是for循环i+1刚好溢出
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 typedef long long ll; 6 int main() 7 { 8 int T,kcase =0; 9 scanf("%d",&T); 10 while(T--) 11 { 12 ll ans = 0; 13 ll n,st,ed,t; scanf("%lld",&n); 14 for(ll i=1;i<=n;i=ed+1) 15 { 16 t = n/i; 17 st = n/(t+1)+1,ed = n/t; 18 ans += (ll)(ed-st+1)*t; 19 } 20 printf("Case %d: %lld\n",++kcase,ans); 21 } 22 return 0; 23 }