您的位置:首页 > 其它

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项的和)

代码:

#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