BigNum

输入输出用 .bin().bout()

  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
namespace BigNum
{
    static const int Base=100000;
    static const int Dig=5;
    struct bignum
    {
        short fh;
        vector<int> V;
        bignum() {*this=0;}
        bignum(int num) {*this=num;}
        inline int size() {return V.size();}
        inline int back() {return V.back();}
        inline void resize(int N) {return V.resize(N),void();}
        inline void clear() {return V.clear(),void();}
        inline void bin()  {string s; cin>>s,*this=s;}
        inline void bout()
        {
            if(fh) putchar('-');
            write(V.back(),'~');
            for(int i=((int)V.size())-2;~i;--i) 
            {
                char buf[20];
                sprintf(buf,"%05lld",V[i]);
                for(int j=0;j<((int)strlen(buf));++j) putchar(buf[j]);
            }
        }

        inline int &operator [] (int i) {return V[i];}
        inline bignum operator = (string s)
        {
            V.clear(),fh=s[0]=='-'?1:0;
            string ss=""; for(int i=fh;i<((int)s.size());++i) ss+=s[i];
            int len=(ss.size()-1)/Dig+1;
            for(int i=0,x;i<len;++i)
            {
                int end=ss.size()-i*Dig,start=max(0ll,end-Dig);
                sscanf(ss.substr(start,end-start).c_str(),"%lld",&x);
                V.eb(x);
            }
            return *this;
        }
        inline bignum operator = (int T)    {V.clear(),fh=T<0?1:0; T=abs(T); do this->V.eb(T%Base),T/=Base; while(T>0); return *this;}
        inline friend bool operator == (bignum a,bignum b)
        {
            if(a.size()!=b.size()||a.fh!=b.fh) return 0;
            for(int i=0;i<((int)a.size());++i) if(a[i]!=b[i]) return 0;
            return 1;
        }
        inline friend bool operator >  (bignum a,bignum b)
        {
            if(a.fh<b.fh) return 1;
            if(a.size()>b.size()) return 1;
            if(a.size()<b.size()) return 0;
            for(int i=((int)a.size())-1;~i;--i)
            {
                if(a[i]>b[i]) return 1;
                if(a[i]<b[i]) return 0;
            }
            return 0;
        }
        inline friend bool operator <  (bignum a,bignum b)
        {
            if(a.fh>b.fh) return 0;
            if(a.size()>b.size()) return 0;
            if(a.size()<b.size()) return 1;
            for(int i=((int)a.size())-1;~i;--i)
            {
                if(a[i]>b[i]) return 0;
                if(a[i]<b[i]) return 1;
            }
            return 0;
        }
        inline friend bool operator >= (bignum a,bignum b) {return a==b||a>b;}
        inline friend bool operator <= (bignum a,bignum b) {return a==b||a<b;}
        inline friend bignum operator + (bignum a,bignum b)
        {
            if(a.fh!=b.fh) return a.fh?(a.fh^=1,b-a):(b.fh^=1,a-b);
            bignum c; c.clear(),c.fh=a.fh;
            for(int i=0,carry=0;;++i)
            {
                if(!carry&&i>=a.size()&&i>=b.size()) break;
                int X=carry;
                X+=(i<a.size()?a[i]:0)+(i<b.size()?b[i]:0);
                c.V.eb(X%Base),carry=X/Base;
            }
            return c;
        }
        inline friend bignum operator - (bignum a,bignum b)
        {
            if(b.fh) return b.fh^=1,a+b;
            bignum c; int flag=0;
            if(a<b) flag=1,Swp(a,b);
            c=a,c.fh=flag;
            for(int i=0,tmp;i<c.size();++i)
            {
                if(i>=b.size()) tmp=0; else tmp=b[i];
                if(c[i]<tmp) c[i+1]-=1,c[i]+=Base;
                c[i]-=tmp;
            }
            while(c.back()==0&&c.size()>1) c.V.pb();
            return c;
        }
        inline friend bignum operator * (bignum a,bignum b)
        {
            bignum c;
            c.resize(a.size()+b.size()),c.fh=(a.fh!=b.fh);
            for(int i=0;i<a.size();++i) for(int j=0;j<b.size();++j) c[i+j]+=a[i]*b[j];
            for(int i=0;i<c.size()-1;++i) c[i+1]+=c[i]/Base,c[i]%=Base;
            while(c.back()==0&&c.size()>1) c.V.pb();
            return c;
        }
        inline friend bignum operator ^ (bignum a,int b)
        {
            bignum res=a;
            while(b) {if(b&1) res*=a; a*=a; b>>=1;}
            return res;
        }
        inline bignum operator += (const bignum &T) {*this=*this+T; return *this;}
        inline bignum operator -= (const bignum &T) {*this=*this-T; return *this;}
        inline bignum operator *= (const bignum &T) {*this=*this*T; return *this;}
        inline bignum operator ^= (const int    &T) {*this=*this^T; return *this;}
    };

    bignum ZERO;
    int Pow[MAX],flag;
    inline void DivInit()
    {
        ZERO="0";
        Pow[0]=1; for(int i=1;i<=__lg(Base);++i) Pow[i]=Pow[i-1]*2;
        flag=1;
    }

    inline bignum trans(bignum a,int x)
    {
        bignum b; b.resize(a.size()+x);
        for(int i=0;i<a.size();++i) b[i+x]=a[i];
        return b;
    }
    inline pair<bignum,bignum> Divide(bignum a,bignum b)
    {
        if(!flag) DivInit();
        if(a<b) return mp(ZERO,a);
        bignum c,d;
        c.resize(a.size()-b.size()+1),d=a;
        for(int i=c.size()-1;~i;--i)
        {
            bignum t; t=trans(b,i);
            for(int j=__lg(Base);~j;--j) if(d>=t*bignum(Pow[j])) c.V[i]+=Pow[j],d-=t*bignum(Pow[j]);
        }
        while(c.back()==0&&c.size()>1) c.V.pb();
        return mp(c,d);
    }
}