压位高精度模板(加,减,乘)

struct bignum{
	#define base 10000
	int a[20000];
	bignum(){memset(a,0,sizeof(a));}
    inline void init(){memset(a,0,sizeof(a));a[0]=1;a[1]=1;}
	inline void read(){
        static char s[1000];scanf("%s",s);
        int n=strlen(s),len=1;
		for(int i=1,k=1;i<=n;i++){
			if(k==10000){len++;k=1;}
			a[len]+=k*(s[n-i]^48);
			k=(k<<1)+(k<<3);
		}a[0]=len;		
	}
    inline bignum trans(int b){
        bignum ans;
        while(b){ans.a[++ans.a[0]]=b%base;b/=base;}
        return ans;
    }
    bignum operator+(const bignum &b){
		bignum ans;
		for(int i=1,x=0,tmp=0;x||i<=a[0]||i<=b.a[0];++i){
			tmp=a[i]+b.a[i]+x;
			ans.a[++ans.a[0]]=tmp%base;
			x=tmp/base;
		}
		return ans;
	}
    bignum operator-(const bignum &b){
		bignum ans;
		for(int i=1,x=0,tmp=0;i<=a[0];++i){
			tmp=a[i]-b.a[i]-x;
			if(tmp>=0) x=0;
			else tmp+=base,x=1;
			ans.a[++ans.a[0]]=tmp;
		}
		while(ans.a[0]>1&&ans.a[ans.a[0]]==0) ans.a[0]--;
		return ans;
	}
    bignum operator*(const bignum &b){
        bignum ans;ans.a[0]=a[0]+b.a[0];
        for(int i=1;i<=a[0];++i){
            for(int j=1,x=0;x||j<=b.a[0];++j){
                ans.a[i+j-1]+=x+a[i]*b.a[j];
                x=ans.a[i+j-1]/base;
                ans.a[i+j-1]%=base;
            }
        }
        while(ans.a[0]>1&&ans.a[ans.a[0]]==0) ans.a[0]--;
        return ans;
    }
    bignum operator*(const int &b){
    	bignum ans;
    	for(int i=1,x=0,tmp=0;x||i<=a[0];++i){
    		tmp=a[i]*b+x;
    		ans.a[++ans.a[0]]=tmp%base;
    		x=tmp/base;
		}
		return ans;
	}
	friend bool operator<(const bignum &x,const bignum &y){
		if(x.a[0]^y.a[0]) return x.a[0]<y.a[0];
		for(int i=x.a[0];i>=1;--i) if(x.a[i]^y.a[i]) return x.a[i]<y.a[i];
		return false;
	}
    friend bool operator>(const bignum &x,const bignum &y){return y<x;}
    friend bool operator==(const bignum &x,const bignum &y){return !(x>y)&&!(x<y);}
	inline void print(){put_int(a[a[0]]);for(int i=a[0]-1;i>=1;i--)printf("%04d",a[i]);putchar('\n');}
	#undef base
};
posted @ 2023-06-12 18:34  Flandreqwq  阅读(42)  评论(3编辑  收藏  举报