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 ...
随机推荐
- python 进程与线程 精要
程序与进程 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程. 程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本:进程是程序的一次 ...
- echarts图表自适应,容器宽度设置为百分比,但是图表显示不全,缩到一起
<div id="chartContainer" style="height:100%;width:100%;"></div> cha ...
- shell脚本明文密码隐藏且加密
将密码放到文件中去,比如/root/.pass.txtpassword=`</root/.pass.txt`还怕密码泄露的话,就把pass.txt权限设置下. chattr +i /root/. ...
- laydate日期插件弹出闪退和多次闪退问题解决
情况:点击第一个input 日期,可以正常选择日期,之后点击任何一个,都会闪一下然后消失,无法正常选择: 原因:lay-key的值的问题,需要循环重新为lay-key赋值 解决: <input ...
- 利用reduce方法,对数组中的json对象去重
数组中的json对象去重 var arr = [{ "name": "ZYTX", "age": "Y13xG_4wQnOWK1Q ...
- Java 获取当前路径的方法总结
Java 获取当前路径的方法总结 1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user. ...
- sublime的安装与使用
1.sublime简介 Sublime是一个代码编辑器,可以编写HTML,php,js,css等文件. Sublime是由程序员Jon Skinner于2008年1月份所开发出来,它最初被设计为一个具 ...
- 暑假集训test-8-29
今天瓜成一坨了. 瓜的说不出话来. 直接退役算了我. T1 傻逼题,但是我傻逼地敲了一个线段树合并,然后把空间炸了,只剩20分, 直接dfs维护子树大小,子树中最大最小值即可统计答案. //Achen ...
- Spring-Security (学习记录二)--修改为自己的登录页面
目录 1.修改spring-security.xml配置文件 2.增加login.jsp页面 3.重启项目即可看到效果 1.修改spring-security.xml配置文件 <!-- auto ...
- 《转》python基础下
转自http://www.cnblogs.com/BeginMan/archive/2013/04/12/3016323.html 一.数字 在看<Python 核心编程>的时候,我就有点 ...