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

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

Input

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

Each case 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 will 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.450

Case 7: 2.5928571429

Case 8: 2.7178571429

Case 9: 2.8289682540

Case 10: 18.8925358988

Case 11: 18.9978964039

Case 12: 18.9978964139

解法一:

调和级数(即f(n))至今没有一个完全正确的公式,但欧拉给出过一个近似公式

n很大时:

          f(n)≈ln(n)+C+1(2*n)

          欧拉常数值:C≈0.57721566490153286060651209

          c++ math库中,log即为ln。

当n很小时:

      直接求,此时公式不是很准。

 #include<stdio.h>
#include<cmath>
#include<string.h>
typedef long long ll;
using namespace std; const double c=0.57721566490153286060651209;
//f(n)≈ln(n)+C+1/(2*n) double sum[]; void fn()
{
sum[]=1.0;
for(int i=; i<=; i++)
{
sum[i]=sum[i-]+1.0/(i*1.0);
}
}
int main()
{
fn();
int t;
scanf("%d",&t);
int n,tt=;
while(t--)
{
scanf("%d",&n);
if(n<=)
printf("Case %d: %.10lf\n",tt++,sum[n]);
else
{
double x=log(n)+c+1.0/(2.0*n);
printf("Case %d: %.10f\n",tt++,x);
}
}
return ;
}

解法二:

如果公式记不住可以选择打表,10e8全打表必定MLE,而每40个数记录一个结果,即分别记录1/40,1/80,1/120,...,1/10e8,这样对于输入的每个n,最多只需执行39次运算,大大节省了时间,空间上也够了。(可以学习一下这种每40个数记录一个结果的思想,免去了执行很大运算量的操作。)

 #include<stdio.h>
#include<cmath>
#include<string.h>
typedef long long ll;
using namespace std; double a[]; void fn()
{
double sum=0.0;
for(int i=; i<; i++)
{
sum+=1.0/i;
if(i%==)
{
a[i/]=sum;
}
}
}
int main()
{
fn();
int t;
scanf("%d",&t);
int n,tt=;
while(t--)
{
scanf("%d",&n);
double ans=a[n/];
for(int i=*(n/)+; i<=n; i++)
{
ans+=1.0/i;
}
printf("Case %d: %.10f\n",tt++,ans);
}
return ;
}

参考博客:https://www.cnblogs.com/shentr/p/5296462.html

LightOJ-1234-Harmonic Number-调和级数+欧拉常数 / 直接打表的更多相关文章

  1. LightOJ 1234 Harmonic Number 调和级数部分和

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...

  2. LightOJ 1234 Harmonic Number(打表 + 技巧)

    http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS     Memory ...

  3. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  4. LightOJ 1234 Harmonic Number

    D - Harmonic Number Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu S ...

  5. LightOJ 1234 Harmonic Number (打表)

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

  6. C - Harmonic Number(调和级数+欧拉常数)

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

  7. LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

  8. LightOJ 1245 Harmonic Number (II)(找规律)

    http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS    ...

  9. LightOJ - 1245 - Harmonic Number (II)(数学)

    链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...

  10. Harmonic Number (LightOJ 1234)(调和级数 或者 区块储存答案)

    题解:隔一段数字存一个答案,在查询时,只要找到距离n最近而且小于n的存答案值,再把剩余的暴力跑一遍就可以. #include <bits/stdc++.h> using namespace ...

随机推荐

  1. BCZM : 1.16

    24点游戏 解法一:穷举法 解法二:分治法

  2. 「题解」:x

    问题 A: x 时间限制: 1 Sec  内存限制: 256 MB 题面 题面谢绝公开. 题解 赛时想到了正解并且对拍了很久.对拍没挂,但是评测姬表示我w0了……一脸懵逼. 不难证明,如果对于两个数字 ...

  3. Java中的线程Thread方法之---interrupt()

    前几篇都介绍了Thread中的几个方法,相信大家都发现一个相似点,那就是sleep,join,wait这样的阻塞方法都必须捕获一个InterruptedException异常,顾名思义就是一个线程中断 ...

  4. TopCoder[SRM587 DIV 1]:ThreeColorability(900)

    Problem Statement      There is a H times W rectangle divided into unit cells. The rows of cells are ...

  5. bzoj 2257 (JSOI 2009) 瓶子与燃料

    Description jyy就一直想着尽快回地球,可惜他飞船的燃料不够了. 有一天他又去向火星人要燃料,这次火星人答应了,要jyy用飞船上的瓶子来换.jyy 的飞船上共有 N个瓶子(1<=N& ...

  6. js的线程和同步异步以及console.log机制

    项目上线了,闲下来就写写东西吧.积累了好多东西都没有做笔记~挑几个印象深刻的记录一下吧. js的同步异步以及单线程问题: 都知道单线程是js的一大特性.但是通常io(ajax获取服务器数据).用户/浏 ...

  7. bagging和boosting以及rand-forest

    bagging: 让该学习算法训练多轮,每轮的训练集由从初始的训练集中随机取出的n个训练样本组成,某个初始训练样本在某轮训练集中可以出现多次或根本不出现,训练之后可得到一个预测函数序列h_1,⋯ ⋯h ...

  8. 2014 0416 word清楚项目黑点 输入矩阵 普通继承和虚继承 函数指针实现多态 强弱类型语言

    1.word 如何清除项目黑点 选中文字区域,选择开始->样式->全部清除 2.公式编辑器输入矩阵 先输入方括号,接着选择格式->中间对齐,然后点下面红色框里的东西,组后输入数据   ...

  9. Hadoop搭建,上传文件时出现错误,没有到主机的路由

    解决方案:(1)从namenode主机ping其它slaves节点的主机名(注意是slaves节点的主机名),如果ping不通,原因可能是namenode节点的/etc/hosts 未配置主机名与IP ...

  10. Python3数据科学入门与实践✍✍✍

    Python3数据科学入门与实践  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候可 ...