跳到正文

个人记录

美味的高精

li_cat约 3 分钟
原文

这是 2025年9月26日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年9月25日
收录
2025年12月12日
更新
2026年10月31日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

写了个压位高精vector

一遍过,代码美妙,遂记之

C++
const int BASE=1e9;

struct BIGINT{
	vector<ll> a;
	BIGINT (){
		a.push_back(0);
	}
	BIGINT (ll x){
		if(!x) a.push_back(0);
		while(x){
			a.push_back(x%BASE);
			x/=BASE;
		}
	}
	BIGINT operator+(BIGINT x){
		BIGINT res(0);
		for(int i=0;i<max(a.size(),x.a.size());++i){
			res.a.push_back(0);
			if(i<a.size()) res.a[i]+=a[i];
			if(i<x.a.size()) res.a[i]+=x.a[i];
			res.a[i+1]+=res.a[i]/BASE;
			res.a[i]%=BASE;
		}
		while(res.a.size()>1 && !res.a[res.a.size()-1]) res.a.pop_back();
		return res;
	}
	BIGINT operator*(BIGINT x){
		BIGINT res(0);
		while(res.a.size()<a.size()+x.a.size()) res.a.push_back(0);
		for(int i=0;i<a.size();++i)
			for(int j=0;j<x.a.size();++j){
				res.a[i+j]+=a[i]*x.a[j];
				res.a[i+j+1]+=res.a[i+j]/BASE;
				res.a[i+j]%=BASE;
			}
		for(int i=0;i<res.a.size();++i){
			res.a[i+1]+=res.a[i]/BASE;
			res.a[i]%=BASE;
		}
		while(res.a.size()>1 && !res.a[res.a.size()-1]) res.a.pop_back();
		return res;
	} 
	bool operator<(BIGINT x){
		if(a.size()!=x.a.size()) return a.size()<x.a.size();
		for(int i=a.size()-1;i>=0;--i)
			if(a[i]!=x.a[i])
				return a[i]<x.a[i];
		return 0;
	}
	void print(){
		reverse(a.begin(),a.end());
		printf("%lld",a[0]);
		for(int i=1;i<a.size();++i) printf("%09lld",a[i]);
		reverse(a.begin(),a.end());
	}
};

BIGINT operator+(BIGINT x,ll y){return x+BIGINT(y);}
BIGINT operator+(ll y,BIGINT x){return x+BIGINT(y);}
BIGINT operator*(BIGINT x,ll y){return x*BIGINT(y);}
BIGINT operator*(ll y,BIGINT x){return x*BIGINT(y);}
BIGINT max(BIGINT x,BIGINT y){
	if(x<y) return y;
	return x;
}//