loj1245(数学)

求解HarmonicNumber(II)问题
本文介绍了一个高效算法解决HarmonicNumber(II)问题,即计算sum=n/1+n/2+n/3+...+n/n的值。通过跳过相同n/i值的区间来加速求解过程。

 

传送门: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

 

转载于:https://www.cnblogs.com/lienus/p/4298663.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值