G - Harmonic Number (II) LightOJ - 1245

Be a regular topic to find it. The number of enumeration before sqrt (n), the number i is the number of occurrences n / in / (i + 1), the contribution of the answer to (n / in / (i + 1)) * i.

For several sqrt behind, it can be obtained directly from the n / i, and must appear only once.

(Mathematics really profound ~ ~ ~ ~)

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(ll time){ 
    ll n;
    cin>>n;
    ll ans=0;
    ll c=sqrt(n);
    ll i;
    for(i=1;i<=c;i++){
        ans+=n/i;
        if((n/i)>(n/(i+(ll)1))) {
            ans+=((n/i-n/(i+(ll)1))*i); 
        }
    }
    i--;
    if(n/i==i) ans-=i;
    printf("Case %d: %lld\n",time,ans);
}
int main () {
    ll t;
    cin>>t;
    for(ll i=1;i<=t;i++) solve(i);    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Accepting/p/12600020.html