LightOJ 1234 Harmonic Number(打表 + 技巧)
http://lightoj.com/volume_showproblem.php?problem=1234
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(打表 + 技巧)的更多相关文章
- LightOJ 1234 Harmonic Number (打表)
Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submi ...
- LightOJ 1234 Harmonic Number
D - Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
- LightOJ 1234 Harmonic Number 调和级数部分和
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...
- LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- LightOJ - 1245 - Harmonic Number (II)(数学)
链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...
- Harmonic Number LightOJ - 1234 (分段打表)
题意: 求调和级数,但n很大啦.. 解析: 分段打表 每间隔50存储一个数,在计算时 只需要找到离输入的n最近的那个数 以它为起点 开始计算即可 emm...补充一下调和级数的运算公式 r为常 ...
- I - Harmonic Number LightOJ - 1234 (分段打表+暴力)
题目给的时间限制是3s,所以可以直接暴力来做,注意n的取值范围是1e8,如果开一个1e8的数组会RE.分段打表,可以每100个数记录一次,然后对每次询问先找到它所在的区间,然后在暴力往后找.(学到了~ ...
- Light oj 1234 - Harmonic Number
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 给你一个数n,让你求 这个要是直接算的话肯定TLE,要是用1e8的数组预处理存储 ...
随机推荐
- linux shell find
find / -ctime +365 ! -name "*.bat" -exce rm -rf {} \;
- addClass()使用方法
含义:为元素添加指定的样式名 具体用法分为以下两种: 1.可以一次添加一个或者多个用空格隔开的样式名 例: <script> $("ul li").addClass(& ...
- java访问共享文件夹
由于工作需要读取局域网中一台机器的共享目录中的文件,需要jcifs-1.1.11.jar的支持,使用SMB协议,以下是实现了远程读取文件的功能: package junit; import jcifs ...
- flask 数据迁移
python flasky.py shell db.create_all() from app.models import User mhc = User("mhc") >& ...
- c# vs c++
[c# vs c++] 1.在 C++ 中,类和结构实际上是相同的,而在 C# 中,它们很不一样.C# 类可以实现任意数量的接口,但只能从一个基类继承.而且,C# Struct不支持继承,也不支持显式 ...
- linux fg&bg
[linux fg&bg] Linux 提供了 fg 和 bg 命令,让我们调度正在运行的任务. 假设你发现前台运行的一个程序需要很长的时间,但是需要干其他的事情,你就可以用 Ctrl-Z , ...
- 【2018沈阳赛区网络预选赛J题】Fantastic Graph 【有上下界的网络流】
要补的题太多了导致最近没写博客(好吧是我懒) 题目链接https://nanti.jisuanke.com/t/31447 题意 给出一个二分图,问能否挑选出一些边,使得每个点的度数都在[L,R]这个 ...
- 上传工程到github
这里主要讲讲如何在mac底下使用github,我刚开始使用时,还是费了一点功夫的,因为网上的资料比较杂,有些不是太准确.故将自己的安装过程比较详细的分享下,方便有需要的人,攒点人品. 首先你得完成如下 ...
- pcl知识
1.pcl/io/pcd_io.h pcl/io/ply_io.h pcl::PLYReader reader; pcl::PCDWriter pcdwriter; 读取ply pcd 2.voidl ...
- TensorFlow全新的数据读取方式:Dataset API入门教程
TensorFlow.data : http://tech.ifeng.com/a/20171109/44752505_0.shtml Pytorch:https://ptorch.com/docs/ ...