LightOJ1234 Harmonic Number —— 分区打表
题目链接:https://vjudge.net/problem/LightOJ-1234
| 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 —— 分区打表的更多相关文章
- LightOJ1234 Harmonic Number
/* LightOJ1234 Harmonic Number http://lightoj.com/login_main.php?url=volume_showproblem.php?problem= ...
- LightOJ1234 Harmonic Number 调和级数求和
[题目] [预备知识] ,其中r是欧拉常数,const double r= 0.57721566490153286060651209; 这个等式在n很大 的时候 比较精确. [解法]可以在 n较小的时 ...
- 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 + . ...
- LightOJ 1234 Harmonic Number (打表)
Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submi ...
- LightOJ 1234 Harmonic Number(打表 + 技巧)
http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS Memory ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- C - Harmonic Number(调和级数+欧拉常数)
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers ...
- LightOJ 1234 Harmonic Number
D - Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
随机推荐
- codeforces A. Wrong Subtraction
A. Wrong Subtraction time limit per test 1 second memory limit per test 256 megabytes input standard ...
- vu 是什么
unsigned char 是无符号字符型 volatile 易变,易失的 volatile unsigned char i; 意思是定义一个无符号字符型的变量 i. 这个变量存放在内存中 ...
- window.open 打开子窗体,关闭全部的子窗体
需求:通过window.open方法打开了子窗体,当关闭主窗体时.子窗体应当也关闭. 实现思路: 1.打开子窗体函数window.open(url,winName)的第二个參数winName能够唯一标 ...
- mvc用UpdateModel报错
项目中使用UpdateModel时报错:未能更新类型“XXXXXX”的模型. 原因如下:表单Post时,有的参数为空,如a=1&b=2&=3.
- VueJS标签消息显示HTML:v-html
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- 【每日Scrum】第七天(4.28)Sprint2总结性会议
本次会议主要是演示了一下本组项目的各项功能,每个人负责那一块儿功能由本人来负责说明和演示,确定alpha版本的发布时间,并且分派了各组员的文档负责情况,上图是会议记录,下面我详细介绍一下我组分派情况: ...
- fuser - identify processes using files or sockets
FUSER(1) User Commands FUSER(1) NAME fuser - identify processes using files or sockets SYNOPSIS fuse ...
- JSTL简单介绍
1.JSTL简单介绍: JSTL(JSP Standard Tag Library.JSP标准标签库)是一个不断完好的开放源码的JSP标签库.其提供两组标签,一组使用 EL(Expression La ...
- Fakeapp2.2安装,使用简记
1,硬件和操作系统,支持cuda的Nvidia显卡,8G及以上的内存,Windows10 x64(推荐,Windows7 x64亲测可行),可以使用gpu-z查看你的显卡详情 我的笔记本是双显卡(都是 ...
- UVA 11888 - Abnormal 89's(Manachar)
UVA 11888 - Abnormal 89's option=com_onlinejudge&Itemid=8&page=show_problem&category=524 ...