高精度板子
chenyongxi3 · · 个人记录
高精度板子,
class BigInt{
public:
static constexpr int P=998244353,G=3,BASE=1000000000,NTT_MAX=1<<23;
static long long qpow(long long a,long long b){long long r=1;for(;b;b>>=1,a=a*a%P)if(b&1)r=r*a%P;return r;}
static void ntt(std::vector<long long>&a,int inv){
if(a.empty()||a.size()>NTT_MAX||(a.size()&(a.size()-1)))throw std::invalid_argument("Invalid NTT length");
int n=a.size();for(int i=1,j=0;i<n;++i){int b=n>>1;for(;j&b;b>>=1)j^=b;j^=b;if(i<j)std::swap(a[i],a[j]);}
for(int l=2;l<=n;l<<=1){long long wN=qpow(G,(P-1)/l);if(inv)wN=qpow(wN,P-2);
for(int i=0;i<n;i+=l){long long w=1;for(int j=0;j<l/2;++j,w=w*wN%P){long long u=a[i+j],v=a[i+j+l/2]*w%P;a[i+j]=(u+v)%P;a[i+j+l/2]=(u-v+P)%P;}}}
if(inv){long long invN=qpow(n,P-2);for(long long&x:a)x=x*invN%P;}
}
std::vector<int>v;int sign;
BigInt():sign(0){}
BigInt(long long val):sign((val>0)-(val<0)){
auto mag=static_cast<unsigned long long>(val);if(val<0)mag=0ULL-mag;
for(;mag;mag/=BASE)v.push_back(mag%BASE);
}
BigInt(std::string s):sign(1){
std::size_t start=!s.empty()&&(s[0]=='+'||s[0]=='-');if(start&&s[0]=='-')sign=-1;
if(start==s.size())throw std::invalid_argument("Invalid BigInt");
for(std::size_t end=s.size();end>start;){std::size_t begin=end-start>9?end-9:start;int digit=0;
for(std::size_t i=begin;i<end;++i){if(s[i]<'0'||s[i]>'9')throw std::invalid_argument("Invalid BigInt");digit=digit*10+s[i]-'0';}
v.push_back(digit);end=begin;}
trim();
}
void trim(){while(!v.empty()&&v.back()==0)v.pop_back();if(v.empty())sign=0;}
static int abs_cmp(const BigInt&a,const BigInt&b){if(a.v.size()!=b.v.size())return a.v.size()<b.v.size()?-1:1;for(std::size_t i=a.v.size();i--;)if(a.v[i]!=b.v[i])return a.v[i]<b.v[i]?-1:1;return 0;}
static int cmp(const BigInt&a,const BigInt&b){if(a.sign!=b.sign)return a.sign<b.sign?-1:1;return abs_cmp(a,b)*a.sign;}
bool operator<(const BigInt&b)const{return cmp(*this,b)<0;}
bool operator==(const BigInt&b)const{return cmp(*this,b)==0;}
bool operator>(const BigInt&b)const{return b<*this;}
bool operator<=(const BigInt&b)const{return!(b<*this);}
bool operator>=(const BigInt&b)const{return!(*this<b);}
bool operator!=(const BigInt&b)const{return!(*this==b);}
static BigInt shift(BigInt a,int m){if(!a.sign)return a;if(m>0)a.v.insert(a.v.begin(),m,0);else if(m<0){auto drop=static_cast<std::size_t>(-1LL*m);if(drop>=a.v.size())return 0;a.v.erase(a.v.begin(),a.v.begin()+drop);}return a;}
static BigInt add_abs(const BigInt&a,const BigInt&b){BigInt c;c.sign=1;int carry=0;for(std::size_t i=0;i<std::max(a.v.size(),b.v.size())||carry;++i){carry+=(i<a.v.size()?a.v[i]:0)+(i<b.v.size()?b.v[i]:0);c.v.push_back(carry%BASE);carry/=BASE;}c.trim();return c;}
static BigInt sub_abs(const BigInt&a,const BigInt&b){BigInt c;c.sign=1;int borrow=0;for(std::size_t i=0;i<a.v.size();++i){int digit=a.v[i]-(i<b.v.size()?b.v[i]:0)-borrow;borrow=digit<0;c.v.push_back(digit+borrow*BASE);}c.trim();return c;}
BigInt operator+(const BigInt&b)const{
if(!sign)return b;
if(!b.sign)return*this;
if(sign==b.sign){BigInt c=add_abs(*this,b);c.sign=sign;return c;}
int order=abs_cmp(*this,b);if(!order)return 0;
BigInt c=order>0?sub_abs(*this,b):sub_abs(b,*this);c.sign=order>0?sign:b.sign;return c;
}
BigInt operator-()const{BigInt c=*this;c.sign=-c.sign;return c;}
BigInt operator-(const BigInt&b)const{return*this+(-b);}
static BigInt mul_naive(const BigInt&a,const BigInt&b){
if(!a.sign||!b.sign)return 0;
BigInt c;c.sign=a.sign*b.sign;c.v.resize(a.v.size()+b.v.size());
for(std::size_t i=0;i<a.v.size();++i){long long carry=0;for(std::size_t j=0;j<b.v.size()||carry;++j){long long t=c.v[i+j]+carry+(j<b.v.size()?1LL*a.v[i]*b.v[j]:0);c.v[i+j]=t%BASE;carry=t/BASE;}}
c.trim();return c;
}
static BigInt mul_karatsuba(BigInt a,BigInt b){int m=std::min(a.v.size(),b.v.size())/2;if(!m)return mul_naive(a,b);BigInt a0=a,a1=shift(a,-m),b0=b,b1=shift(b,-m);a0.v.resize(m);a0.trim();b0.v.resize(m);b0.trim();BigInt z0=a0*b0,z2=a1*b1,z1=(a0+a1)*(b0+b1)-z0-z2;return shift(z2,2*m)+shift(z1,m)+z0;}
private:
static void transform(std::vector<int>&a,const std::vector<int>&root,bool inv){
int n=a.size();
if(!inv){for(int k=n/2;k;k>>=1)for(int i=0;i<n;i+=2*k)for(int j=0;j<k;++j){
int u=a[i+j],v=a[i+j+k],sum=u+v,dif=u-v;a[i+j]=sum>=P?sum-P:sum;a[i+j+k]=1LL*(dif<0?dif+P:dif)*root[k+j]%P;}}
else{for(int k=1;k<n;k<<=1)for(int i=0;i<n;i+=2*k)for(int j=0;j<k;++j){
int u=a[i+j],v=1LL*a[i+j+k]*root[k+j]%P,sum=u+v,dif=u-v;a[i+j]=sum>=P?sum-P:sum;a[i+j+k]=dif<0?dif+P:dif;}
std::reverse(a.begin()+1,a.end());int invN=qpow(n,P-2);for(int&x:a)x=1LL*x*invN%P;}
}
template<int RAD> static void pack(const BigInt&a,std::vector<int>&out){
int pos=0;
if(RAD==100){for(std::size_t i=0;i<a.v.size();i+=2){int limb=a.v[i];for(int j=0;j<4;++j,limb/=100)out[pos++]=limb%100;
if(i+1==a.v.size()){out[pos++]=limb;break;}int next=a.v[i+1];out[pos++]=limb+10*(next%10);
next/=10;for(int j=0;j<4;++j,next/=100)out[pos++]=next%100;}}
else{for(int limb:a.v)for(int rem=BASE;rem>1;rem/=RAD,limb/=RAD)out[pos++]=limb%RAD;}
}
template<int RAD,int DIGITS> static BigInt convolve(const BigInt&a,const BigInt&b){
std::size_t len=(9*a.v.size()+DIGITS-1)/DIGITS+(9*b.v.size()+DIGITS-1)/DIGITS;
if(len-1>NTT_MAX)return mul_karatsuba(a,b);
int sz=1;while(static_cast<std::size_t>(sz)<len-1)sz<<=1;
std::vector<int>A(sz),B(sz),root(sz);root[1]=1;
for(int k=2;k<sz;k<<=1){int w=qpow(G,(P-1)/(2*k));for(int j=0;j<k;j+=2){root[k+j]=root[(k+j)/2];root[k+j+1]=1LL*root[k+j]*w%P;}}
pack<RAD>(a,A);pack<RAD>(b,B);transform(A,root,false);transform(B,root,false);
for(int i=0;i<sz;++i)A[i]=1LL*A[i]*B[i]%P;
transform(A,root,true);BigInt c;c.sign=a.sign*b.sign;c.v.reserve(a.v.size()+b.v.size());
long long carry=0,limb=0,place=1;
for(std::size_t i=0;i<len;++i){long long val=carry+(i<len-1?A[i]:0);carry=val/RAD;limb+=place*(val%RAD);place*=RAD;
if(place>=BASE){c.v.push_back(limb%BASE);limb/=BASE;place/=BASE;}}
if(limb)c.v.push_back(limb);
c.trim();return c;
}
public:
static BigInt mul_ntt(const BigInt&a,const BigInt&b){
if(!a.sign||!b.sign)return 0;
auto small=std::min(a.v.size(),b.v.size());
if(3*small<=(P-1)/(999*999))return convolve<1000,3>(a,b);
if((9*small+1)/2<=(P-1)/(99*99))return convolve<100,2>(a,b);
return convolve<10,1>(a,b);
}
BigInt operator*(const BigInt&b)const{if(!sign||!b.sign)return 0;if(v.size()<64||b.v.size()<64)return mul_naive(*this,b);if(v.size()<256||b.v.size()<256)return mul_karatsuba(*this,b);return mul_ntt(*this,b);}
private:
static void mul_small(BigInt&a,int val){
if(!val){a=0;return;}
long long carry=0;for(int&x:a.v){long long cur=1LL*x*val+carry;x=cur%BASE;carry=cur/BASE;}
for(;carry;carry/=BASE)a.v.push_back(carry%BASE);
}
static int div_small(BigInt&a,int val){long long rem=0;for(std::size_t i=a.v.size();i--;){long long cur=rem*BASE+a.v[i];a.v[i]=cur/val;rem=cur%val;}a.trim();return rem;}
using QR=std::pair<BigInt,BigInt>;
static constexpr int DIV_LEAF=128,DIV_BZ=320,DIV_NEWTON=8192;
static BigInt radix(int n){BigInt r;r.sign=1;r.v.resize(n+1);r.v.back()=1;return r;}
static BigInt low(const BigInt&a,int n){BigInt r;r.sign=a.sign;r.v.assign(a.v.begin(),a.v.begin()+std::min<int>(n,a.v.size()));r.trim();return r;}
static BigInt high(const BigInt&a,int n){if(n>=(int)a.v.size())return 0;BigInt r;r.sign=a.sign;r.v.assign(a.v.begin()+n,a.v.end());return r;}
static BigInt join(BigInt a,const BigInt&b,int n,int off=0){
int count=std::min(n,std::max(0,(int)b.v.size()-off));
a.v.insert(a.v.begin(),a.sign?n:count,0);if(count)std::copy_n(b.v.begin()+off,count,a.v.begin());a.sign=1;a.trim();return a;
}
static QR school(BigInt a,const BigInt&b){
if(abs_cmp(a,b)<0)return {BigInt(0),std::move(a)};
int n=b.v.size();if(n==1){int rem=div_small(a,b.v[0]);return {std::move(a),BigInt(rem)};}
BigInt q;q.sign=1;q.v.resize(a.v.size()-n+1);a.v.push_back(0);
unsigned long long lead=b.v.back(),next=b.v[n-2];
for(int i=(int)q.v.size()-1;i>=0;--i){
unsigned long long top=1ULL*a.v[i+n]*BASE+a.v[i+n-1],digit=top/lead,rem=top%lead;
if(digit>=BASE){digit=BASE-1;rem=top-digit*lead;}
while(rem<BASE&&digit*next>rem*BASE+a.v[i+n-2]){--digit;rem+=lead;}
unsigned long long borrow=0;
for(int j=0;j<n;++j){auto prod=digit*b.v[j]+borrow;int val=a.v[i+j]-int(prod%BASE);borrow=prod/BASE+(val<0);a.v[i+j]=val+(val<0?BASE:0);}
if(a.v[i+n]<(int)borrow){--digit;int carry=0;for(int j=0;j<n;++j){int val=a.v[i+j]+b.v[j]+carry;carry=val>=BASE;a.v[i+j]=val-carry*BASE;}}
q.v[i]=digit;
}
a.v.resize(n);a.trim();q.trim();return {std::move(q),std::move(a)};
}
static QR bz2(BigInt a,const BigInt&b){
int n=b.v.size();if(n<=DIV_LEAF||(n&1)||(int)a.v.size()-n+1<=DIV_LEAF/2)return school(std::move(a),b);
int half=n/2;BigInt bh=high(b,half),bl=low(b,half);
QR hi=bz3(high(a,half),b,bh,bl,half);
QR lo=bz3(join(std::move(hi.second),a,half),b,bh,bl,half);
return {join(std::move(hi.first),lo.first,half),std::move(lo.second)};
}
static QR bz3(BigInt a,const BigInt&b,const BigInt&bh,const BigInt&bl,int half){
BigInt top=high(a,half);QR qr;
if(abs_cmp(high(top,half),bh)<0)qr=bz2(std::move(top),bh);
else{qr.first.sign=1;qr.first.v.assign(half,BASE-1);qr.second=top-shift(bh,half)+bh;}
qr.second=join(std::move(qr.second),a,half)-qr.first*bl;
while(qr.second.sign<0){qr.second+=b;--qr.first;}
return qr;
}
static BigInt reciprocal(const BigInt&b,int prec){
int len=b.v.size(),width=std::min(len,prec+1);BigInt top=high(b,len-width);
if(width<len)++top;
if((int)top.v.size()>width)return radix(prec);
if(prec<=16)return school(radix(width+prec),top).first;
int half=(prec+2)/2;BigInt est=reciprocal(b,half),err=radix(width+half)-top*est;
return shift(est,prec-half)+high(est*err,width+2*half-prec);
}
static QR newton_step(BigInt a,const BigInt&b,const BigInt&inv,int scale){
if(abs_cmp(a,b)<0)return {BigInt(0),std::move(a)};
BigInt q=high(a*inv,scale),r=a-q*b;
if(abs_cmp(r,b)>=0){r=sub_abs(r,b);++q;}
return {std::move(q),std::move(r)};
}
static QR blocks(BigInt a,BigInt b,bool use_newton){
int len=b.v.size(),pad=0,quot=(int)a.v.size()-len+1;
if(!use_newton){int step=1;while((len-1)/step>=DIV_LEAF)step*=2;pad=(step-len%step)%step;
if(pad){a=shift(std::move(a),pad);b=shift(std::move(b),pad);len+=pad;}}
int width=use_newton?std::min(len,quot):len,scale=len+width+1;
BigInt inv=use_newton?reciprocal(b,width+1):BigInt(0);
if(quot<=width){QR qr=use_newton?newton_step(std::move(a),b,inv,scale):bz2(std::move(a),b);if(pad)qr.second=high(qr.second,pad);return qr;}
int off=(quot-1)/width*width;BigInt q,r=high(a,off+width);q.sign=1;q.v.resize(quot);
for(;off>=0;off-=width){BigInt cur=join(std::move(r),a,width,off);
QR qr=use_newton?newton_step(std::move(cur),b,inv,scale):bz2(std::move(cur),b);
std::copy(qr.first.v.begin(),qr.first.v.end(),q.v.begin()+off);r=std::move(qr.second);}
q.trim();if(pad)r=high(r,pad);return {std::move(q),std::move(r)};
}
static QR divide(BigInt a,BigInt b,int mode){
if(!b.sign)throw std::runtime_error("Div by 0");
if(abs_cmp(a,b)<0)return {BigInt(0),std::move(a)};
int as=a.sign,qs=as*b.sign;a.sign=b.sign=1;
if(b.v.size()==1){int rem=div_small(a,b.v[0]);if(a.sign)a.sign=qs;return {std::move(a),BigInt(1LL*as*rem)};}
int len=b.v.size(),quot=(int)a.v.size()-len+1;
if(mode<0){bool fast=(len>=DIV_NEWTON&">=len)||(len>=DIV_NEWTON/2&"/2>=len)||(len>=2*DIV_NEWTON&">=len/2);
mode=fast?2:std::min(len,quot)>=DIV_BZ?1:0;}
int norm=BASE/(b.v.back()+1);if(norm!=1){mul_small(a,norm);mul_small(b,norm);}
QR qr=mode?blocks(std::move(a),std::move(b),mode==2):school(std::move(a),b);
if(norm!=1)div_small(qr.second,norm);
if(qr.first.sign)qr.first.sign=qs;
if(qr.second.sign)qr.second.sign=as;
return qr;
}
public:
static QR divmod(BigInt a,BigInt b){return divide(std::move(a),std::move(b),-1);}
static BigInt div_naive(BigInt a,BigInt b){return divide(std::move(a),std::move(b),0).first;}
static BigInt div_bz(BigInt a,BigInt b){return divide(std::move(a),std::move(b),1).first;}
static BigInt div_newton(BigInt a,BigInt b){return divide(std::move(a),std::move(b),2).first;}
BigInt operator/(const BigInt&b)const{return divmod(*this,b).first;}
BigInt operator%(const BigInt&b)const{return divmod(*this,b).second;}
static std::string to_bin(BigInt a){std::string s;while(a.sign)s+=char('0'+div_small(a,2));return s;}
static BigInt from_bin(std::string s){BigInt a;for(auto it=s.rbegin();it!=s.rend();++it){mul_small(a,2);if(*it=='1')++a;}return a;}
static BigInt bit_op(BigInt a,BigInt b,int op){
bool na=a.sign<0,nb=b.sign<0,neg=op==1?(na&nb):op==2?(na|nb):(na^nb);
std::string sa=to_bin(na?~a:std::move(a)),sb=to_bin(nb?~b:std::move(b)),sc;sc.reserve(std::max(sa.size(),sb.size()));
for(std::size_t i=0;i<std::max(sa.size(),sb.size());++i){int ba=na^(i<sa.size()&&sa[i]=='1'),bb=nb^(i<sb.size()&&sb[i]=='1'),bc=op==1?(ba&bb):op==2?(ba|bb):(ba^bb);sc+=char('0'+(bc^neg));}
BigInt c=from_bin(std::move(sc));return neg?~c:c;
}
BigInt operator&(const BigInt&b)const{return bit_op(*this,b,1);}
BigInt operator|(const BigInt&b)const{return bit_op(*this,b,2);}
BigInt operator^(const BigInt&b)const{return bit_op(*this,b,3);}
BigInt operator~()const{return-(*this)-BigInt(1);}
BigInt operator<<(long long b)const{
if(b<0)throw std::invalid_argument("Negative shift");
BigInt r=*this;while(r.sign&&b>=30){mul_small(r,1<<30);b-=30;}if(r.sign&&b)mul_small(r,1<<b);return r;
}
BigInt operator>>(long long b)const{
if(b<0)throw std::invalid_argument("Negative shift");
BigInt r=*this;bool lost=false;while(r.sign&&b>=30){lost|=div_small(r,1<<30)!=0;b-=30;}if(r.sign&&b)lost|=div_small(r,1<<b)!=0;return sign<0&&lost?r-BigInt(1):r;
}
BigInt&operator+=(const BigInt&b){return*this=*this+b;}
BigInt&operator-=(const BigInt&b){return*this=*this-b;}
BigInt&operator*=(const BigInt&b){return*this=*this*b;}
BigInt&operator/=(const BigInt&b){return*this=*this/b;}
BigInt&operator%=(const BigInt&b){return*this=*this%b;}
BigInt&operator&=(const BigInt&b){return*this=*this&b;}
BigInt&operator|=(const BigInt&b){return*this=*this|b;}
BigInt&operator^=(const BigInt&b){return*this=*this^b;}
BigInt&operator<<=(long long b){return*this=*this<<b;}
BigInt&operator>>=(long long b){return*this=*this>>b;}
BigInt&operator++(){return*this=*this+BigInt(1);}
BigInt operator++(int){BigInt t=*this;++(*this);return t;}
BigInt&operator--(){return*this=*this-BigInt(1);}
BigInt operator--(int){BigInt t=*this;--(*this);return t;}
friend std::istream&operator>>(std::istream&is,BigInt&a){std::string s;if(is>>s){try{a=BigInt(std::move(s));}catch(const std::invalid_argument&){is.setstate(std::ios::failbit);}}return is;}
friend std::ostream&operator<<(std::ostream&os,const BigInt&a){if(!a.sign)return os<<'0';if(a.sign<0)os<<'-';os<<std::to_string(a.v.back());for(std::size_t i=a.v.size()-1;i--;){std::string s=std::to_string(a.v[i]);os<<std::string(9-s.size(),'0')<<s;}return os;}
};