LightOJ1234 Harmonic Number 调和级数求和
【题目】

【预备知识】
,其中r是欧拉常数,const double r= 0.57721566490153286060651209;
这个等式在n很大 的时候 比较精确。
【解法】可以在 n较小的时候,比如n<1e6时,直接用预处理的打表O(1)求值,在n比较 大的时候,运用以上公式,此时要减去 1/(2*n)加以修正。
#include<iostream>
#include<cmath>
using namespace std;
const double euler= 0.57721566490153286060651209;
const int maxn = 1e6;
double a[maxn];
int cas = ;
int main(){
long long n;
a[] = ;
for(int i=; i<maxn; i++){
a[i] = a[i-] + 1.0 / i;
}
int t;
cin>>t;
while(t--){
cin>>n;
if(n < maxn){
printf("Case %d: %.10lf\n",cas++,a[n]);
continue;
}
double ans = log(+n) + euler - 1.0/(*n);
printf("Case %d: %.10lf\n",cas++,ans);
}
return ;
}
【分块打表】
虽然1e8的表打不出来,但1e6的表很好打,所以每隔100个数记录一次前缀和。到时用的时候,O(1)取出最接近n的前缀和,余下不足100个数暴力 求和即可。
#include<iostream>
#include<cmath>
using namespace std;
const double euler= 0.57721566490153286060651209;
const int maxn = 1e8+; double a[maxn/];
int count = ; int cas = ;
int main(){
long long n;
a[] = ;
double s = ;
for(int i=; i<maxn; i++){
s += 1.0/i;
if( i % == ){
a[count++] = s;
}
}
int t;
cin>>t;
while(t--){
double ans = ;
cin>>n;
int num = n / ;//对应a[num]
ans += a[num];
for(long long i=num * + ; i<=n; i++){
ans += 1.0/i;
}
printf("Case %d: %.10lf\n", cas++, ans); }
return ;
}
LightOJ1234 Harmonic Number 调和级数求和的更多相关文章
- LightOJ1234 Harmonic Number
/* LightOJ1234 Harmonic Number http://lightoj.com/login_main.php?url=volume_showproblem.php?problem= ...
- LightOJ1234 Harmonic Number —— 分区打表
题目链接:https://vjudge.net/problem/LightOJ-1234 1234 - Harmonic Number PDF (English) Statistics Foru ...
- 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 ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- 1245 - Harmonic Number (II)(规律题)
1245 - Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...
- Harmonic Number(调和级数+欧拉常数)
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers ...
- Harmonic Number (调和级数+欧拉常数)题解
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
随机推荐
- Spring持久化之MyBatis
MyBatis是一个优秀的轻量级持久化框架,本文主要介绍MyBatis与Spring集成的配置与用法. 1. Spring MyBatis配置 1.1 添加Maven依赖 在pom.xml文件里添加m ...
- iOS开发遇到的坑之三--使用asi框架在xcode下正常运行,但是打包时却不能进行网络访问
前言: 前两篇博客遇到的问题是前几天在实验室开发的时候遇到的,花了两三天时间在上面,今天突然心血来潮,想把这些”坑”写下来,所以才有了这两篇写的很丑的博客随笔 今天在开发时又遇到一个问题,那就是标题所 ...
- tableview 删除cell
正如在以前的帖子说,但是我在转到故事版(StoryBoard)教程之前,我有另外一个问题来回答. 我如何从UITableView删除一行呢? 当人们构建简单的表视图引用程序后,这是另一个常见的问题 ...
- HDU-2018-奶牛的故事
这题找到递推式就好写了,递推式大致是: f=n (n<=4) f=f(n-1)+f(n-3) (n>4) 其实这题的题意,我觉得是有很大的问题的,它前后说的每年年初的意思都不一样,敬请参考 ...
- 【数位dp】bzoj1833: [ZJOI2010]count 数字计数
数位dp姿势一直很差啊:顺便庆祝一下1A Description 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. Input 输入文件中仅包含一行两个整数a ...
- docker系列之基础命令-1
1.docker基础命令 docker images 显示镜像列表 docker ps 显示容器列表 docker run IMAGE_ID 指定镜像, 运行一个容器 docker start/sto ...
- perl学习笔记之:正则表达式
Perl 中的正则表达式 正则表达式的三种形式 正则表达式中的常用模式 正则表达式的 8 大原则 正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过 ...
- 数据结构( Pyhon 语言描述 ) — —第9章:列表
概念 列表是一个线性的集合,允许用户在任意位置插入.删除.访问和替换元素 使用列表 基于索引的操作 基本操作 数组与列表的区别 数组是一种具体的数据结构,拥有基于单个的物理内存块的一种特定的,不变的实 ...
- python基础——13(系统、时间、序列化模块)
一.时间模块 1.标准库time %y 两位数的年份表示(00-99) %Y 四位数的年份表示(0000-9999) %m 月份(01-12) %d 月中的一天(0-31) %H 24小时制小时数(0 ...
- leetcode刷题——双指针
知识点 专题-B-双指针 题目: 题解: CS-Notes Algorithm_Interview_Notes-Chinese awesome-algorithm zcy19941015的博客