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 ...
随机推荐
- redis数据类型:sets
sets类型及操作: set是集合,它是string类型的无序集合.set是通过hash table实现的, 添加.删除和查找的复杂度都是O(1).对集合我们可以取并集.交集.差集. 通过这些操作我们 ...
- IAR和Keil文件包含路径设置
在模块化编程时,为一个模块单独设置头文件是必不可少的. 在两款主流编译器中,在引用模块函数时候,包含头文件路径是必须的,那么设置文件路径的准确性就显得尤为重要. 否则,编译器会报错,无法打开某某头文件 ...
- Mysql CPU占用高的问题解决方法小结
通过以前对mysql的操作经验,先将mysql的配置问题排除了,查看msyql是否运行正常,通过查看mysql data目录里面的*.err文件(将扩展名改为.txt)记事本查看即可.如果过大不建议用 ...
- Webdriver如何解决页面元素过期:org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
当运行Webdriver时抛出如下异常:org.openqa.selenium.StaleElementReferenceException: Element not found in the cac ...
- POJ 2239 Selecting Courses
二分图的最大匹配.课程和时间可以看做二分图. #include<cstdio> #include<cstring> #include<cmath> #include ...
- Chapter 2 Open Book——10
I sent that, and began again. 我发送了它,然后又一次重新开始写了. Mom,Everything is great. Of course it's raining. I ...
- Chapter 2 Open Book——1
The next day was better… and worse. 明天会更好也会更坏. It was better because it wasn't raining yet, though t ...
- Dokan虚拟磁盘开发实战
因工作需要,最近与同事合作使用Dokan开发了一个虚拟磁盘的简单程序,初步实现了远程目录映射到本地虚拟磁盘的功能. 远程服务端是用Python写的,主要是将远程主机上的目录文件传给客戶端,在这里就不细 ...
- ieee80211_rx
ieee80211rx.c(E:\code\linux\net\ieee80211) 所有接收到的帧都送到这个函数中去 int ieee80211_rx(struct ieee80211_device ...
- Entity Framework技巧系列之二 - Tip 6 - 8
提示6. 如何及何时使用贪婪加载 什么时候你需要使用贪婪加载? 通常在你的程序中你知道对查询到的实体将要进行怎样的操作. 例如,如果你查询一个订单以便为一个客户重新打印,你知道没有组成订单的项目即产品 ...