码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ - 1245

时间:2015-04-21 22:16:54      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

Status

Description

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 divisions only. However, you are given n, you have to find H(n) as in my code.

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) calculated by the code.

Sample Input

11

1

2

3

4

5

6

7

8

9

10

2147483647

Sample Output

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

Source

Problem Setter: Jane Alam Jan
/**
          题意:求n/1 + n/2 + n/3 + ........n/n
          做法:先求tt = sqrt(n); 比如 n = 10 
          sqrt(10) = 3,10/1 ~ 10/10 之间有 10/6 ~ 10/10 之间都是1 个数为(10/1 - 10/2)个
          之间为2的个数为 (10/4 ~ 10/5) 有(10/2 ~ 10/3) 个
          为3的个数有 10 /3 有(10/3) 个
          然后加判断 如果 n/(int)sqrt(n) == (int)sqrt(n)  则sum -= n/(int)sqrt(n)
**/
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
          freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
          int T;
          int Case = 1;
          scanf("%d",&T);
          while(T--)
          {
                    long long n;
                    scanf("%lld",&n);
                    long long res = 0;
                    int tt = (int)sqrt(n);
                    for(int i=1;i<=tt;i++)
                    {
                              res += (n/i);
                    }
                    for(int j=1;j<=tt;j++)
                    {
                              res += (n/j - n/(j+1)) * j;
                    }
                    if( n/(int)sqrt(n) == (int)sqrt(n))
                    res -= (n/(int)sqrt(n));
                    printf("Case %d: %lld\n",Case++,res);
          }
          return 0;
}

 

LightOJ - 1245

标签:

原文地址:http://www.cnblogs.com/chenyang920/p/4445544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!