[模板] 高精度加/减/乘

//tbig[0]: the length of num

const int bsz=20050,blk=10000;
struct tbig{
	int val[bsz];
	tbig(){cl();}
	tbig(int v){set(v);}
	void cl(){memset(val,0,sizeof(val));}
	void set(int v){
		cl();
		while(v){val[++val[0]]=v%blk;v/=blk;}
	}
	int& operator[](int p){return val[p];}
	const int& operator[](int p)const{return val[p];}
	void pr(){
		printf("%d",val[val[0]]);
		repdo(i,val[0]-1,1){
			printf("%04d",val[i]);
		}
	}
};
typedef const tbig& ftbig;
tbig operator+(ftbig l,ftbig r){
	tbig res;
	res[0]=max(l[0],r[0]);
	rep(i,1,res[0]){
		res[i]+=l[i]+r[i];
		if(res[i]>blk)res[i]-=blk,res[i+1]++;
	}
	while(res[res[0]+1])++res[0];
	return res;
}
tbig operator*(ftbig l,ftbig r){//no fft,no bugs
	tbig res;
	rep(i,1,l[0]){
		rep(j,1,r[0]){
			res[i+j-1]+=l[i]*r[j];
			if(res[i+j-1]>blk)res[i+j]+=res[i+j-1]/blk,res[i+j-1]%=blk;
		}
	}
	res[0]=l[0]+r[0]+1;
	while(res[0]&&res[res[0]]==0)--res[0]; //important
	return res;
}

举个栗子: 求 \(A_a^b\)

tbig a(int a,int b){
	if(b>a)return 0; //important
	tbig res(1);
	rep(i,1,b)res=res*(a-i+1);
	return res;
}
posted @ 2018-10-23 19:49  Ubospica  阅读(137)  评论(0编辑  收藏  举报