X

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "myalgo.h"

#define OneThread 1

typedef unsigned long long ull;

using namespace std;

const int initsz=32768;
const ull E10s[]= {1,10,100,1000,10000,100000,
    1000000,10000000,100000000,1000000000,10000000000ULL,
    100000000000ULL,1000000000000ULL,10000000000000ULL,
    100000000000000ULL,1000000000000000ULL};

class xu {
public:
    ull *d;
    int lt,sz;
    xu () {
        sz=initsz;
        lt=-1;
        d=new ull[sz];
        //for(int i=0;i<sz;++i) d[i]=0;
    }
    ~xu () {
        delete[] d;
    }
    xu (const xu& x) {
        sz=x.sz;
        lt=x.lt;
        d=new ull[sz];
        for(int i=0; i<=lt; ++i) d[i]=x.d[i];
        //for(int i=0;i<sz;++i) d[i]=x.d[i];
    }
    xu (const char* s) {
        lt=(d[0]=0);
        for(int i=0; s[i]!=0; ++i) {
            (*this)*=10;
            d[0]+=s[i]-48;
        };
    }
    xu& operator =(const xu& x) {
        sz=x.sz;
        lt=x.lt;
        d=new ull[sz];
        for(int i=0; i<=lt; ++i) d[i]=x.d[i];
        return *this;
    }
    xu& operator =(const ull& x) {
        lt=0;
        d[0]=x;
    }
    xu& operator =(const char* s) {
        lt=-1;
        d[0]=0;
        unsigned k;
        int i,j;
        for(i=0; s[i]!=0; i+=9) {
            k=0;
            for(j=i; j<i+9; ++j) {
                if(s[j]<'0'||s[j]>'9') {
                    (*this)*=E10s[j-i];
                    (*this)+=(xu)k;
                    return *this;
                }
                k=k*10+s[j]-48;
            }
            (*this)*=1000000000;
            (*this)+=(xu)k;
        }
        return *this;
    }
    xu (const ull& x) {
        sz=1;
        lt=0;
        d=new ull[sz];
        for(int i=1; i<sz; ++i) d[i]=0;
        d[0]=x;
    }
    xu (const ull& x,int s) {
        sz=s;
        lt=0;
        d=new ull[sz];
        for(int i=1; i<sz; ++i) d[i]=0;
        d[0]=x;
    }
    
    xu enlarge(int s) {
        if(s<initsz) s=initsz;
        sz+=s;
        ull* nd=new ull[sz];
        for(int i=0; i<=lt; ++i) {
            nd[i]=d[i];
        }
        delete[] d;
        d=nd;
    }

    void indec() {
        //scanf("%llu",&d[0]);
        lt=(d[0]=0);
        char s[65536]= {};
        scanf("%s",s);
        for(int i=0; s[i]!=0; ++i) {
            (*this)*=10;
            d[0]+=s[i]-48;
        }
    }
    void outdec() {
        if(lt==0) {
            printf("%llu\n",d[0]);
            return;
        }
        int osz=lt*5/2+4,i,olt=0;
        ull *n=new ull[osz],up;
        for(i=0; i<osz; ++i) n[i]=0;
        const ull E9=1000000000;
        for(int j=lt; j>=0; --j) {
            up=0;
            for(i=0; i<=olt; ++i) {
                n[i]<<=32;
            }
            n[0]+=(d[j]>>32);
            for(i=0; i<=olt+2; ++i) {
                up=(n[i]+=up)/E9;
                n[i]-=up*E9;
            }
            if(n[olt+2]!=0) olt+=2;
            else if(n[olt+1]!=0) olt+=1;

            up=0;
            for(i=0; i<=olt; ++i) {
                n[i]<<=32;
            }
            n[0]|=(d[j]&(ull)0xffffffff);
            for(i=0; i<=olt+2; ++i) {
                up=(n[i]+=up)/E9;
                n[i]-=up*E9;
            }
            if(n[olt+2]!=0) olt+=2;
            else if(n[olt+1]!=0) olt+=1;
        }
        printf("%llu",n[olt]);
        for(i=olt-1; i>=0; --i) {
            printf("%09llu",n[i]);
        }
        printf("\n");
    }
    void outhex() {
        for(int i=lt; i>=0; --i) {
            printf("%08X",(int)(d[i]>>32));
            printf("%08X",(int)d[i]);
        }
        printf("\n");
    }

    xu &l_move64(unsigned step) {
        if(lt+step+2>sz) {
            enlarge(step);
        }
        int i;
        for(i=(lt+=step); i>=step; --i) {
            d[i]=d[i-step];
        }
        while(i>=0) d[i--]=0;
        return *this;
    }
    xu& operator <<=(unsigned step) {
        if(step>=64) {
            l_move64(step>>6);
            step&=63;
        }
        unsigned _st_=64-step;
        ull a=0,b=(d[lt+1]=0);
        for(int i=lt+1; i>0; --i) {
            d[i]|=(d[i-1]>>_st_);
            d[i-1]<<=step;
        }
        if(d[lt+1]!=0) ++lt;
        return *this;
    }
    xu &r_move64(unsigned step) {
        lt-=step;
        for(int i=0; i<=lt; ++i) d[i]=d[i+step];
        return *this;
    }
    xu& operator >>=(unsigned step) {
        if(step>=64) {
            r_move64(step>>6);
            step&=63;
        }
        unsigned _st_=64-step;
        ull a=0,b=0;
        for(int i=0; i<lt; ++i) {
            (d[i]>>=step)|=(d[i+1]<<_st_);
        }
        if((d[lt]>>=step)==0) lt--;
        return *this;
    }

    xu& operator +=(const xu &x) {
        if(x.lt>sz-2||lt>sz-2) enlarge(0);
        d[lt+1]=0;
        for(int i=lt+1;i<x.lt+2;++i) d[i]=0;
        for(int i=0; i<=x.lt;) {
            if((d[i]+=x.d[i])<x.d[i]) {
                while((d[++i]+=1)==0) d[i]=x.d[i];
            } else ++i;
        }
        if(d[lt+1]!=0) lt++;
        return *this;
    }
    xu operator +(const xu& x){
        xu a=(*this);
        a+=x;
        return a;
    }
    xu& operator -=(const xu& x){
        for(int i=0; i<=x.lt;) {
            if(d[i]<(d[i]-=x.d[i])) {
                while((d[++i]-=1)==F_64) d[i]-=x.d[i];
            } else ++i;
        }
        while(d[lt]==0&&lt>=0) lt--;
        return *this;
    }
    xu operator -(const xu& x){
        xu a=(*this);
        a-=x;
        return a;
    }
    xu& operator *=(ull x) {
        if(lt>sz-2) enlarge(0);
        if(x>(ull)0xffffffff) {
            unsigned x1=(x>>32),x2=(x&(ull)0xffffffff);
            xu y=*this;
            (*this)*=x1;
            y*=x2;
            (*this)<<=32;
            (*this)+=y;
            return *this;
        }
        ull a=0,b=0,c=(d[lt+1]=d[lt+2]=0);
        for(int i=0; i<lt+2; ++i) {
            a=(d[i]>>32)*x;
            d[i]=(d[i]&F_32)*x;
            b=c;
            c=(a>>32);
            a=((a<<32)|b);
            if((d[i]+=a)<a) ++c;
        }
        if(d[lt+1]) ++lt;
        return *this;
    }
    xu operator *(ull x){
        xu a=(*this);
        a*=x;
        return a;
    }
    unsigned operator %(unsigned x) {
        ull r=0;
        for(int i=lt; i>=0; --i) {
            r=((((r<<32)+(d[i]>>32))%x<<32)+(d[i]&F_32))%x;
        }
        return r;
    }
    xu& operator /=(unsigned x) {
        ull a,b,c=0;
        for(int i=lt; i>=0; --i) {
            c+=(d[i]>>32);
            b=c/x;
            a=((c-b*x)<<32)+(d[i]&F_32);
            d[i]=a/x;
            c=((a-x*d[i])<<32);
            d[i]|=(b<<32);
        }
        if(d[lt]==0) lt--;
        return *this;
    }
    xu operator /(unsigned x){
        xu a=(*this);
        a/=x;
        return a;
    }
};

xu xfact(unsigned n) {
    xu r=1;
    unsigned i=1;
    if(n%2) {
        r=n;
        --n;
    }
    while(n>i) {
        r*=(n*i);
        --n;
        ++i;
    }
    return r;
}

xu xpow(ull a,unsigned b){
    xu x(1,initsz);
    while(b--) x*=a;
    return x;
}
xu calcpi(unsigned n){
    xu a=xpow(100000000,n),b=a*7,p(0,a.lt+4),p2(0,a.lt+4),d(0,a.lt+4);
    int z=-1;
    a*=3;
    while((d=(a/=9)/(z+=2)).lt>=0){
        p2+=(b/=49)/z;
        (p+=d)-=(a/=9)/(z+=2);
        p2-=(b/=49)/z;
    }
    //www.cnblogs.com
    
    return ((p<<=1)+=p2);
}

int main() {
    xu x,y;
    ull a,t;
    char s[65536]={};
    printf("%llu\n\n",allbits);
    while(1) {
        scanf("%llu",&a);
        calcpi(a).outdec();
    }
    return 0;
}

 

posted @ 2017-03-04 21:52  141421356  阅读(135)  评论(0)    收藏  举报