A: BBP Formula
https://www.cnblogs.com/LzyRapx/p/7802790.html
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define ll long long 5 6 inline ll qpow(ll x, ll n, ll mod) 7 { 8 ll base = x; 9 ll ans = 1; 10 while (n) 11 { 12 if (n & 1) ans = (ans * base) % mod; 13 base = base * base % mod; 14 n >>= 1; 15 } 16 return ans; 17 } 18 19 inline double BBP(int n, ll k, ll b) 20 { 21 double res = 0.0; 22 for (int i = 0; i <= n; ++i) 23 res += qpow(16, n - i, (8 * i + b)) * 1.0 / (8 * i + b); 24 for (int i = n + 1; i <= (n + 500); ++i) 25 res += powf(16, n - i) * 1.0 / (8 * i + b); 26 return k * res; 27 } 28 29 inline char print(double tmp) 30 { 31 int x = int(tmp); 32 if (x >= 0 && x <= 9) 33 return x + '0'; 34 return x - 10 + 'A'; 35 } 36 37 int t, n; 38 39 inline void Run() 40 { 41 scanf("%d", &t); 42 for (int kase = 1; kase <= t; ++kase) 43 { 44 scanf("%d", &n); --n; 45 double ans = BBP(n, 4, 1) - BBP(n, 2, 4) - BBP(n, 1, 5) - BBP(n, 1, 6); 46 ans = ans - (int)ans; 47 if (ans < 0) ans += 1.0; 48 ans *= 16.0; 49 printf("Case #%d: %d %c\n", kase, n + 1, print(ans)); 50 } 51 } 52 53 int main() 54 { 55 #ifdef LOCAL 56 freopen("Test.in", "r", stdin); 57 #endif 58 59 Run(); 60 61 return 0; 62 }