美味的高精

· · 个人记录

写了个压位高精vector

一遍过,代码美妙,遂记之

const int BASE=1e9;

struct BIGINT{
    vector<ll> a;
    BIGINT (){
        a.push_back(0);
    }
    BIGINT (ll x){
        if(!x) a.push_back(0);
        while(x){
            a.push_back(x%BASE);
            x/=BASE;
        }
    }
    BIGINT operator+(BIGINT x){
        BIGINT res(0);
        for(int i=0;i<max(a.size(),x.a.size());++i){
            res.a.push_back(0);
            if(i<a.size()) res.a[i]+=a[i];
            if(i<x.a.size()) res.a[i]+=x.a[i];
            res.a[i+1]+=res.a[i]/BASE;
            res.a[i]%=BASE;
        }
        while(res.a.size()>1 && !res.a[res.a.size()-1]) res.a.pop_back();
        return res;
    }
    BIGINT operator*(BIGINT x){
        BIGINT res(0);
        while(res.a.size()<a.size()+x.a.size()) res.a.push_back(0);
        for(int i=0;i<a.size();++i)
            for(int j=0;j<x.a.size();++j){
                res.a[i+j]+=a[i]*x.a[j];
                res.a[i+j+1]+=res.a[i+j]/BASE;
                res.a[i+j]%=BASE;
            }
        for(int i=0;i<res.a.size();++i){
            res.a[i+1]+=res.a[i]/BASE;
            res.a[i]%=BASE;
        }
        while(res.a.size()>1 && !res.a[res.a.size()-1]) res.a.pop_back();
        return res;
    } 
    bool operator<(BIGINT x){
        if(a.size()!=x.a.size()) return a.size()<x.a.size();
        for(int i=a.size()-1;i>=0;--i)
            if(a[i]!=x.a[i])
                return a[i]<x.a[i];
        return 0;
    }
    void print(){
        reverse(a.begin(),a.end());
        printf("%lld",a[0]);
        for(int i=1;i<a.size();++i) printf("%09lld",a[i]);
        reverse(a.begin(),a.end());
    }
};

BIGINT operator+(BIGINT x,ll y){return x+BIGINT(y);}
BIGINT operator+(ll y,BIGINT x){return x+BIGINT(y);}
BIGINT operator*(BIGINT x,ll y){return x*BIGINT(y);}
BIGINT operator*(ll y,BIGINT x){return x*BIGINT(y);}
BIGINT max(BIGINT x,BIGINT y){
    if(x<y) return y;
    return x;
}//