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. 解决ecplise+phthon2.7中使用pytesser和tesseract进行ocr,出现报错的问题

    网上很多使用ecplise+phthon2.7中使用pytesser或者tesseract进行OCR网站验证码的案例,但配置起来实在让人崩溃. 通用步骤:1.下载了pytesser_v0.0.1: 2 ...

  2. 封装baseControl

    package com.huawei.base; import java.io.IOException;import java.io.OutputStream;import java.io.Print ...

  3. 201671010140. 2016-2017-2 《Java程序设计》java学习第十六周

    java学习第十六周-并发        本周,学习了Java中线程,并发的知识,在老师的带领下,进行了对知识的理解学习,以及对实验的运行讲解,对这一块内容掌握的还可以,在自主编程中,也能够完成.线, ...

  4. Android中Activity的四种启动方式

    谈到Activity的启动方式必须要说的是数据结构中的栈.栈是一种只能从一端进入存储数据的线性表,它以先进后出的原则存储数据,先进入的数据压入栈底,后进入的数据在栈顶.需要读取数据的时候就需要从顶部开 ...

  5. rocketmq--push消费过程

    Rocketmq消费分为push和pull两种方式,push为被动消费类型,pull为主动消费类型,push方式最终还是会从broker中pull消息.不同于pull的是,push首先要注册消费监听器 ...

  6. linux: 空指令(:)

    :指令 描述: 空命令,除了参数替换和重定向外不执行任何操作,总是保证退出码为0. eg1:创建文件(不需要调用其它程序,速度更快) :>/path/to/file 测试: 创建10000个不存 ...

  7. ubuntu tensorflow cpu Faster-RCNN配置参考

    https://blog.csdn.net/qq_36652619/article/details/85006559     (参考) https://blog.csdn.net/zcy0xy/art ...

  8. linux系统中的进程

    一.fork 在类unix系统中,我们所执行的任何程序,都是由父进程(parent process)所产生出来的一个子进程(child process),子进程在结束后,将返回到父进程去.此一现象被称 ...

  9. spring4-2-bean配置-6-使用外部属性文件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk0AAAFGCAIAAAD4tzxRAAAgAElEQVR4nO2d27HsOm+tOxWn4CeXAm ...

  10. nginx相关教程

    1.nginx简易的教程 http://www.cnblogs.com/jingmoxukong/p/5945200.html#%E8%B7%A8%E5%9F%9F%E8%A7%A3%E5%86%B3 ...