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 ...
随机推荐
- var、let、const声明变量的区别
let和var声明变量的区别:1.let所声明的变量只在let命令所在的代码块内有效.(块级作用域) for(let i=0;i<10;i++){ // ... } console.log(i) ...
- numpy中tile函数
tile函数位于python模块numpy.lib.shape_base中,他的功能是重复某个数组. 函数的形式是tile(A,reps) 函数参数说明中提到A和reps都是array_like的,什 ...
- RedHat7搭建KVM虚拟机
RedHat7搭建KVM虚拟机 1. 宿主机安装RedHat7.3系统 1.1选择语言 中文.简体中文(中国) 1.2安装位置 1.2.1自定义分区,选择LVM,将分区空间全部分配给根 1.2.2禁用 ...
- grub加密。
一.介绍 安全无小事 linux系统的安全分为很多方面,什么端口啊,什么网络啊,听着都特么烦,今天谈谈最简单明显的密码安全. 二.单用户模式 单用户模式个人觉得相当有用,可以用来修复系统,修改密码… ...
- leepcode作业解析 - 5-20
22.缺失数字 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: ...
- JavaScript正则表达式-字符
普通字符 大小写字母.数字.其他任何符号. 转义字符 转义字符 含义 \f 换页符 \n 换行符 \r 回车符 \t 制表符 \b 退格符 \o 空字符 \xnn 由十六进制数nn指定的ASCII码对 ...
- Spring,Mybatis,Springmvc框架整合项目(第一部分)
一.说在前面的话 本篇博文实现一个注册登录小项目,使用spring,mybatis,springmvc框架进行整合,我们创建的是一个maven工程,主要是方便jar包版本的管理.项目使用eclispe ...
- centos中python2替换为python3,并解决yum出错
这里采用安装python3.6版本. 安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel r ...
- python基础——15(加密、excel操作、ini文件操作、xml操作模块及数据格式分类)
一.加密模块 1.有解密的加密方式(base64) #base64加密 import base64 str_encrypt = input("输入要加密的字符串:\n") base ...
- C# 导出Excel的示例
Excel知识点. 一.添加引用和命名空间 添加Microsoft.Office.Interop.Excel引用,它的默认路径是C:\Program Files\Microsoft Visual S ...