HDU 6217 BBP Formula

$\pi$ 的算法还是很多的

HDU 6217 BBP Formula

题目描述

利用下面式子计算 $\pi$ 的第 $k$ 十六进制位

$ \pi = \sum_{k=0}^{\infty }\frac{1}{16^{k}}(\frac{4}{8k+1}-\frac{2}{8k+4}-\frac{1}{8k+5}-\frac{1}{8k+6}) $

题解

采用 BBP 的奇怪算法。

计算第 $n$ 位的时候,将级数拆分成两段(拿第一个级数为例)

$\sum_{k=0}^{\infty }\frac{1}{16^{k}}(\frac{1}{8k+1})$

$ = \sum{k=0}^{n}\frac{1}{16^{k}}(\frac{1}{8k+1}) + \sum{k=n+1}^{\infty}\frac{1}{16^{k}}(\frac{1}{8k+1})$

$ = \frac {1} {16^n}(\sum{k=0}^{n}\frac{16^{n-k}}{8k+1} + \sum{k=n+1}^{\infty}\frac{16^{n-k}}{8k+1})$

$ = \frac {1} {16^n}(\sum_{k=0}^{n}\frac{16^{n-k} \text {mod} (8k+1)}{8k+1} + R(n))$

由于是计算第 $n$ 位,我们就将前面的分数消掉就行。 $R(n)$ 计算 $1000$ 项即可。

时间复杂度为 $O(n)$ 。

AC Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//#define NOSTDCPP
//#define Cpp11
//#define Linux_System
#ifndef NOSTDCPP
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <cstdio>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#endif
# ifdef Linux_System
# define getchar getchar_unlocked
# define putchar putchar_unlocked
# endif
# define RESET(_) memset(_, 0, sizeof(_))
# define RESET_(_, val) memset(_, val, sizeof(_))
# define fi first
# define se second
# define pb push_back
# define midf(x, y) ((x + y) >> 1)
# define DXA(_) ((_ << 1))
# define DXB(_) ((_ << 1) | 1)
# define next __Chtholly__
# define x1 __Mercury__
# define y1 __bbtl04__
# define index __ikooo__
using namespace std;
typedef long long ll;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;
typedef long double ld;
const int MOD = 1e9 + 7;
const int maxn = 300009;
const int maxm = 300009;
const double eps = 1e-6;
ll myrand(ll mod){return ((ll)rand() << 32 ^ (ll)rand() << 16 ^ rand()) % mod;}
template <class T>
inline bool scan_d(T & ret)
{
char c;
int sgn;
if(c = getchar(), c == EOF)return false;
while(c != '-' && (c < '0' || c > '9'))c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(), c >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
ret *= sgn;
return true;
}
#ifdef Cpp11
template <class T, class ... Args>
inline bool scan_d(T & ret, Args & ... args)
{
scan_d(ret);
scan_d(args...);
}
#define cin.tie(0); cin.tie(nullptr);
#define cout.tie(0); cout.tie(nullptr);
#endif
inline bool scan_ch(char &ch)
{
if(ch = getchar(), ch == EOF)return false;
while(ch == ' ' || ch == '\n')ch = getchar();
return true;
}
inline void out_number(ll x)
{
if(x < 0)
{
putchar('-');
out_number(- x);
return ;
}
if(x > 9)out_number(x / 10);
putchar(x % 10 + '0');
}
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
ll pow_mod(ll a, ll b, ll c)
{
if(c == 1) return 0;
ll ans = 1;
while(b)
{
if(b & 1) ans = a * ans % c;
b >>= 1;
a = a * a % c;
}
return ans;
}
double BBP(int n, ll x)
{
double ans = 0.0;
for(int i = 0; i <= n; ++ i) ans += (pow_mod(16, n - i, x + 8 * i) * 1.0 / (x + 8 * i));
for(int i = n + 1; i <= n + 1 + 1000; ++ i) ans += powf(16, n - i) / (x + 8 * i);
return ans;
}
int BIT(int x)
{
if(x < 10) return '0' + x;
return x - 10 + 'A';
}
int main()
{
int T, n;
double ans;
scanf("%d", &T);
for(int Casen = 1; Casen <= T; ++ Casen)
{
ans = 0.0;
scanf("%d", &n);
-- n;
ans += 4.0 * BBP(n, 1);
//cout << ans << endl;
ans += -2.0 * BBP(n, 4);
//cout << ans << endl;
ans += -1.0 * BBP(n, 5);
//cout << ans << endl;
ans += -1.0 * BBP(n, 6);
ans = ans - (int) ans;
if(ans < 0.0) ans += 1.0;
//cout << ans << endl;
ans = ans * 16.0;
printf("Case #%d: %d %c\n", Casen, ++ n, BIT(int(ans)));
}
return 0;
}
文章目录
  1. 1. HDU 6217 BBP Formula
    1. 1.1. 题目描述
    2. 1.2. 题解
      1. 1.2.0.1. AC Code
{{ live2d() }}