只支持非负数,用 vectoer 实现。
#include<bits/stdc++.h>
using namespace std;
using ll=unsigned long long;
using bigint=unsigned long long;
using bigbig=__int128;
struct bign{
static const int block=14;
static const ll base=pow(10,block);
vector<ll>a;
bign(ll x=0){
for(;x;x/=base)a.push_back(x%base);
}
bign(string s){
reverse(s.begin(),s.end());
for(int i=0;i<(int)s.length();i+=block){
int r=min((int)s.length(),i+block),x=0;
for(int j=r-1;j>=i;j--)x=x*10+s[j]-48;
a.push_back(x);
}
}
bign operator = (ll x){
return *this=bign(x);
}
bign operator = (string s){
return *this=bign(s);
}
void resize(int len){
a.assign(len,0);
}
ll operator [] (const int &x)const{
return a[x];
}
ll& operator [] (const int &x){
return a[x];
}
friend istream& operator >> (istream& in,bign &x){
string s;
in>>s,x=s;
return in;
}
friend ostream& operator << (ostream& out,const bign &x){
if(x.a.empty())out<<"0";
else{
ll ed=x.a.back();
printf("%llu",ed);
for(int i=x.a.size()-2;~i;i--)printf("%0*llu",block,x[i]);
}
return out;
}
bool operator < (const bign &x)const{
if(a.size()!=x.a.size())return a.size()<x.a.size();
for(int i=a.size()-1;~i;i--)if(a[i]^x[i])return a[i]<x[i];
return 0;
}
bool operator == (const bign &x)const{
if(a.size()!=x.a.size())return 0;
for(int i=0;i<(int)a.size();i++)if(a[i]^x[i])return 0;
return 1;
}
bool operator != (const bign &x)const{
return !(*this==x);
}
bool operator > (const bign &x)const{
return x<*this;
}
bool operator <= (const bign &x)const{
if(a.size()!=x.a.size())return a.size()<x.a.size();
for(int i=a.size()-1;~i;i--)if(a[i]^x[i])return a[i]<x[i];
return 1;
}
bool operator >= (const bign &x)const{
return x<=*this;
}
bign operator += (const bign &x){
ll r=0;
for(int i=0;i<(int)x.a.size()||r;i++){
if(i<(int)x.a.size())r+=x[i];
if(i>=(int)a.size())a.push_back(0);
if((a[i]+=r)>=base)r=1,a[i]-=base;
else r=0;
}
return *this;
}
bign operator + (const bign &x)const{
bign t=*this;
return t+=x;
}
bign operator -= (const bign &x){
ll r=0;
for(int i=0;i<(int)x.a.size()||r;i++){
if(i<(int)x.a.size())r+=x[i];
if(a[i]>=r)a[i]-=r,r=0;
else a[i]+=base-r,r=1;
}
for(;!a.empty()&&!a.back();)a.pop_back();
return *this;
}
bign operator - (const bign &x)const{
bign t=*this;
return t-=x;
}
bign operator -= (const ll &_x){
ll r=0;
ll x=_x;
for(int i=0;x||r;i++){
r+=x%base,x/=base;
if(a[i]>=r)a[i]-=r,r=0;
else a[i]+=base-r,r=1;
}
for(;!a.empty()&&!a.back();)a.pop_back();
return *this;
}
bign operator - (const ll &x)const{
bign t=*this;
return t-=x;
}
bign operator * (const bign &x)const{
int len=a.size()+x.a.size()+1;
vector<bigbig>t(a.size()+x.a.size()+1);
for(int i=0;i<(int)a.size();i++)for(int j=0;j<(int)x.a.size();j++){
int k=i+j;
t[k]+=(bigbig)a[i]*x[j],t[k+1]+=t[k]/base,t[k]%=base;
}
bign ans;
ans.resize(len);
for(int i=0;i<(int)t.size();i++){
if(i+1<(int)t.size())t[i+1]+=t[i]/base;
ans[i]=t[i]%base;
}
for(;!ans.a.empty()&&!ans.a.back();)ans.a.pop_back();
return ans;
}
bign operator *= (const bign &x){
return *this=*this*x;
}
bign operator *= (const int &x){
bigint r=0;
for(int i=0;i<(int)a.size()||r;i++){
if(i>=(int)a.size())a.push_back(0);
r+=x*(bigint)a[i],a[i]=r%base,r/=base;
}
return *this;
}
bign operator * (const int &x)const{
bign t=*this;
return t*=x;
}
bign operator *= (const ll &x){
bigint r=0;
for(int i=0;i<(int)a.size()||r;i++){
if(i>=(int)a.size())a.push_back(0);
r+=x*(bigint)a[i],a[i]=r%base,r/=base;
}
return *this;
}
bign operator * (const ll &x)const{
bign t=*this;
return t*=x;
}
bign operator /= (const ll &x){
bigint r=0;
for(int i=a.size()-1;~i;i--){
r=r*base+a[i];
a[i]=r/x,r%=x;
}
for(;!a.empty()&&!a.back();)a.pop_back();
return *this;
}
bign operator / (const ll &x)const{
bign t=*this;
return t/=x;
}
friend bign qpow(const bign &_x,const ll &_y){
bign x(_x),ans(1);
for(ll y=_y;y;y>>=1,x*=x)if(y&1)ans*=x;
return ans;
}
ll trans()const{
ll x=0;
for(int i=a.size()-1;~i;i--)x=x*base+a[i];
return x;
}
};
int main(){
return 0;
}
标签:return,--,ll,zhengjun,int,bign,operator,const,模板
From: https://www.cnblogs.com/A-zjzj/p/17191659.html