[HDU - 6217 ] BBP Formula (数学)

BBP Formula (数学)

链接: HDU - 6217

题面:

思路:

原式:

\[\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}}) \]

我们将上式乘以\(16^n\)就相当于将小数点向后移动了\(\mathit n\)位。

不妨设:

\[\\ BBP(4,1,n)=\sum_{k=0}^{\infty}\frac{1}{16^k}\frac{4}{8k+1}*16^n \\=\sum_{k=0}^{n}\frac{1}{16^k}\frac{4}{8k+1}*16^n+\sum_{k=n+1}^{\infty}\frac{1}{16^k}\frac{4}{8k+1}*16^n \\ =4*(\sum_{k=0}^{n}\frac{16^{n-k}}{8k+1}+\sum_{k=n+1}^{\infty}\frac{16^{n-k}}{8k+1}) \]

只考虑上面公式的小数位,则等价于:

\[=4*(\sum_{k=0}^{n}\frac{16^{n-k}\ mod\ (8k+1)}{8k+1}+\sum_{k=n+1}^{\infty}\frac{16^{n-k}}{8k+1}) \]

那么对该式前面进行\(O(nlogn)\) 处理,用快速幂就可以避免高精度运算。

后式我们只需要让$\infty $取一个合适的范围,

保证第\(\mathit n\)位答案的正确性即可。

答案为下式的第一位小数,然后转为16进制输出即可。

\[BBP(4,1,n-1)+BBP(-2,4,n-1)+BBP(-1,5,n-1)+BBP(-1,6,n-1) \]

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
double bbp(int k, int base, int n)
{
    double res = 0;
    for (int i = 0; i <= n; ++i) {
        res += powmod(16, n - i, 8 * i + base) * 1.0 / (8 * i + base);
    }
    for (int i = n + 1; i <= n + 1000; ++i) {
        res += powf(16, n - i) * 1.0 / (8 * i + base);
    }
    res *= k;
    return res;
}
char solve(int x)
{
    if (x <= 9) {
        return '0' + x;
    } else {
        return 'A' + x - 10;
    }
}
int n;
int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    int t;
    t = readint();
    repd(icase, 1, t) {
        n = readint();
        double ans = bbp(4, 1, n - 1) + bbp(-2, 4, n-1) + bbp(-1, 5, n-1) + bbp(-1, 6, n-1);
        ans = ans - (int)(ans);
        if (ans < 0) {
            ans = 1 + ans;
        }
        ans*=16;
        char cans = solve((int)(ans));
        printf("Case #%d: %d %c\n", icase, n, cans );
    }

    return 0;
}



posted @ 2020-10-20 20:37  茄子Min  阅读(127)  评论(0编辑  收藏  举报