题目链接:lightoj 1234 - Harmonic Number
题意:给定一个 n ,然后求解调和级数。
思路:首先可以用工数的知识,找极限加上欧拉常数。一开始估计精度不够,事实上也是可以过得。发现有
AC 代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e6 + 1;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
void add(LL &x, LL y) { x += y; x %= MOD; }
double f[MAXN]; int top;
const int N = 1e8;
void getf()
{
top = 0; f[top++] = 0; double res = 0;
for(int i = 1; i <= N; i++)
{
res += 1.0 / i;
if(i % 50 == 0) {
f[top++] = res;
}
}
}
int main()
{
getf();
int t, kcase = 1; scanf("%d", &t);
while(t--)
{
int n; scanf("%d", &n);
int s = n / 50;
double ans = f[s];
for(int i = s * 50+1; i <= n; i++) {
ans += 1.0 / i;
}
printf("Case %d: %.12lf\n", kcase++, ans);
}
return 0;
}