跳到正文

个人记录

高精模板

cll6约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年5月8日
更新
2026年5月8日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1
C++
struct long_int{
	string str="0";
	int len=1,ng=0;
	string NumSum(string a,string b){
		string c="";
		for(int i=a.size()-1,j=b.size()-1,t=0;t||i>=0||j>=0;t=((i>=0?(int)a[i]:48)+(j>=0?(int)b[j]:48)-96+t)/10,i--,j--)
			c=(char)(((i>=0?(int)a[i]:48)+(j>=0?(int)b[j]:48)-96+t)%10+48)+c;
		return c;
	}
	string NumSub(string a,string b){
		if(a==b)return "0";
		string c="",zr="";
		for(int i=a.size()-1,j=b.size()-1,t=0;i>=0||j>=0;t=((int)a[i])-t<(j>=0?(int)b[j]:48),i--,j--)
			if(((int)a[i]-t-(j>=0?(int)b[j]:48)+10)%10==0)zr=zr+"0";
			else c=(char)(((int)a[i]-t-(j>=0?(int)b[j]:48)+10)%10+48)+zr+c,zr="";

		return c;
	}
	string NumPro(string a,string b){
		if(a=="0"||b=="0")return "0";
		string c="";
		for(int i=a.size()-1;i>=0;i--)for(int j=b.size()-1,t=0,d;t||j>=0;j--)
		if(c.size()<a.size()+b.size()-i-j-1)c=(char)((((int)a[i]-48)*(j>=0?((int)b[j]-48):0)+t)%10+48)+c,t=(((int)a[i]-48)*(j>=0?((int)b[j]-48):0)+t)/10;
		else d=(int)c[c.size()-a.size()-b.size()+i+j+1]-48,c[c.size()-a.size()-b.size()+i+j+1]=(char)((((int)a[i]-48)*(j>=0?((int)b[j]-48):0)+t+d)%10+48),
		t=(((int)a[i]-48)*(j>=0?((int)b[j]-48):0)+t+d)/10;
		return c;
	}
	bool StringCmp(string a,string b){
		return (a.size()==b.size()?(a<b):(a.size()<b.size()));
	}
	//比较符
	friend const bool operator < (const long_int &L,const long_int &R){
		if(L.ng!=R.ng)return R.ng>L.ng;
		if(L.len!=R.len)return L.ng==1?R.len>L.len:L.len>R.len;
		return L.ng==1?R.str>L.str:L.len>R.len;
	}
	friend const bool operator > (const long_int &L,const long_int &R){return R<L;}
	friend const bool operator <= (const long_int &L,const long_int &R){return !(L>R);}
	friend const bool operator >= (const long_int &L,const long_int &R){return !(L<R);}
	friend const bool operator == (const long_int &L,const long_int &R){return (L>=R)&&(L<=R);}
	friend const bool operator != (const long_int &L,const long_int &R){return !(L==R);}
	//运算符
	friend const long_int operator + (const long_int &L,const long_int &R){
		long_int SUM;
		string NUM;
		int NG,LEN;
		if(L.ng==R.ng)NUM=SUM.NumSum(L.str,R.str),NG=L.ng==0?R.ng:L.ng;
		else if(L.ng!=R.ng)
		NUM=(SUM.StringCmp(R.str,L.str))?SUM.NumSub(L.str,R.str):SUM.NumSub(R.str,L.str),
		NG=(!L.ng||!R.ng)?(L.ng+R.ng):(SUM.StringCmp(L.str,R.str)?-1:1);
		LEN=NUM.size();
		SUM.str=NUM,SUM.len=LEN,SUM.ng=NG;
		return SUM;
	}
	friend const long_int operator - (const long_int &L,const long_int &R){     //这是减号
		long_int SUB;
		string NUM;
		int NG,LEN;
		if(L.ng==R.ng)NUM=SUB.StringCmp(R.str,L.str)?SUB.NumSub(L.str,R.str):SUB.NumSub(R.str,L.str),
		NG=L.str==R.str?0:(SUB.StringCmp(L.str,R.str)?-1:1);
		else if(L.ng!=R.ng)NUM=SUB.NumSum(L.str,R.str),NG=L.ng?L.ng:-R.ng;
		LEN=NUM.size();
		SUB.str=NUM,SUB.len=LEN,SUB.ng=NG;
		return SUB;
	}
	friend const long_int operator - (const long_int &NUM){    					//这是负号
		long_int ANS=NUM;
		ANS.ng=-NUM.ng;
		return ANS;
	}
	friend const long_int operator * (const long_int &L,const long_int &R){
		long_int PRO;
		string NUM;
		int NG,LEN;
		NG=L.ng*R.ng;
		NUM=PRO.NumPro(L.str,R.str);
		LEN=NUM.size();
		PRO.str=NUM,PRO.len=LEN,PRO.ng=NG;
		return PRO;
	}
	//输入输出
	friend istream &operator >> (istream &in,long_int &LONG_INT){
		string IN;
	    in>>IN;
	    LONG_INT.ng=(IN=="0")?0:(IN[0]=='-'?-1:1);
	    if(IN[0]=='-')IN.erase(0,1);
	    LONG_INT.str=IN,LONG_INT.len=IN.size();
		return in;
	}
	friend ostream &operator << (ostream &out,long_int &LONG_INT){
	    out<<(LONG_INT.ng==-1?"-":"")+LONG_INT.str;
		return out;
	}
};
原文