loj1245(数学)

 

传送门:Harmonic Number (II)

题意:求sum=n/1+n/2+n/3+...+n/n。(n<2^31)

分析:在一定的区间内n/i的值是一定的,因此要跳过这段区间来加速求解。

#pragma comment(linker,"/STACK:1024000000,1024000000")

#include <cstdio>

#include <cstring>

#include <string>

#include <cmath>

#include <limits.h>

#include <iostream>

#include <algorithm>

#include <queue>

#include <cstdlib>

#include <stack>

#include <vector>

#include <set>

#include <map>

#define LL long long

#define mod 100000000

#define inf 0x3f3f3f3f

#define eps 1e-6

#define N 10000000

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

#define PII pair<int,int>

using namespace std;

inline LL read()

{

    char ch=getchar();LL x=0,f=1;

    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}

    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}

    return x*f;

}

int main()

{

    int n;

    int T,cas=1;

    T=read();

    while(T--)

    {

       n=read();

       LL ans=0;

       for(LL i=1,last=0;i<=n;i=last+1)

       {

           last=n/(n/i);

           ans+=(last-i+1)*(n/i);

       }

       printf("Case %d: %lld\n",cas++,ans);

    }

}
View Code

 

你可能感兴趣的:(数学)