http://lightoj.com/volume_showproblem.php?problem=1234

Harmonic Number

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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

In this problem, you are given n, you have to find Hn.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 108).

Output

For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.

Sample Input

12

1

2

3

4

5

6

7

8

9

90000000

99999999

100000000

Sample Output

Case 1: 1

Case 2: 1.5

Case 3: 1.8333333333

Case 4: 2.0833333333

Case 5: 2.2833333333

Case 6: 2.450

Case 7: 2.5928571429

Case 8: 2.7178571429

Case 9: 2.8289682540

Case 10: 18.8925358988

Case 11: 18.9978964039

Case 12: 18.9978964139

题目大意:

求1 + 1/2 + 1/3 + 1/4 + 1/ 5 +...+ 1/ n(1 ≤ n ≤ 108)

调和级数部分和,可以利用公式,(唉,然而我并不记得公式高数没学好-_-||)

如果直接循环的肯定会超时,那么我们开一个10^8/40 = 250万的数组用来分别存

1到1/40的和、1到1/80的和、1到1/120的和、1到1/160的和、... 、1到1/2500000的和

这样对于每一个n最多循环39次,节省了时间

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std;
const int N = ;
const int M = 1e8 + ;
typedef long long ll; double a[N]; int main()
{
int t, n, p = ;
double s = ;
for(int i = ; i < M ; i++)
{
s += (1.0 / i);
if(i % == )
a[i / ] = s;
}
scanf("%d", &t);
while(t--)
{
p++;
scanf("%d", &n);
int x = n / ;
s = a[x];
for(int i = x * + ; i <= n ; i++)
s += (1.0 / i);
printf("Case %d: %.10f\n", p, s);
}
return ;
}

LightOJ 1234 Harmonic Number(打表 + 技巧)的更多相关文章

  1. LightOJ 1234 Harmonic Number (打表)

    Harmonic Number Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submi ...

  2. LightOJ 1234 Harmonic Number

    D - Harmonic Number Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu S ...

  3. LightOJ 1234 Harmonic Number 调和级数部分和

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...

  4. LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)

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

  5. LightOJ 1245 Harmonic Number (II)(找规律)

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

  6. LightOJ - 1245 - Harmonic Number (II)(数学)

    链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...

  7. Harmonic Number LightOJ - 1234 (分段打表)

    题意: 求调和级数,但n很大啦.. 解析: 分段打表  每间隔50存储一个数,在计算时  只需要找到离输入的n最近的那个数 以它为起点 开始计算即可 emm...补充一下调和级数的运算公式   r为常 ...

  8. I - Harmonic Number LightOJ - 1234 (分段打表+暴力)

    题目给的时间限制是3s,所以可以直接暴力来做,注意n的取值范围是1e8,如果开一个1e8的数组会RE.分段打表,可以每100个数记录一次,然后对每次询问先找到它所在的区间,然后在暴力往后找.(学到了~ ...

  9. Light oj 1234 - Harmonic Number

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 给你一个数n,让你求 这个要是直接算的话肯定TLE,要是用1e8的数组预处理存储 ...

随机推荐

  1. 页面生成柱状图 --- D3.js

    转载自:https://www.cnblogs.com/fastmover/p/7779660.html D3.js从入门到"放弃"指南 前言 近期略有点诸事不顺,趁略有闲余之时, ...

  2. Linux 硬盘工具之hdparm

    安装 yum -y install hdparm 显示硬盘的相关设置 测试硬盘的读取速度 检测IDE硬盘的电源管理模式 [root@cnscn ~]# hdparm -C /dev/sda /dev/ ...

  3. mysql 如何清除sql缓存

    对一条sql进行优化时,发现原本很慢的一条sql(将近1分钟) 在第二次运行时, 瞬间就完成了(.00sec) 这是因为mysql对同一条sql进行了缓存,服务器直接从上次的查询结果缓存中读取数据,而 ...

  4. easyui隐藏列

    1.$("#test-datagrid").datagrid('hideColumn', 'password');其中第二个参数为对应的域,即field 2. <th dat ...

  5. Linux实战教学笔记52:GlusterFS分布式存储系统

    一,分布式文件系统理论基础 1.1 分布式文件系统出现 计算机通过文件系统管理,存储数据,而现在数据信息爆炸的时代中人们可以获取的数据成指数倍的增长,单纯通过增加硬盘个数来扩展计算机文件系统的存储容量 ...

  6. SQL select 执行顺序

    一.sql语句的执行步骤:1)语法分析,分析语句的语法是否符合规范,衡量语句中各表达式的意义.2)语义分析,检查语句中涉及的所有数据库对象是否存在,且用户有相应的权限.3)视图转换,将涉及视图的查询语 ...

  7. go_常量与枚举

    package main import ( "fmt" "math" ) //常量的数值可以作为各种类型使用 func consts(){ const file ...

  8. 图解Git

    转载自http://marklodato.github.io/visual-git-guide/index-zh-cn.html#diff 图解Git 其他语言: Deutsch English Es ...

  9. C++ std::unordered_map

    std::unordered_map template < class Key, // unordered_map::key_type class T, // unordered_map::ma ...

  10. Java 8特性

    1. Java8的新特性 1.1. Lambda表达式和函数式接口 最简单的Lambda表达式可以用逗号分隔的参数列表.->符号和功能语句块来表示.示例如下: Arrays.asList( &q ...