带负数的压位高精度(加减乘)


struct Van{
    
    bool fu; //是否是负数
    int tt,mod; //高精的长度
    int s[1005]; //压位用的数组
    
    inline Van(){ //整体初始化
        fu=0; tt=0; mod=1e9;
        memset(s,0,sizeof(s));
    }
    
    inline Van read(){  register char ch; //高精度读入
        while(!isdigit(ch=getchar()))if(ch=='-')fu=1;
        char _[100005]; rg l=0,r=-1; _[0]=ch;
        while(isdigit(_[++l]=getchar()));; tt=l/9-!(l%9);
        for(rg i=(l-1)%9+1;i;--i) (s[tt]*=10)+=_[++r]^48;
        for(rg i=tt-1;i>=0;--i)for(rg j=0;j<9;++j)(s[i]*=10)+=(_[++r]^48);
        while(tt&&!s[tt])--tt;; return (*this);
    }
    inline void print(){ //高精度输出
        if(fu)putchar('-');
        printf("%d",s[tt]);
        for(rg i=tt-1;i>=0;--i)
            printf("%09d",s[i]);
        putchar('\n');
    }

    inline bool operator >(const Van &x){ //定义大于
        if(tt!=x.tt)return tt>x.tt;
        for(rg i=tt;i>=0;--i)
            if(s[i]!=x.s[i])return s[i]>x.s[i];
        return 0;
    }
    inline bool operator <(const Van &x){ //定义小于
        if(tt!=x.tt)return tt<x.tt;
        for(rg i=tt;i>=0;--i)
            if(s[i]!=x.s[i])return s[i]<x.s[i];
        return 0;
    }

    inline Van operator =(int x){ //int的等于
        while(tt)s[tt]=0,--tt;
        s[0]=x%mod; s[1]=x/mod;
        if(s[1])tt=1;; return *this;
    }
    inline Van operator =(ll x){ //ll的等于
        while(tt)s[tt]=0,--tt;
        while(x)s[tt]=x%mod,x/=mod,++tt;
        if(!s[tt])--tt;; return *this;
    }

    inline void add(const Van &x){ //加法的底层
        rg sign=0; if(x.tt>tt)tt=x.tt;
        for(rg i=0;i<=tt;++i){
            s[i]+=x.s[i]+sign; sign=0;
            if(s[i]>mod)s[i]-=mod,sign=1;
        }if(sign)s[++tt]=1;
    }

    inline void cut(const Van &x){ //减法的底层
        if(fu)cout<<54564<<endl;
        rg sign=0; 
        for(rg i=0;i<=tt;++i){
            s[i]-=x.s[i]+sign; sign=0;
            if(s[i]<0)s[i]+=mod,sign=1;
        }while(tt&&!s[tt])--tt;
        if(!tt&&!s[tt]) fu=0;
    }

    inline void mul(const Van &x){ //乘法的底层
        Van y; ll num; y.tt=tt+x.tt;
        for(rg i=0;i<=tt;++i){ num=0;
            for(rg j=0;j<=x.tt;++j){
                num=(ll)s[i]*x.s[j]+y.s[j+i]+num;
                y.s[j+i]=num%mod; num/=mod;
            } if(num)y.s[x.tt+i+1]=num;
        }if(num)++y.tt;; *this=y;
    }

    inline void operator +=(Van x){ //赋值加法重载
        if(fu==x.fu){(*this).add(x); return;}
        if(*this>x) (*this).cut(x);
        else x.cut(*this),*this=x,fu^=1;
    }
    inline Van operator +(const Van &x){ //加法正常重载
        Van y=*this; y+=x; return y;
    }
    
    inline void operator -=(Van x){ //赋值减法重载
        if(fu!=x.fu){(*this).add(x); return;}
        if(*this>x){(*this).cut(x); return;}
        x.cut(*this); *this=x; if(s[tt])fu^=1;
    }
    inline Van operator -(const Van &x){ //减法正常重载
        Van y=*this; y-=x; return y;
    }
    
    inline void operator *=(Van x){ //赋值乘法重载
        if(!s[tt]||!x.s[x.tt]){Van y; *this=y;}
        if(fu!=x.fu)fu=1;else fu=0;; (*this).mul(x);
    }
    inline Van operator *(const Van &x){ //乘法正常重载
        Van y=*this; y*=x; return y;
    }

    inline Van operator *(int x){  Van y; y=x; return (*this)*y;}
    inline void operator *=(int x){Van y; y=x; (*this)*=y;}
    inline Van operator +(int x){  Van y; y=x; return (*this)+y;}
    inline void operator +=(int x){Van y; y=x; (*this)+=y;}
    inline Van operator -(int x){  Van y; y=x; return (*this)-y;}
    inline void operator -=(int x){Van y; y=x; (*this)-=y;}
};

猜你喜欢

转载自www.cnblogs.com/812-xiao-wen/p/10901972.html