您的位置:首页 > 其它

UVA_11526 H(n)

2008-10-24 14:06 253 查看
思路就是找到n/i结果相同的,跳过并累加
注意的是数据十分变态,居然还有n<0的数。。。无语。。。数据量也是大的吓人....

ProblemG[/b]

H(n)[/b]

Input:[/b]StandardInput

Output:[/b]StandardOutput

WhatisthevaluethissimpleC++functionwill
return?

longlongH(intn){

longlong
res=0;


for(inti
=1;i<=n;i=i+1){


res=
(res+n/i);


}

return
res;


}

Input

ThefirstlineofinputisanintegerT(T<=1000)
thatindicatesthenumberoftestcases.EachofthenextTlinewillcontaina
singlesigned32bitintegern.

Output

Foreachtestcase,outputwillbeasinglelinecontainingH(n).

SampleInputOutputforSampleInput

2
5
10
10
27

Problem-setter:Md.ArifuzzamanArif

SpecialThanks:ShahriarManzoor

#include<iostream>
#include<cmath>
usingnamespacestd;
longlongH(longlongn){

if(n<=0)return0;
longlongres=n;
longlongi,pre=n,tmp;
for(i=2;i<=n;i++)
{
res+=(tmp=n/i);
if(tmp!=pre)
{
res+=(n/tmp-i)*(tmp);
i=n/tmp;
pre=tmp;
}

}
returnres;
}
intmain()
{
longlongn,t;
cin>>t;
while(t--)cin>>n,cout<<H(n)<<endl;
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: