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 ...
随机推荐
- cmake 指定输出目录
$ mkdir ~/cpp-netlib-build $ cd ~/cpp-netlib-build $ cmake -DCMAKE_BUILD_TYPE=Debug \ > -DCMAKE_C ...
- off-by-one&doublefree. 看雪10月ctf2017 TSRC 第四题赛后学习
off-by-one 0x00 发现漏洞 1.off-by-one 在massage函数中,如图所示,可以修改的字节数比原内存大小多了一个字节 2.悬挂指针 可以看到,在free堆块的时候,没有清空指 ...
- MySQL数据库安全配置
文章来源:http://www.xfocus.net MySQL数据库安全配置 1.前言 MySQL 是完全网络化的跨平台关系型数据库系统,同时是具有客户机/服务器体系结构的分布式数据库管理系统.它具 ...
- jquery动态实现填充下拉框
当点下拉框时动态加载后台数据. 后台代码 protected void doPost(HttpServletRequest request, HttpServletResponse response) ...
- Xcode 6 创建 Objective-C category
1. Command + N 2. 选择 iOS - Source - Objective-C File 3.File Type 选择 Category,Class 填基于的类名,File填扩展的名
- tableview 删除cell
正如在以前的帖子说,但是我在转到故事版(StoryBoard)教程之前,我有另外一个问题来回答. 我如何从UITableView删除一行呢? 当人们构建简单的表视图引用程序后,这是另一个常见的问题 ...
- 拖拽大图轮播pc 移动兼容
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Spring入门Ioc、DI笔记
Spring是为解决企业应用程序开发复杂性而创建的一个Java开源框架.以下是本人学习过程中总结的一些笔记: 如何学习Spring - 掌握用法 - 深入理解 - 反复总结 - 再次深入理解和实践 s ...
- 简单jQuery图片自适应宽度插件jQuery.imgAutoSize.js
(function ($) { var loadImg = function (url, fn) { var img = new Image(); img. ...
- 蓝牙学习(4) -- L2CAP
L2CAP in protocol 首先看一下L2CAP在Bluetooth protocol architecture diagram中的位置: Features of L2CAP Logical ...