LightOJ 1234 Harmonic Number 调和级数部分和
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234

Sample Input Sample Output
Case :
Case : 1.5
Case : 1.8333333333
Case : 2.0833333333
Case : 2.2833333333
Case : 2.450
Case : 2.5928571429
Case : 2.7178571429
Case : 2.8289682540
Case : 18.8925358988
Case : 18.9978964039
Case : 18.9978964139
分析:这个是高数的东西 发散 n足够大时它无穷大 直接公式解。
1.虽然输出五花八门,但是不用管它,精度能保证在10 ^-8就没问题,直接用%.10lf 即可;
2. n的范围是10^8,肯定不能正常跑,但是我们有公式,不怕,前面10000个可以正常打表,后面的我们就用公式,再说了,这个公式能成立,本来就是在n比较大的时候,公式如下: r为常数,r=0.57721566490153286060651209(r就是欧拉常数)。
特别注意,由于题目要求精度为10^-8,常数r也是前人推导出来的,然而也只推导了有限位数,所以正常利用这个公式,并不能达到精度要求,我们只好比较样例和我们自己输出的数据,增添一些可行的项,经尝试,在原公式的基础上,再减去一个1.0/(2*n)恰好可以满足精度,也算是投机取巧了。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<cmath>
#include<iostream> using namespace std;
typedef long long LL; #define INF 0x3f3f3f3f
#define N 12000
#define mod 1000000007
#define R 0.57721566490153286060651209///R就是欧拉常数 double a[N]; int main()
{
int T,k,i,x;
double sum=0.0; a[]=;
for(i=;i<=;i++)
a[i]=a[i-]+1.0/i; scanf("%d", &T); k=;
while(T--)
{
printf("Case %d: ", k++);
scanf("%d", &x);
if(x<=)
printf("%.10f\n", a[x]);
else
{
sum=log(x+)+R-1.0/(*x);
printf("%.10f\n", sum);
}
}
return ;
}
/*sum=log(n+1)+R-1.0/(2*n);*/
有大神说:可以思维逆转,让我们用另一种思路再去看这个题目。
如果只靠打表,而且是有技术含量的打表,也能很好的解决这个问题,既然10^8的表我们打不出来,但是200万的表我们还是能打的,这样一来,我们先平均分而且间隔着,每50个记录一个,先把分布在10^8数据中的值放在表里,真正计算时,我们便先找到距离我们的n最近的且小于n的在表中存过数据的一个数,然后再在这个数的基础上递推这往下算,这样一来,便大大降低了时间复杂度,太啰嗦了,还是看代码吧!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<cmath>
#include<iostream> using namespace std;
typedef long long LL; #define INF 0x3f3f3f3f
#define N 2100000
#define MAXN 100000000
#define mod 1000000007
#define R 0.57721566490153286060651209///R就是欧拉常数 double a[N]; int main()
{
int T,k,i,x,c;
double sum=0.0; a[]=;
c=;
for(i=;i<=MAXN;i++)
{
sum+=1.0/i;
if(i%==)
a[c++]=sum;
} scanf("%d", &T); k=;
while(T--)
{ scanf("%d", &x); int num=x/;
double s;
s=a[num];
for(i=*num+;i<=x;i++)
s+=1.0/i;
printf("Case %d: %.10f\n", k++, s);
}
return ;
}
/*sum=log(n+1)+R-1.0/(2*n);*/
LightOJ 1234 Harmonic Number 调和级数部分和的更多相关文章
- LightOJ 1234 Harmonic Number(打表 + 技巧)
http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS Memory ...
- LightOJ 1234 Harmonic Number
D - Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
- LightOJ 1234 Harmonic Number (打表)
Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submi ...
- 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最近而且小于n的存答案值,再把剩余的暴力跑一遍就可以. #include <bits/stdc++.h> using namespace ...
- 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 ...
随机推荐
- 第三次冲刺spring会议(第五次会议)
[例会时间]2014/5/24 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇
- REST认识
大家对REST的认识? 谈到REST大家的第一印象就是通过http协议的GET,POST,DELETE,PUT方法实现对url资源的CRUD(创建.读取.更新和删除)操作.比如http://www.a ...
- AFNetworking 关于JSON text did not start with array or object and option to allow fragments not set 错误
AFHTTPSessionManager *manager =[AFHTTPSessionManager manager]; [manager GET:@"http://www.baidu. ...
- PHP ServerPush (推送) 技术
用来代替ajax的请求 转自:http://blog.163.com/bailin_li/blog/static/17449017920124811524364/ 需求: 我想做个会员站内通知的功能. ...
- mysql多表链接查询
select area from areas where cityid=(select cityid from cities where city like '杭州%') and id!=(selec ...
- npm常用指令
安装: npm install <name> npm install <name> 安装依赖包,默认安装最新版本,也可在后面加上版本号,并且将安装信息加入项目的package. ...
- HDU - 1045 Fire Net(二分匹配)
Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...
- eclipse 中执行 main 函数如何添加参数
我们通常执行 main 函数都是直接在类界面 右键 选择 Run As --> Java Application 但是如何 执行时带有参数呢? 右键 --> Run As --> R ...
- iOS不可变数组的所有操作
#pragma mark 创建数组 //1.通过对象方法创建数组 NSArray * array = [[NSArray alloc]initWithObjects:@"One", ...
- 在Activity之间传递数据—传递值对象
传递有两种方式,一种是类继承自Serializable(Java方式,速度较慢),另一种是类继承自Parcelable(Android方式) 继承自Serializable的时候,实现比较简单,类只需 ...