【高精度】模板

 自己上学期花了三天打的,long long压9位,支持进制转换,加减乘除,字符串初始化,运算等等,应该无敌了

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; 

//注意最低位是从0开始计算的! 

typedef long long LL;
const int max_size=240;
const int L=9;
const int inc=10;
const LL base=1000000000; //1 E L

char buf[20];
int len;

void write(LL x, bool fill)
{
	while(x>0)
		buf[len++]=x%inc+'0',x/=inc;
	if(fill)
		while(len<L)
			buf[len++]='0';
	while(len)
		putchar(buf[--len]);
}

struct hugeint
{
	LL d[max_size],l;
	bool sig;
	
	inline bool equals_zero() const
	{
		return l==0;
	}
	
	void operator = (const char *p)
	{
		int f=strlen(p);
		const char *q=p;
		sig=false;
		if(p[0]=='-')
		{
			--f;
			++q;
			sig=true;
		}
		l=f/L;
		for(int i=0,tmp;i<l;i++)
		{
			d[i]=0;
			for(int j=L;j>=1;j--)
				d[i]=d[i]*inc+q[f-i*L-j]-'0';
		}
		if(f%L)
		{
			d[l]=0;
			for(int i=L*l;i<f;i++)
				d[l]=d[l]*inc+q[i-L*l]-'0';
			l++;
		}
		while(l>0&&d[l-1]==0)
			--l;
	}
	
	void operator = (const hugeint &t)
	{
		l=t.l,sig=t.sig;
		for(int i=0;i<l;i++)
			d[i]=t.d[i];
	}
	
	void operator = (char *p)
	{
		const char *x=p;
		*this=x;
	}
	
	template<class T1>
	void operator = (T1 x)
	{
		char str[max_size*L];
		int t=0;
		T1 y=x;
		while(y!=0)
			y/=inc,++t;
		str[t]='\0';
		if(x<0)
			str[0]='-',t++,x=-x;
		while(x!=0)
		{
			str[--t]=x%inc+'0';
			x/=inc;
		}
		*this=str; 
	}
	
	template<class T1>
	hugeint(const T1 &x)
	{
		*this=x;
	}
	
	hugeint(){}
	
	bool operator == (const hugeint &t) const
	{
		if((sig^t.sig)&&(l>0||t.l>0)||l!=t.l)
			return false;
		for(int i=0;i<l;i++)
			if(d[i]!=t.d[i])
				return false;
		return true;
	}
	
	bool operator < (const hugeint &t) const
	{
		if(sig&&!t.sig)
			return true;
		if(l<t.l)
			return sig^true;
		if(l>t.l)
			return sig^false;
		int i;
		for(i=l-1;i>=0&&t.d[i]==d[i];i--);
		if(i<0)
			return false;
		if(d[i]<t.d[i])
			return sig^true;
		if(d[i]>t.d[i])
			return sig^false;
	}
	
	bool operator > (const hugeint &t) const
	{
		return t<*this;
	}
	
	bool operator <= (const hugeint &t) const
	{
		return *this<t||*this==t;
	}
	
	bool operator >= (const hugeint &t) const
	{
		return t<=*this;
	}
	
	hugeint operator - () const
	{
		hugeint res=*this;
		res.sig^=true;
		return res;
	}
	
	hugeint abs() const
	{
		hugeint res=*this;
		res.sig=false;
		return res;
	}
	
	template<class T1>
	hugeint operator << (T1 k) const
	{
		hugeint res=*this;
		res.l+=k;
		for(int i=res.l-1;i>=k;i--)
			res.d[i]=res.d[i-k];
		for(int i=k-1;i>=0;i--)
			res.d[i]=0;
		return res;
	}
	
	template<class T1>
	hugeint operator >> (T1 k) const
	{
		hugeint res=*this;
		res.l=max(res.l-k,(LL)0);
		for(int i=0;i<res.l;i++)
			res.d[i]=res.d[i+k];
		return res;
	}
	
	hugeint operator + (const hugeint &t) const
	{
		hugeint res;
		res.sig=sig;
		res.l=max(l,t.l);
		LL c=0,k;
		if(t.sig^sig)
		{
			k=this->abs()<t.abs()?1:-1;
			res.sig^=k==1;
			for(int i=0;i<res.l;i++)
			{
				res.d[i]=((i<t.l?t.d[i]:0)-(i<l?d[i]:0))*k-c;
				c=res.d[i]<0;
				res.d[i]+=c*base;
			}
			while(res.l>0&&res.d[res.l-1]==0)
				--res.l;
			return res;
		}
		for(int i=0;i<res.l;i++)
		{
			res.d[i]=(i<l?d[i]:0)+(i<t.l?t.d[i]:0)+c;
			c=res.d[i]>=base;
			res.d[i]-=c*base;
		}
		if(c)
			res.d[res.l++]=c;
		return res;
	}
	
	hugeint operator - (const hugeint &t) const
	{
		return *this+(-t);
	}
	
	hugeint operator * (const hugeint &t) const
	{			
		hugeint res;
		if(t.equals_zero()||this->equals_zero())
		{
			res.l=0;
			return res;
		}
		res.sig=sig^t.sig;
		res.l=l+t.l-1;
		LL c;
		for(int i=0;i<=res.l;i++)
			res.d[i]=0;
		for(int i=0;i<l;i++)
		{
			c=0;
			for(int j=0;j<t.l;j++)
			{
				res.d[i+j]=res.d[i+j]+d[i]*t.d[j]+c;
				c=res.d[i+j]/base;
				res.d[i+j]%=base;
			}
			if(c)
				res.d[i+t.l]=c;
		}
		if(res.d[res.l])
			++res.l;
		while(res.l>0&&res.d[res.l-1]==0)
			--res.l;
		return res;
	}
	
	hugeint operator / (const hugeint &t) const
	{
		hugeint res,ss=t.abs();
		res.sig=sig^t.sig;
		if(t.equals_zero()||this->abs()<ss)
			res.l=0;
		else
		{
			res.l=l-t.l+1;
			LL le,re,me;
			hugeint r=*this>>(res.l-1);
			for(int i=res.l-1;i>=0;i--)
			{
				le=((r.l>=t.l?r.d[t.l-1]:0)+(r.l>t.l?r.d[t.l]*base:0))/(t.d[t.l-1]+1);
				re=((r.l>=t.l?r.d[t.l-1]:0)+(r.l>t.l?r.d[t.l]*base:0)+1)/t.d[t.l-1];
				while(le<re)
				{
					me=re+le+1>>1;
					if(r<t*me)
						re=me-1;
					else
						le=me;
				}
				res.d[i]=le;
				r=(r-ss*le<<1)+d[i-1];
			}
			if(res.d[res.l-1]==0)
				--res.l;
		}
		return res;
	}
	
	hugeint operator % (const hugeint &x) const
	{
		hugeint tmp=this->abs()/x*x;
		return this->sig?*this+tmp+x.abs():*this-tmp;
	}
	
	void print()
	{
		if(l==0)
		{
			puts("0");
			return ;
		}
		if(sig)
			putchar('-');
		write(d[l-1],false);
		for(int i=l-2;i>=0;i--)
			write(d[i],true);
		puts("");
	}
}A,B,C,M;

char s1[max_size*L],s2[max_size*L];
int n,tot;

int main()
{
/*	A="65465468468468468468";
	B=-546546464;
	((A%B*54641-5151+454654)*"64542434534654654").print(); */
//	scanf("%s",s1);
	A="11281783718378173818378138797897987987987987099898989008098098718978978976748747489679847698478967984768479674986749876984769874986794798674986798479864789666666666666666666666666666666999999999947867487587475874858485748758748578478574875847857487584758748578475847857487584785787817837988979878998789798780987988888888888888888888888888888888888888888888978798798798789798798787879767165356156261526617261979381983018209189281987267467627647237819820182091000000000000000000000000000000000000009187291729817982711281783718378173818378138797897987987987987099898989008098098718978978976748747489679847698478967984768479674986749876984769874986794798674986798479864789666666666666666666666666666666999999999947867487587475874858485748758748578478574875847857487584758748578475847857487584785787817837988979878998789798780987988888888888888888888888888888888888888888888978798798798789798798787879767165356156261526617261979381983018209189281987267467627647237819820182091000000000000000000000000";
	B="8679479867498679847986478966666666666666666666666666666699999999994786748758747587485848574875874857847857487584785748758475874857847584785748758478578781783798897987899878979878098798888888888888888888888888888888888888888888897879879879878979879878787976716535615626152661726197938198301820918928198726746762764723781982018209100000000000000000000000000000000000000918729172981798271128178371837817381837813879789798798798798709989898900809809871897897897674874748967984769847896798476847967498674987698476";
	C=A;
	int k=500;
	while(k--)
		A/B;
	printf("f"); 
/*	while(B+1<C)
	{
		M=(B+C)*(base/2)>>1;
		if(M*M>A)
			C=M;
		else
			B=M;
	}
	B.print();*/
	return 0;
}

猜你喜欢

转载自blog.csdn.net/BUAA_Alchemist/article/details/84949216