高精度(压位+判负数+加减乘+读写)

本算法使用压位(9位)

目前支持负数的加减乘,以及快速读写


struct gj{
	
	bool fu; //是否是负数
	int tt,mod; //高精的长度
    int s[40005]; //压位用的数组
	
	inline gj(){ //整体初始化
		fu=0; tt=0; mod=1e9;
		memset(s,0,sizeof(s));
	}
    
    inline gj 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 gj &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 gj &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 gj 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 gj 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 gj &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 gj &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 gj &x){ //乘法的底层
		gj 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 +=(gj x){ //赋值加法重载
		if(fu==x.fu){(*this).add(x); return;}
		if(*this>x) (*this).cut(x);
		else x.cut(*this),*this=x,fu^=1;
    }
    inline gj operator +(const gj &x){ //加法正常重载
		gj y=*this; y+=x; return y;
    }
	
	inline void operator -=(gj 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 gj operator -(const gj &x){ //减法正常重载
		gj y=*this; y-=x; return y;
    }
    
    inline void operator *=(gj x){ //赋值乘法重载
		if(!s[tt]||!x.s[x.tt]){gj y; *this=y;}
		if(fu!=x.fu)fu=1;else fu=0;; (*this).mul(x);
    }
    inline gj operator *(const gj &x){ //乘法正常重载
		gj y=*this; y*=x; return y;
    }

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

posted @ 2019-05-21 19:54  一只不咕鸟  阅读(601)  评论(2编辑  收藏  举报