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. 修改Tomcat的端口号方法

      (1).查找conf路径下的server.xml文件,路径如: I: \tomcat6\apache-tomcat-6.0.32\conf\server.xml (2).打开server.xml文 ...

  2. vue 学习四 了解组件

    1组件的注册 全局注册 import Vue from 'vue'; import com from './component1'; Vue.component("com_name" ...

  3. delphi 获得系统目录

    利用Api函数,现在我介绍两个Api函数,利用他们就可以轻松简单的获取这些特殊系统目录. Function SHGetSpecialFolderLocation(hwndOwner: HWND; nF ...

  4. NX二次开发-UFUN设置环境变量UF_set_variable

    NX9+VS2012 #include <uf.h> #include <stdio.h> UF_initialize(); //UFUN方式 //设置环境变量 int a = ...

  5. flutter 按钮单选封装

    数字是自己先写死的 List list =[ { "title": "影视特效", , }, { "title": "室内设计&q ...

  6. faster-rcnn算法总结

    faster-rcnn的整体流程比较复杂,尤其是数据的预处理部分,流程比较繁琐.我写faster-rcnn系列文章的目的是对该算法的原始版本有个整体的把握,如果需要使用该算法做一些具体的任务,推荐使用 ...

  7. (转)HashSet<T>类

    转载于:http://www.importnew.com/6931.html HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复 ...

  8. linux mysql 远程访问权限问题

    1.为了让访问mysql的客户端的用户有访问权限,我们可以通过如下方式为用户进行授权:mysql> grant all on *.* to user_name@'%' identified by ...

  9. 将excel表格或csv转换为Shapefile文件

    读取csv转为shp 构造读取csv函数 def read_csv(fp): ret = [] with open(fp, 'rb') as f: for line in f: ret.append( ...

  10. python 通过zabbix api获得所有主机的ip

    #!/usr/bin/env python3 #coding=utf-8 import jsonimport requests#from urllib import requests, parse,e ...