Lightoj 1234 Harmonic Number (play table + tricks)

Source: Internet
Author: User

http://lightoj.com/volume_showproblem.php?problem=1234

Harmonic number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%LLD &%llusubmit Status Practice Lightoj 1234

Description

In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:

In this problem, you is given n, you have to find Hn.

Input

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

Each case is starts with a line containing an integer n (1≤n≤108).

Output

For each case, print the case number and the nth harmonic number. Errors less than 10-8 'll be ignored.

Sample Input

12

1

2

3

4

5

6

7

8

9

90000000

99999999

100000000

Sample Output

Case 1:1

Case 2:1.5

Case 3:1.8333333333

Case 4:2.0833333333

Case 5:2.2833333333

Case 6:2.45

Case 7:2.5928571429

Case 8:2.7178571429

Case 9:2.828968254

Case 10:18.8925358988

Case 11:18.9978964039

Case 12:18.9978964139

Main topic:

Request 1 + 1/3 + + + + 1/5 +...+ 1/n(1≤n≤108)

The harmonic progression part and, can use the formula, (alas, however I do not remember the formula high number did not learn-_-| | )

If the direct loop is bound to time out, then we open an array of 10^8/40 = 2.5 million to separate the

1 to 1/40 and, 1 to 1/80 and, 1 to 1/120 and, 1 to 1/160, and 、... 、 1 to 1/2500000 and

This saves time by looping up to 39 times per N

#include <stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intN =2500010;Const intM = 1e8 +Ten; typedefLong Longll;DoubleA[n];intMain () {intT, N, p =0; Doubles =0;  for(inti =1; i < M; i++) {s+= (1.0/i); ifI +==0) A[i/ +] =s; } scanf ("%d", &t);  while(t--) {p++; scanf ("%d", &N); intx = N/ +; S=A[x];  for(inti = x * ++1; I <= N; i++) s+= (1.0/i); printf ("Case %d:%.10f\n", P, s); }    return 0;}

Lightoj 1234 Harmonic Number (play table + tricks)

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.