(UVA - 11526) H(n)
What is the value this simple C++ function will return?long long H(int n){
long long res = 0;
for( int i = 1; i <= n; i=i+1 ){
res = (res + n/i);
}
return res;
}
Input
The first line of input is an integer T (T ≤ 1000) that indicates the number of test cases. Each of the
next T line will contain a single signed 32 bit integer n.
Output
For each test case, output will be a single line containing H(n).
Sample Input
2
5
10
Sample Output
10
27
分析:列几项观察
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
const int INF=0x3f3f3f3f;
const int N=105;
typedef long long LL;
LL H(LL n)
{
LL ans=0,tmp=sqrt(n);
for(LL i=1;i<=tmp;i++)
ans+=(n/i);
return 2*ans-tmp*tmp;
}
int main()
{
LL t,n;
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
printf("%lld\n",H(n));
}
return 0;
}
文档来源:51CTO技术博客https://blog.51cto.com/u_13696685/2989977
页:
[1]