LightOJ 1234 Harmonic Number (调和级数水题)
2016-07-27 17:47
405 查看
题目链接:
http://acm.hust.edu.cn/vjudge/contest/70017#problem/I
题目大意:
求调和级数前n项的和,T个样例,(1≤T≤10000,1≤n≤108)
分析:
直接求肯定TLE,但是如果使用公式的话前几项精度不够,所以前106或107项
O(N)暴力跑出,然后使用高精度的调和级数求和公式,具体可以搜索维基百科或者百度欧拉常数
http://baike.baidu.com/link?url=BWFVuV7oshbt5k7Z2HhvmV84MlXGg2bBE0_MJsQ9ZOJLI8o773s5-Z6k6xK7csGekpFwn0kn539eYbgY-lUDeq
最后的三个公式是表示欧拉常数,y=Hn−ln n+ln(n+1)2−16n(n+1)+130n2(n+1)2 (Hn为调和级数前n项的和)
代码:
http://acm.hust.edu.cn/vjudge/contest/70017#problem/I
题目大意:
求调和级数前n项的和,T个样例,(1≤T≤10000,1≤n≤108)
分析:
直接求肯定TLE,但是如果使用公式的话前几项精度不够,所以前106或107项
O(N)暴力跑出,然后使用高精度的调和级数求和公式,具体可以搜索维基百科或者百度欧拉常数
http://baike.baidu.com/link?url=BWFVuV7oshbt5k7Z2HhvmV84MlXGg2bBE0_MJsQ9ZOJLI8o773s5-Z6k6xK7csGekpFwn0kn539eYbgY-lUDeq
最后的三个公式是表示欧拉常数,y=Hn−ln n+ln(n+1)2−16n(n+1)+130n2(n+1)2 (Hn为调和级数前n项的和)
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <cmath> #include <set> #define MOD 998244353 using namespace std; typedef unsigned long long ull; typedef long long ll; /*---------------------------head files----------------------------------*/ const double euler = 0.577215664901532860606512090082; double arr[1000009]; void init() { arr[1] = 1.0; for (int i = 2 ; i <= 1000000 ; i ++) { arr[i] = arr[i-1] + 1.0 / i ; } } int main() { init(); int T,k=1; ll n; scanf("%d",&T); while (T--) { scanf("%lld",&n); printf("Case %d: ",k++); if (n<=1000000) printf("%.8f\n",arr ); else { double ans = (log(n) + log(n+1))/2.0; double x1 = 1.0; x1 = x1 / 6.0 / n / (n+1); double x2 = x1 ; x2 = x2 /5.0 / n / (n+1); ans = ans - x1 + x2 ; printf("%.10f\n",ans+euler); } } }
相关文章推荐
- LightOJ 1067 Combinations 组合数取模
- LightOJ 1004 - Monkey Banana Problem
- LightOJ 1011 - Marriage Ceremonies
- LightOJ 1033 - Generating Palindromes
- LightOJ 1047 - Neighbor House
- LightOJ 1013 - Love Calculator
- LightOJ 1122 - Digit Count
- LightOJ1112——Curious Robin Hood
- lightoj-1427:Substring Frequency
- lightoj 1007Mathematically Hard (欧拉函数水题)
- lightoj1197Help Hanzo(二次筛)
- lightoj1213 - Fantasy of a Summation
- lightoj1214 - Large Division
- lightoj1220Mysterious Bacteria
- lightoj1234Harmonic Number
- lightoj1236Pairs Forming LCM
- lightoj1245Harmonic Number (II)
- lightoj1259 - Goldbach`s Conjecture(水题)
- lightoj1282 - Leading and Trailing(水题)
- lightoj1336Sigma Function(dfs打表或容斥)