1245-harmonic Number (II) (law problem)

Source: Internet
Author: User

1245-harmonic Number (II)
PDF (中文版) 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 is that I was using the the integer divisionsonly. However, you were given n, you had to find H (n) as in Mycode.

Input

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

Each case is 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:46,475,828,386

The solution, own ideas, but can not write out, read the answer, it is quite simple;

First look at two examples 1.N =   &NBSP;SQRT (Ten) = 3     10/SQRT (x) = 3i        1   2   3 &NB Sp       4   5   6   7   8   9   10n/i    10  5   3   & nbsp     2   2   1   1   1   1    1 m =  n/isum + = M;m = 1 Number 10/1-10/2 = 5;m = 2 the number of 10/2-10/3 = 2;m = 3 10/3-10/4 = 1;&NBSP;2.N =   &NBSP;&NBSP;SQRT (4     20/SQRT (20) = 5i        1   2   3   4       5   6   7   8   9 &nbs P               ( ), 20n/i   &NBSP;20 &NBSP;10 6   5       4   3   2   2   2    2     1   &nbsp ; 1     1     1     1     1     1     1    1   &N Bsp;1&nbsP;m =  n/isum + = M;m = 1 Number 20/1-20/2 = 10;m = 2 Number 20/2-20/3 = 4;m = 3 Number 20/3-20/4 = 1;m = 4 Number 20/4-20/5 = 1;...M = i Number 20/i-20/(i + 1) (1<= i <= sqrt (n))   So we can conclude that the number of sqrt (n) before we can directly use the For loop to find the sum + + + (sqrt-n/i (i + 1)) * I; When sqrt (n) = N/sqrt (n) (such as the first example 10,sum added a 3), Sum added a sqrt (n), minus the words, here output%i64d unexpectedly wa ... Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 #defineMem (x, y) memset (x,y,sizeof (x))7 using namespacestd;8typedefLong LongLL;9 Const intinf=0x3f3f3f3f;Ten  One intMain () { A     intT; -     intN,cnt=0; - LL Res; thescanf"%d",&T); -      while(t--){ -res=0; -scanf"%d",&n); +         intm=sqrt (n); -          for(intI=1; i<=m;i++) res+=n/i; +          for(intI=1; i<=m;i++) Ares+= (n/i-n/(i+1))*i; at             if(m==n/m) res-=m; -printf"Case %d:%lld\n",++cnt,res); -     } -     return 0; -}

1245-harmonic Number (II) (law problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.