解题报告:Lightoj Harmonic Number 打表
2016-07-20 09:33
417 查看
Harmonic Number
Time Limit:3000MS
Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit
Status
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
题意:
给定n,询问从1到n的倒数和。
思路:
T有1W,如果对于每个N(1e8)直接爆力肯定超时,如果考虑打表,题目给的内存是32M,全部打下来肯定会MLE,于是每隔100个数记录一个到表中,如果查询时从刚好小一点的数开始,每次查询复杂度o(1e2),整体复杂度o(1e8)+T*o(1e2),虽然前面的预处理复杂度高达o(1e8),但是每次只是进行一次除法和加法,还是能够接受的
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
double ans[1000001];
inline double work(int n){
int i = n/100;
double res = ans[i];
for(i=i*100+1;i<=n;i++){
res += 1.0/i;
}return res ;
}
int main()
{
int n=100000000;
ans[0]=0;double tmp=0;
for(int i=1;i<=n;i++){
tmp += 1.0/i;
if(i%100==0){
ans[(int)(i/100)]=tmp;
}
}
int T,t=0;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
printf("Case %d: %.9f\n",++t,work(n));
}return 0;
}
Time Limit:3000MS
Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit
Status
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
题意:
给定n,询问从1到n的倒数和。
思路:
T有1W,如果对于每个N(1e8)直接爆力肯定超时,如果考虑打表,题目给的内存是32M,全部打下来肯定会MLE,于是每隔100个数记录一个到表中,如果查询时从刚好小一点的数开始,每次查询复杂度o(1e2),整体复杂度o(1e8)+T*o(1e2),虽然前面的预处理复杂度高达o(1e8),但是每次只是进行一次除法和加法,还是能够接受的
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
double ans[1000001];
inline double work(int n){
int i = n/100;
double res = ans[i];
for(i=i*100+1;i<=n;i++){
res += 1.0/i;
}return res ;
}
int main()
{
int n=100000000;
ans[0]=0;double tmp=0;
for(int i=1;i<=n;i++){
tmp += 1.0/i;
if(i%100==0){
ans[(int)(i/100)]=tmp;
}
}
int T,t=0;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
printf("Case %d: %.9f\n",++t,work(n));
}return 0;
}
相关文章推荐
- paramiko error
- 从bootloader到用户程序执行的跳转
- 解决fcitx中文输入法出现两个输入框问题
- jasig CAS 实现单点登录 - java、php客户端登录实现
- HDU4825(字典树+贪心)
- 2.6.28内核以太网帧接收时的流量控制
- iOS 视频剪切、旋转,视频添加音频、添加水印,视频导出
- ubuntu14.04系统相关
- struts中上传文件配置
- 41 和为 s 的两个数字
- ICCV研讨会:实时SLAM的未来以及深度学习与SLAM的比较
- Android 设计模式 笔记 - 责任链模式
- JavaScript DOM 对象
- 单工半双工和全双工
- Codeforces 363 Div2
- tableview的复用问题
- Photoshop切图
- 141. Linked List Cycle--LeetCode Record
- 开源库实践-图片加载缩放控件
- Spark 源码解析 : DAGScheduler中的DAG划分与提交