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. RocketMQ初探(四)之RocketMQ4.x版本可视化管理控制台rocketmq-console-ng搭建(Apache)

    之前有部署过3.2.6为AliBaba版本的Web监控平台(可参考之前博客 https://www.cnblogs.com/buyige/p/9395453.html),现用RocketMQ4.2.0 ...

  2. springMVC环境搭建(1)

    工作一年以来,写的都是.net,最近比较闲,想把之前学过的java相关的东西捡起来,也学点新的东西.以前做过SSH架构,一直好奇spring mvc是怎么样的,所以今天试试看. 总体的代码结构 手动输 ...

  3. Unity协程使用经验

    [Unity协程使用经验] 1.协程的好处是,异步操作发起的地方和结束的地方可以统一在一个方法,这样就不用引入额外的成员变量来进行状态同步. 2.在一个协程中,StartCoroutine()和 yi ...

  4. AssetBundle依赖

    [Managing asset dependencies] 一个Asset会依赖其它Asset.可以把一个Asset所依赖的Asset也打包进自己的AssetBundle.可是多个Asset可能依赖同 ...

  5. Simple Cubemap Reflection

    [Simple Cubemap Reflection] Cubemap加在MainTex上,所以Property需要按如下定义: 注意_Cubemap的类型是CUBE. 使用Cubemap,需要计算反 ...

  6. 依靠反射来个Dbutils

    闲来无事,写个dbutils玩玩,不完善,满足基本增删改查,上代码 1.Dbutils package db; import annotation.Table; import java.util.*; ...

  7. SVN的“Invalid authz configuration”错误的解决方法

    公司有人离职后,我把他svn账号删除 然后就报这个错了,我检查了authz文件,完全看不出什么错误.... 网上的各种方法试一遍,无果. 蹲个厕所,继续查这个问题 看到一个答案: 给不存在的组配置权限 ...

  8. Dell 1420N使用Kubuntu默认无线驱动后网络不稳定的解决方法

    前几天在我的Dell 1420N上安装了Kubuntu 13.04,装了系统软件中的私有无线网卡驱动Broadcom STA wireless driver后,虽然能上网,但是很不稳定,经常断线,非常 ...

  9. DBArtist之Oracle入门第3步: 安装配置PL/SQL Developer

    操作系统:            WINDOWS 7 (64位) 数据库:               Oracle 11gR2 (64位) PL/SQL Developer :    PL/SQL ...

  10. 第一个Django应用程序_part1

    一.查看Django是否安装 参考文档:https://docs.djangoproject.com/en/1.11/intro/tutorial01/ 如果Django已经安装,可以看到安装的版本号 ...