评论

收藏

[C++] (UVA - 11526) H(n)

编程语言 编程语言 发布于:2021-07-06 14:59 | 阅读数:248 | 评论:0

  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;
}
  

  
关注下面的标签,发现更多相似文章