Harmonic Number
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:
In this problem, you are given n, you have to find Hn.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 108).
Output
For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.
Sample Input
12
1
2
3
4
5
6
7
8
9
90000000
99999999
100000000
Sample Output
Case 1: 1
Case 2: 1.5
Case 3: 1.8333333333
Case 4: 2.0833333333
Case 5: 2.2833333333
Case 6: 2.450
Case 7: 2.5928571429
Case 8: 2.7178571429
Case 9: 2.8289682540
Case 10: 18.8925358988
Case 11: 18.9978964039
Case 12: 18.9978964139
解题思路:
调和级数。
看到这么简单的公式第一反应。 打表!!
当然定睛一看 108 次方。好像不能打表。
其实还是可以的。不要硬打就行了。
每隔100个记录一次。
因为t并不大。那么每次最多算100次。
当然用公式算更快了:
注释有公式求法。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) print f("%d\n", n)
#define pc(n) print f("%c", n)
#define pdd(n,m) print f("%d %d", n, m)
#define pld(n) print f("%int d\n", n)
#define pldd(n,m) print f("%int d %int d\n", n, m)
#define sld(n) scanf("%int d",&n)
#define sldd(n,m) scanf("%int d%int d",&n,&m)
#define slddd(n,m,k) scanf("%int d%int d%int d",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define aint (x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int ,int >
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define Max(x,y) (x)>(y)?(x):(y)
#define int long long
typedef pair<int ,int > PII;
typedef long long ll;
typedef unsigned long long ull ;
typedef long double ld;
const int MOD = 1e9 + 7;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int INF = 0x3f3f3f3f3f3f3f3f;
//const int inf = 0x3f3f3f3f;
inline int read(){int ret = 0, sgn = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')sgn = -1;ch = getchar();}
while (ch >= '0' && ch <= '9'){ret = ret*10 + ch - '0';ch = getchar();}
return ret*sgn;}
inline void Out(int a){if(a>9) Out(a/10);putchar(a%10+'0');}
int qmul(int a,int b,int mod){int res=0;while(b){if(b&1)res=(res+a)%mod;a=(a+a)%mod;b>>=1;}return res;}
int qpow(int m,int k,int mod){int res=1%mod,t=m%mod;while(k){if(k&1)res=qmul(res,t,mod);t=qmul(t,t,mod);k>>=1;}return res;}
int gcd(int a,int b){if(b > a) swap(a,b); return b==0?a : gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int inv(int x,int mod){return qpow(x,mod-2,mod)%mod;}
//const int N = 3e3+15;
int t = 1,cas = 1;
int n,m;
const int N = 1e6+15;
int prime[700000],pcnt;
bool mark[N];
// 如果变量名都相同的话,就不用传参了
//void getPrimes(int prime[],int N,int &pcnt)
void getPrimes()
{
memset(mark,0,sizeof(mark));
mark[0] = mark[1] = 1;
pcnt = 0;
for(int i = 2; i < N ; i ++)
{
if(!mark[i])
prime[pcnt++] = i;
for(int j = 0 ; i*prime[j] < N && j < pcnt ; j ++)
{
mark[i*prime[j]] = 1;
if(i%prime[j] == 0)
break;
}
}
}
double ans[N];
signed main(){
ans[0] = 0;
double res = 0;
for(int i = 1 ; i < N*100 ; i ++){
res += 1.0/i;
if(i%100 == 0){
ans[i/100] = res;
}
}
cin>>t;
while(t--){cin>>n;
res = ans[n/100];
for(int i = n/100*100+1; i <= n ; i ++){
res += 1.0/i;
}
printf("Case %lld: %.8lf\n",cas++,res);
}
return 0;
}
/*
void init()
{
for(int i=1;i<=1000002;i++)
{
res[i] += res[i-1] + 1.0/i;
}
}
int main()
{
init();
int t;
scanf("%d",&t);
int cas=0;
while(t--)
{
int n;
scanf("%d",&n);
double sum=0;
if(n<=1000000)
{
sum=res[n];
}
else
{
sum = log(n)+0.57721566490153286060651209+1.0/(n*2);
}
printf("Case %d: %.10lf\n",++cas,sum);
}
}*/
// 调和级数近似求和通项解 f(n)≈ln(n)+C+1/(2*n)
// C≈0.57721566490153286060651209