LightOJ 1245 - Harmonic Number (II) (求n/1+n/2+n/2+....+n/n)

1245 - Harmonic Number (II)
  PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 32 MB

I was trying to solve problem '1234 - Harmonic Number',I wrote the following code

long long H( int n ) {
   
 long long res = 0;
   
 for( int i = 1; i <= n; i++ )
        res
 = res + n / i;
   
 return res;
}

Yes, my error was that I was using the integer divisionsonly. However, you are given n, you have to find H(n) as in mycode.

Input

Input starts with an integer T (≤ 1000),denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤n < 231).

Output

For each case, print the case number and H(n) calculatedby the code.

Sample Input

Output for Sample Input

11

1

2

3

4

5

6

7

8

9

10

2147483647

Case 1: 1

Case 2: 3

Case 3: 5

Case 4: 8

Case 5: 10

Case 6: 14

Case 7: 16

Case 8: 20

Case 9: 23

Case 10: 27

Case 11: 46475828386

 









ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 10000010
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int main()
{
	LL t,cas=0;
	LL i;
	scanf("%lld",&t);
	while(t--)
	{
	    LL n;
		scanf("%lld",&n);
		LL ans=0;
		LL num=sqrt(n);
		for(i=1;i<=num;i++)
		ans+=n/i+(n/i-n/(i+1))*i;
		//printf("num=%lld\n",num);
		if(n/num==num)
		ans-=n/num;
		printf("Case %lld: ",++cas); 
		printf("%lld\n",ans);
	}
	return 0;
}


你可能感兴趣的:(LightOJ 1245 - Harmonic Number (II) (求n/1+n/2+n/2+....+n/n))