LightOJ-1234-Harmonic Number-调和级数+欧拉常数 / 直接打表
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-调和级数+欧拉常数 / 直接打表的更多相关文章
- LightOJ 1234 Harmonic Number 调和级数部分和
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...
- LightOJ 1234 Harmonic Number(打表 + 技巧)
http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS Memory ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- LightOJ 1234 Harmonic Number
D - Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
- LightOJ 1234 Harmonic Number (打表)
Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submi ...
- C - Harmonic Number(调和级数+欧拉常数)
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers ...
- LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- LightOJ - 1245 - Harmonic Number (II)(数学)
链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...
- Harmonic Number (LightOJ 1234)(调和级数 或者 区块储存答案)
题解:隔一段数字存一个答案,在查询时,只要找到距离n最近而且小于n的存答案值,再把剩余的暴力跑一遍就可以. #include <bits/stdc++.h> using namespace ...
随机推荐
- 阿里云HBase推出全新X-Pack服务 定义HBase云服务新标准
2018年12月13日,第八届中国云计算标准和应用大会在京召开,会上阿里云HBase宣布推出全新X-Pack服务,支持SQL.时序.时空.图.全文检索能力.复杂分析,从处理到分析全栈式数据库,客户开箱 ...
- nginx+keepalive 实现高可用负载均衡方案
转:http://ju.outofmemory.cn/entry/52165 主nginx负载均衡器:172.26.11.99 (通过keepalived配置了VIP:172.26.11.101供外 ...
- icmp隧道--icmpsh
本地:ubantu 目标主机:windows 在ubantu上关闭自带的icmp回应 sysctl -w net.ipv4.icmp_echo_ighore_all=1 ubantu上启动 pip i ...
- windows下装LINUX后,进不了系统
在网上找了一款叫"DisckGenius"的软件,运行后选“硬盘”/“重建主引导记录(MBR)”,然后重启,就正常了. 还有系统盘最好是FAT32格式的.
- jquery操作html元素之(添加元素)
添加新的 HTML 内容 我们将学习用于添加新内容的四个 jQuery 方法: append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在 ...
- spring配置hibernate的sessionFactory
1.首先通过dataSource来配置sessionFactory <!--读入配置文件 --> <bean id="propertyConfigurer" cl ...
- IK 用java 代码实现分词
需要导入IK 对应的jar 包 IKAnalyzer2012.jar lucene-core-4.10.jar public static void main(String[] args) throw ...
- n-map安装实操
强烈建议大家从官网下载nmap,而不是其他的第三方.官网地址:https://nmap.org/download.html 打开是这样的,感觉有点阴森森的色调.BTW,谁能逃得过真香定律呢. wind ...
- USACO 2008 January Silver Telephone Lines /// 二分最短路 邻接表dijkstra oj22924
题目大意: 一共有N (1 ≤ N ≤ 1,000)个电线杆,有P P (1 ≤ P ≤ 10,000)对电线杆是可以连接的, 用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电线杆 ...
- Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)
题意: 给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1].问划分的方案数. n<=1000000 题解: ...