题目链接:https://vjudge.net/problem/LightOJ-1234

1234 - Harmonic Number
Time Limit: 3 second(s) Memory Limit: 32 MB

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

Output for Sample Input

12

1

2

3

4

5

6

7

8

9

90000000

99999999

100000000

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

题意:

对于一个数n,输出 sigma(1/k),1<=k<=n。

题解:

1.一开始想离线做,结果发现输入完一个测试数据就必须输出,不能离线。

2. 由于n<=1e8,开一个1e8大小的数组是不可能的,那么可以尝试开一个1e6的数组,然后每隔100就存一个数据,分区打表。

3.假设能开1e8大小的数组,那么经过预处理后,那么查询只需O(1),这种处理方式是完全偏向于时间;如果不开数组,每次都重新计算,那么每次查询需O(n),而n可高达1e7,这种处理方式完全偏向于空间。可见两种极端的处理方式都无法解决问题,而需在这两者之间作个权衡,人生也如此!

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const int INF = 2e9;
  15. const LL LNF = 9e18;
  16. const int mod = 1e9+;
  17. const int MAXM = 1e5+;
  18. const int MAXN = 1e6+;
  19.  
  20. double table[MAXN];
  21. void init()
  22. {
  23. double s = ;
  24. for(int i = ; i<=; i++)
  25. {
  26. s += 1.0/i;
  27. if(i%==) table[i/] = s;
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. init();
  34. int T, n, kase = ;
  35. scanf("%d", &T);
  36. while(T--)
  37. {
  38. scanf("%d", &n);
  39. double s = table[n/];
  40. for(int i = n/*+; i<=n; i++)
  41. s += 1.0/i;
  42.  
  43. printf("Case %d: %.10f\n", ++kase, s);
  44. }
  45. }

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 调和级数求和

    [题目] [预备知识] ,其中r是欧拉常数,const double r= 0.57721566490153286060651209; 这个等式在n很大 的时候 比较精确. [解法]可以在 n较小的时 ...

  3. Harmonic Number 求Hn; Hn = 1 + 1/2 + 1/3 + ... + 1/n; (n<=1e8) T<=1e4; 精确到1e-8; 打表或者调和级数

    /** 题目:Harmonic Number 链接:https://vjudge.net/contest/154246#problem/I 题意:求Hn: Hn = 1 + 1/2 + 1/3 + . ...

  4. LightOJ 1234 Harmonic Number (打表)

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

  5. LightOJ 1234 Harmonic Number(打表 + 技巧)

    http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS     Memory ...

  6. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  7. C - Harmonic Number(调和级数+欧拉常数)

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

  8. LightOJ 1234 Harmonic Number

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

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

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

随机推荐

  1. Django简单粗暴快速发送邮件!

    >>尽管Python已经提供了相对易用的邮件发送模块 smtplib ,但Django仍对其做了轻度的封装.封装后的模块不仅发送邮件速度快,而且在开发环境下也很容易对邮件发送进行测试, 并 ...

  2. git移除上一次的commit中误添加的文件

    在使用git进行版本管理时,往往会出现一些误操作,比如将一些不加上传的文件放到了暂存区,即上传到了上一次commit中 比如: commit c134ab90ca7c4daf8bfa22e3ad706 ...

  3. 【java】spring项目中 对entity进行本类间的克隆

    方法1: [使用spring自带BeanUtils实现克隆] [要求:需要被克隆的类实现Cloneable接口并且重写clone()方法] >例子: >>实体: package co ...

  4. Scut游戏服务器免费开源框架--快速开发(1)

    Scut快速开发(1) 1        开发环境 需要安装的软件 a)        VS2010开发工具(.Net Framework 4.0以上) 2        HelloWorld 2.1 ...

  5. 怎样高速启动Android模拟器(Android Emulator)

    总所周知,每次我们启动Android Emulator,都须要花费非常长一段时间,几分钟甚至十几分钟.事实上,我们能够使用快照(Snapshot)功能,来高速启动Android模拟器. 首先.须要在A ...

  6. 用DD-WRT自建计费WiFi热点

    架设无线网络(Wlan)向周围的用户有偿共享网络. 传统的方法,能够使用专业的无线与宽带计费网关设备和软件.比方MikroTik等. 只是,对于个人架设WiFi热点来说.这些方案太过昂贵,安装设置也很 ...

  7. 第14章8节《MonkeyRunner源代码剖析》 HierarchyViewer实现原理-获取控件列表并建立控件树

    在上几节的描写叙述中,我们把HierarchyViewer初始化好.也把ViewServer给装备好了.那如今距离获得一个控件去操作它是万事具备仅仅欠东风了,欠了那一股春风了?欠了的是建立控件树这个东 ...

  8. 将txt文件数据存入excel表格

    前言 最近使用Appium自动化在测试设备配网的情况,需要记录每次成功与否和耗时时间. 由于App不是很稳定,执行一段时间会奔溃,因此数据只能通过追加的形式写入到txt文件. 实现过程 存储在txt文 ...

  9. 《UNIX 环境高级编程》编译环境的搭建( 运行本专栏代码必读 )

    第一步:搭建基本的编译环境 安装gcc, g++, bulid-essential等编译软件 第二步:下载本书示例源码包 可在这里下载 www.apuenook.com 第三步:解压下载到的包并放在用 ...

  10. 2014年辛星解读css第五节

    本小节我们解说css中的"盒模型".即"box model",它通经常使用于在布局的时候使用,这个"盒模型"也有人成为"框模型&q ...