其他分享
首页 > 其他分享> > 高精度

高精度

作者:互联网

有个巨佬学长(zyf),写的一份高精,蛮好用的。

放出来以后方便套板子,啥都有,直接用就行。

以下为代码

namespace zyf{
	struct bign{
		static const int maxlen=200,width=8;
		static const long long limit=100000000LL;
		long long len,bit[maxlen];
		long long& operator[](int p){ return bit[p]; }
		void ClearBit(){ memset(bit,0,sizeof(bit)); }
		void Delete0(){ for(;!bit[len-1] && len>1;--len); }
		
		bign(int p=0){ *this=p; }
		bign& operator=(int p){
			ClearBit();
			len=p?0:1; for(;p;p/=limit) bit[len++]=p%limit;
			return *this;
		}
		bign(const char *p){ *this=p; }
		
		bign& operator+=(bign b){
			len=max(len,b.len)+1;
			for(int i=0;i<len;++i) bit[i]+=b[i],bit[i+1]+=bit[i]/limit,bit[i]%=limit;
			Delete0(); return *this;
		}
		bign& operator-=(bign b){
			for(int i=0;i<len;++i){ bit[i]-=b[i]; if(bit[i]<0) bit[i]+=limit,--bit[i+1]; }
			Delete0(); return *this;
		}
		bign& operator*=(bign b){
			bign a=*this; ClearBit(); len=a.len+b.len;
			for(int i=0;i<a.len;++i)
				for(int j=0;j<b.len;++j)
					bit[i+j]+=a[i]*b[j],bit[i+j+1]+=bit[i+j]/limit,bit[i+j]%=limit;
			Delete0(); return *this;
		}
		bign& operator*=(int b){
			++len; long long t=0;
			for (int i=0;i<len;++i)
				bit[i]=t+bit[i]*b,t=bit[i]/limit,bit[i]%=limit;
			Delete0(); return *this;
		}
		
		bign& operator/=(int b){
			for(int i=len-1;i>0;--i) bit[i-1]+=limit*(bit[i]%b),bit[i]/=b; bit[0]/=b;
			Delete0(); return *this;
		}
		
		bool operator<(bign b) const{
			if(len>b.len) return false;
			if(len<b.len) return true;
			for(int i=len-1;i>=0;--i)
				if(bit[i]!=b[i]) return bit[i]<b[i];
			return bit[0]<b[0];
		}
		bool operator==(bign b) const{ return !(*this<b) && !(b<*this); }
		bool operator!=(bign b) const{ return !(*this==b); }
		bool operator>(bign b) const{ return !(*this<b) && !(*this==b); }
		bool operator<=(bign b) const{ return *this<b || *this==b; }
		bool operator>=(bign b) const{ return *this>b || *this==b; }
		
		bool odd(){ return bit[0]%2==1; }
		bool even(){ return bit[0]%2==0; }
	};
	bign operator+(bign a,bign b){ return a+=b; }
	bign operator-(bign a,bign b){ return a-=b; }
	bign operator*(bign a,bign b){ return a*=b; }
	bign operator*(bign a,int b){ return a*=b; }
	bign operator/(bign a,int b){ return a/=b; }
	istream& operator>>(istream &is,bign &p){
		string s; is>>s; p=s.c_str();
		return is;
	}
	ostream& operator<<(ostream &os,bign p){
		os.fill('0'); os<<p.bit[p.len-1];
		for(int i=p.len-2;i>=0;--i){ os.width(bign::width); os<<p.bit[i]; }
		return os;
	}
	bign sqrt(bign x){
		bign head=1,tail=x;
		while(head<=tail){
			bign mid=(head+tail)/2;
			if(x<mid*mid) tail=mid-1; else head=mid+1;
		}
		return tail;
	}
	bign gcd(bign a,bign b){
		bign gcd=1;
		while(a!=b){
			if(a<b) swap(a,b);
			if(a.even() && b.even()) gcd*=2,a/=2,b/=2;
			else if(a.even() && b.odd()) a/=2;
			else if(a.odd() && b.even()) b/=2;
			else a=a-b;
		}
		return gcd*=a;
	}
}
using zyf::bign;

使用方法:使用 bign 定义高精度,maxlen 代表最大位数。

标签:return,高精度,int,len,bign,operator,bit
来源: https://www.cnblogs.com/LAK666/p/16438026.html