hdu6217 - BBP Formula

链接

点击跳转

题解

先乘以 1 6 n − 1 16^{n-1} 16n1

现在就是要计算小数后面第一位

假设 1 6 n − k = t ( 8 k + 1 ) + r 16^{n-k} = t(8k+1) + r 16nk=t(8k+1)+r,其中 t t t是整数, r r r是不超过 ( 8 k + 1 ) (8k+1) (8k+1)的余数

则其对小数的贡献是 r / ( 8 k + 1 ) r/(8k+1) r/(8k+1)

然后会发现算出来是负数

没关系,我只要不停的加一,直到它大于 0 0 0为止就停下

代码

#include 
#include 
#include 
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct EasyMath
{
    ll prime[maxn], phi[maxn], mu[maxn];
    bool mark[maxn];
    ll fastpow(ll a, ll b, ll c)
    {
        ll t(a%c), ans(1ll);
        for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
        return ans;
    }
    void exgcd(ll a, ll b, ll &x, ll &y)
    {
        if(!b){x=1,y=0;return;}
        ll xx, yy;
        exgcd(b,a%b,xx,yy);
        x=yy, y=xx-a/b*yy;
    }
    ll inv(ll x, ll p)  //p是素数
    {return fastpow(x%p,p-2,p);}
    ll inv2(ll a, ll p)
    {
        ll x, y;
        exgcd(a,p,x,y);
        return (x+p)%p;
    }
    void shai(ll N)
    {
        ll i, j;
        for(i=2;i<=N;i++)mark[i]=false;
        *prime=0;
        phi[1]=mu[1]=1;
        for(i=2;i<=N;i++)
        {
            if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
            for(j=1;j<=*prime and i*prime[j]<=N;j++)
            {
                mark[i*prime[j]]=true;
                if(i%prime[j]==0)
                {
                    phi[i*prime[j]]=phi[i]*prime[j];
                    break;
                }
                mu[i*prime[j]]=-mu[i];
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
        }
    }
    ll CRT(vector<ll> a, vector<ll> m) //要求模数两两互质
    {
        ll M=1, ans=0, n=a.size(), i;
        for(i=0;i<n;i++)M*=m[i];
        for(i=0;i<n;i++)(ans+=a[i]*(M/m[i])%M*inv2(M/m[i],m[i]))%=M;
        return ans;
    }
}em;
ll T, n;
int main()
{
    double ans=0;
    ll i, k, kase;
    T=read();
    rep(kase,1,T)
    {
        ll n=read();
        ans=0;
        rep(k,0,n-1)
        {
            ans += double( 4*em.fastpow(16,n-1-k,8*k+1)%(8*k+1) ) / (8*k+1);
            ans -= double( 2*em.fastpow(16,n-1-k,8*k+4)%(8*k+4) ) / (8*k+4);
            ans -= double( 1*em.fastpow(16,n-1-k,8*k+5)%(8*k+5) ) / (8*k+5);
            ans -= double( 1*em.fastpow(16,n-1-k,8*k+6)%(8*k+6) ) / (8*k+6);
            ans -= ll(ans);
        }
        double inv = 1.0/16;
        rep(k,n,n+100000)
        {
            ans += inv * ( 4.0/(8*k+1) - 2.0/(8*k+4) - 1.0/(8*k+5) - 1.0/(8*k+6) );
            inv /= 16;
        }
        ans -= ll(ans);
        while(ans<0)ans++;
        printf("Case #%lld: %lld %X\n",kase,n,int(ans*16)%16);
    }
    return 0;
}

你可能感兴趣的:(#,数学杂题)