题目链接: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,这种处理方式完全偏向于空间。可见两种极端的处理方式都无法解决问题,而需在这两者之间作个权衡,人生也如此!

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e6+; double table[MAXN];
void init()
{
double s = ;
for(int i = ; i<=; i++)
{
s += 1.0/i;
if(i%==) table[i/] = s;
}
} int main()
{
init();
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
double s = table[n/];
for(int i = n/*+; i<=n; i++)
s += 1.0/i; printf("Case %d: %.10f\n", ++kase, s);
}
}

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. CentOS配置sshd

    用SSH来远程管理计算机,就不用到计算机实际地点来回跑了 环境:服务器:CentOS6.6,客户机win8.1 putty 配置服务器: 1.检查SSHD是否安装(默认情况下是系统自带的),使用命令 ...

  2. AC自动机(加强版)

    题目描述 有NN个由小写字母组成的模式串以及一个文本串TT.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串TT中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据. 每组数据 ...

  3. 【jar】JDK将单个的java文件打包为jar包,并引用到项目中使用【MD5加密】

    ==================================================================================================== ...

  4. Git安装及SSH Key管理之Windows篇

    一.安装环境 1.本机系统:Windows 10 Pro(64位)2.Git版本:Git-2.11.0-64-bit.exe(64位) 二.Git安装 去官网下载完后一路下一步完成安装,如下图:   ...

  5. 一个Netfilter nf_conntrack流表查找的优化-为conntrack添加一个per cpu cache

    独悲须要忍受.快乐须要分享对Linux协议栈多次perf的结果,我无法忍受conntrack的性能,然而它的功能是如此强大,以至于我无法对其割舍,我想自己实现一个高速流表.可是我不得不抛弃依赖于con ...

  6. 百科知识 scm文件如何打开

    用scplayer打开,目前有效的下载链接将是: http://download.csdn.net/download/kevingao/2686778  

  7. CSDN - 进程结束后new出的内存会回收吗?

    http://blog.csdn.net/stanjiang2010/article/details/5386647     关键词:内存回收  

  8. C# Select SelectMany 区别

    string[] text = { "Today is 2018-06-06", "weather is sunny", "I am happy&qu ...

  9. 【每日Scrum】第三天(4.13) TD学生助手Sprint1站立会议

    TD学生助手Sprint1站立会议(4.13) 任务看板 站立会议内容 组员 昨天 今天 困难 签到 刘铸辉 (组长) 昨天完成了课程的增删改查功能 今天早晨静姐调整了下界面和配色,下午和宝月兄一起做 ...

  10. lowB三人组代码示例

    冒泡排序:列表在内存重只存一份,所以不需要重复赋值 import random from timewrap import * #时间装饰器 # 初级版本 @cal_time def bubble_so ...