【题目】

【预备知识】

,其中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. laydate控件后台返回的时间前台格式化

    //功能:laydate控件后台返回的时间前台格式化 //参数:laydate控件值 function formatDate(strTime) { if ("" === strTi ...

  2. javase(5)_面向对象

    一.概述 1.面向对象是一种思想,让我们由执行者变成指挥者,执行者是面向过程,指挥者是面向对象.例如人开冰箱门,开冰箱门这个动作应该属于门而不是人,冰箱自己最清楚门应该怎么开,人只是调用了冰箱的这个动 ...

  3. 摘抄 Promise原理

    1.简单的promise: //极简promise雏形 function Promise(fn){ var value = null; callbacks = [];//callback为数组,因为可 ...

  4. 学习笔记(_huaji_)

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

  5. 使用Redis作为高速缓存

    Redis适合哪些业务场景常规业务系统的数据库访问中,读写操作的比例一般在7/3到9/1,也就是说读操作远多于写操作,因此高并发系统设计里,通过NoSQL技术将热点数据(短期内变动概率小的数据)放入内 ...

  6. day12-图

  7. iOS UITextView点击事件处理

    自定义一个UITextView UITextView 的selectedRange 影响 selectedTextRange 改变前者可影响后者 self.selectedRange -->se ...

  8. raywenderlich.com Objective-C编码规范

    原文链接 : The official raywenderlich.com Objective-C style guide 原文作者 : raywenderlich.com Team 译文出自 : r ...

  9. (原)剑指offer变态跳台阶

    变态跳台阶 时间限制:1秒空间限制:32768K 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法.   分析一下明天是个斐波那契 ...

  10. 剑指Offer(书):调整数组顺序使奇数位于偶数前面

    题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. public void ...