高精度模板

· · 个人记录

但是这个高精度跑得比较慢。。。有没有大佬可以帮我优化一下 QWQ。

struct LLINT{
    static const long long base=1000000;
    static const long long width=6;
    vector<long long>v;
    bool op;
    inline LLINT(long long num=0){
        *this=num;
        op=false;
    }
    inline LLINT operator = (long long val){
        v.clear();
        if(val<0){
            val=-val;
            op=true;
        }
        do{
            v.push_back(val%base);
            val/=base;
        }while(val>0);
        return *this;
    }
    inline LLINT operator = (const string &str){
        v.clear();
        string str2=str;
        if(str2[0]=='-'){
            op=true;
            for(string::iterator i=str2.begin();i<str2.end()-1;i++){
                *i=*(i+1);
            }
            *(str2.end()-1)='\0';
            str2.resize(str2.length()-1);
        }
        long long cur,len=(str2.length()-1)/width+1;
        for(long long i=0;i<len;i++){
            long long end=str2.length()-i*width;
            long long begin=max(0ll,end-width);
            sscanf(str2.substr(begin,end-begin).c_str(),"%lld",&cur);
            v.push_back(cur);
        }
        return *this;
    }
    inline LLINT operator + (const LLINT &W)const{
        LLINT sum;
        sum.v.clear();
        if(op==true){
            if(W.op==true){
                sum.op=true;
            }else{
                return W-(-*this);
            }
        }else if(W.op==true){
            return *this-(-W);
        }
        long long n=v.size(),m=W.v.size();
        for(long long i=0,g=0;g!=0||i<n||i<m;i++){
            long long cur=g;
            if(i<n){
                cur+=v[i];
            }
            if(i<m){
                cur+=W.v[i];
            }
            g=cur/base;
            cur%=base;
            sum.v.push_back(cur);
        }
        return sum;
    }
    inline LLINT operator += (const LLINT &W){
        *this=*this+W;
        return *this;
    }
    inline LLINT operator - (const LLINT &W)const{
        LLINT G;
        G.v.clear();
        if(op==false&&W.op==false&&*this<W){
            return -(W-*this);
        }
        if(op==true&&W.op==false){
            return -(-*this+W);
        }else if(op==false&&W.op==true){
            return *this+(-W);
        }else if(op==true&& W.op==true){
            return -W-(-*this);
        }
        long long n=v.size(),m=W.v.size();
        for(long long i=0,g=0;g!=0||i<n;i++){
            long long cur=v[i]+g;
            if(i<m){
                cur-=W.v[i];
            }
            if(cur>=0){
                g=0;
            }else{
                g=-1;
                cur+=base;
            }
            G.v.push_back(cur);
        }
        for(long long i=G.v.size()-1;i>=0;i--){
            if(G.v[i]==0){
                G.v.resize(G.v.size()-1);
            }else{
                break;
            }
        }
        if(G.v.size()==0){
            G.v.resize(1,0);
        }
        return G;
    }
    inline LLINT operator - ()const{
        LLINT W=*this;
        W.op=!(op);
        return W;
    }
    inline LLINT operator -= (const LLINT &W){
        *this=*this-W;
        return *this;
    }
    inline LLINT operator * (const LLINT &W)const{
        LLINT G;
        G.v.clear();
        if(op!=W.op){
            G.op=true;
        }
        long long n=v.size(),m=W.v.size(),len=n+m-1;
        G.v.resize(n+m);
        for(long long i=0;i<n;i++){
            for(long long j=0;j<m;j++){
                G.v[i+j]+=this->v[i]*W.v[j];
                if(G.v[i+j]>=base){
                    G.v[i+j+1]+=G.v[i+j]/base;
                    G.v[i+j]%=base;
                }
            }
        }
        for(long long i=len;i>=0;i--){
            if(G.v[i]==0){
                len--;
            }else{
                break;
            }
        }
        G.v.resize(len+1);
        if(G.v.size()==0){
            G.v.resize(1,0);
        }
        return G;
    }
    inline LLINT operator *= (const LLINT &W){
        *this=*this*W;
        return *this;
    }
    inline LLINT operator / (const LLINT &W)const{
        LLINT G=0;
        if(op!=(W<0)){
            G.op = true;
        }
        LLINT V=0;
        for(long long i=v.size()-1;i>=0;i--){
            V=V*base+v[i];
            long long l=0,r=base-1,mid;
            LLINT re;
            while(l<=r){
                mid=(l+r)/2;
                re=(long long)mid;
                LLINT bi=V-re*W;
                if(V-re*W<W&&V-re*W>=0){
                    break;
                }else if(V-re*W>=W){
                    l=mid+1;
                }else{
                    r=mid-1;
                }
            }
            G=G*base+re;
            V=V-re*W;
        }
        for(long long i=G.v.size()-1;i>=0;i--){
            if(G.v[i]==0){
                G.v.resize(G.v.size()-1);
            }else{
                break;
            }
        }
        if(G.v.size()==0){
            G.v.resize(1,0);
        }
        return G;
    }
    inline LLINT operator % (const LLINT &W)const{
        LLINT G=*this/W;
        LLINT V=*this-G*W;
        return V;
    }
    inline LLINT operator /= (const long long &W){
        *this=*this/W;
        return *this;
    }
    inline LLINT operator /= (const LLINT &W){
        *this=*this/W;
        return *this;
    }
    inline LLINT operator %= (const long long &W){
        *this=*this%W;
        return *this;
    }
    inline LLINT operator %= (const LLINT &W){
        *this=*this%W;
        return *this;
    }
    inline LLINT operator ++ (){
        LLINT W=1ll;
        *this=*this+W;
        return *this;
    }
    inline LLINT operator -- (){
        LLINT W=1ll;
        *this=*this-W;
        return *this;
    }
    inline LLINT operator ++ (int){
        LLINT W=1ll;
        *this=*this+W;
        return *this;
    }
    inline LLINT operator -- (int){
        LLINT W=1ll;
        *this=*this-W;
        return *this;
    }
    inline bool operator < (const LLINT &W)const{
        if(op==true&&W.op==false){
            return true;
        }
        if(op==false&&W.op==true){
            return false;
        }
        if(op==true&&W.op==true){
            LLINT G,V;
            G=*this;
            G.op=false;
            V=W;
            V.op=false;
            return V<G;
        }
        if(v.size()!=W.v.size()){
            return v.size()<W.v.size();
        }
        for(long long i=v.size()-1;i>=0;i--){
            if(v[i]!=W.v[i]){
                return v[i]<W.v[i];
            }
        }
        return false;
    }
    inline bool operator > (const LLINT &W)const{
        return W<*this;
    }
    inline bool operator <= (const LLINT &W)const{
        return !(*this>W);
    }
    inline bool operator >= (const LLINT &W)const{
        return !(*this<W);
    }
    inline bool operator != (const LLINT &W)const{
        return *this<W||*this>W;
    }
    inline bool operator == (const LLINT &W)const{
        return !(*this!=W);
    }
    inline friend istream &operator >> (istream &in,LLINT &W){
        string str;
        if(!(in>>str)){
            return in;
        }
        W=str;
        return in;
    }
    inline friend ostream &operator << (ostream &out,LLINT &W){
        if(W.op==true){
            out<<'-';
        }
        out<<W.v.back();
        for(long long i=W.v.size()-2;i>=0;i--){
            char str[base+5];
            sprintf(str,"%0*lld",(int)width,W.v[i]);
            out<<str;
        }
        return out;
    }
};