【题目】

【预备知识】

,其中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 调和级数求和的更多相关文章

  1. LightOJ1234 Harmonic Number

    /* LightOJ1234 Harmonic Number http://lightoj.com/login_main.php?url=volume_showproblem.php?problem= ...

  2. LightOJ1234 Harmonic Number —— 分区打表

    题目链接:https://vjudge.net/problem/LightOJ-1234 1234 - Harmonic Number    PDF (English) Statistics Foru ...

  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. LightOJ 1245 Harmonic Number (II)(找规律)

    http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS    ...

  7. 1245 - Harmonic Number (II)(规律题)

    1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...

  8. Harmonic Number(调和级数+欧拉常数)

    In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers ...

  9. Harmonic Number (调和级数+欧拉常数)题解

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

随机推荐

  1. How to debug add-ins for arcgis

    Debugging add-ins To debug an add-in, follow these steps: Confirm that the add-in is deployed to the ...

  2. poi导出word模板项目实例(一个文件)

    在页面上填写值,然后导出到word模板中,并把页面上的值带到模板中,也就是导出word文档,提前有word 的模板形式, 1.jsp 页面   <table class="formTa ...

  3. 四. python网络编程

    第八章.网络基础知识 1. TCP/IP协议介绍 1.TCP/IP概念 TCP/IP: Transmission Control Protocol/Internet Protocol的简写,中译名为传 ...

  4. UISearchBar的应用

    当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...

  5. LeetCode 字符串相乘

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...

  6. 学习笔记(_huaji_)

    假如我没有见过太阳,我也许会忍受黑暗. 如果我知道自己会在哪里死去,我就永远都不去那儿.失败的经历,其实也有它的价值. 人的过失会带来错误,但要制造真正的灾难还得用计算机. 嘴角微微上扬已不复当年轻狂 ...

  7. vuex其实超简单,只需3步

    前言 之前几个项目中,都多多少少碰到一些组件之间需要通信的地方,而因为种种原因,event bus 的成本反而比vuex还高, 所以技术选型上选用了 vuex, 但是不知道为什么,团队里的一些新人一听 ...

  8. Codeforces Round #877 (Div. 2) E. Danil and a Part-time Job

    E. Danil and a Part-time Job 题目链接:http://codeforces.com/contest/877/problem/E time limit per test2 s ...

  9. http过程

    当在浏览器里输入URL地址时,http的通讯过程: 1) 连接 DNS解析:URL——>DNS服务器(找到返回其ip,否则继续将DNS解析请求传给上级DNS服务器) Socket连接:通过IP和 ...

  10. 【SaltStack】在Master上给Minion端安装zabbix

    一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...