一个用来屯高精度类板子的模板.
基本都是用最朴素的万进制压位实现的高精度,乘法没有FFT,除法没有倍增.
代码如下:
typedef long long LL;
#define memset(a) memset(a,0,sizeof(a))
struct unsigned_bigint{
int a[N+9],n;
unsigned_bigint(int X=0){memset(a);for (n=0;X;X/=10000) a[++n]=X%10000;if (!n) n=1;}
unsigned_bigint(LL X){memset(a);for (n=0;X;X/=10000) a[++n]=X%10000;if (!n) n=1;}
unsigned_bigint(char *S,int N){
memset(a);n=N;
for (int i=4;i<n+4;i+=4){
int t=i/4;
if (i<n+1) a[t]=S[n-i+1]-'0';
if (i<n+2) a[t]=a[t]*10+S[n-i+2]-'0';
if (i<n+3) a[t]=a[t]*10+S[n-i+3]-'0';
if (i<n+4) a[t]=a[t]*10+S[n-i+4]-'0';
}
for (;!a[n];--n);
if (!n) n=1;
}
unsigned_bigint(char *S){
memset(a);n=strlen(S+1);
for (int i=4;i<n+4;i+=4){
int t=i>>2;
if (i<n+1) a[t]=S[n-i+1]-'0';
if (i<n+2) a[t]=a[t]*10+S[n-i+2]-'0';
if (i<n+3) a[t]=a[t]*10+S[n-i+3]-'0';
if (i<n+4) a[t]=a[t]*10+S[n-i+4]-'0';
}
for (;!a[n];--n);
if (!n) n=1;
}
unsigned_bigint(string S){
memset(a);n=S.size();
for (int i=4;i<n+4;i+=4){
int t=i>>2;
if (i<=n) a[t]=S[n-i]-'0';
if (i<=n+1) a[t]=a[t]*10+S[n-i+1]-'0';
if (i<=n+2) a[t]=a[t]*10+S[n-i+2]-'0';
if (i<=n+3) a[t]=a[t]*10+S[n-i+3]-'0';
}
for (;!a[n];--n);
if (!n) n=1;
}
int &operator [] (const int &p){return a[p];}
friend istream &operator >> (istream &in,unsigned_bigint &p){
string s;
in>>s;
p=unsigned_bigint(s);
return in;
}
friend ostream &operator << (ostream &out,const unsigned_bigint &p){
out<<p.a[p.n];
for (int i=p.n-1;i>=1;--i){
if (p.a[i]<1000) out<<0;
if (p.a[i]<100) out<<0;
if (p.a[i]<10) out<<0;
out<<p.a[i];
}
return out;
}
bool operator < (const unsigned_bigint &p)const{
if (n^p.n) return n<p.n;
for (int i=n;i>=0;--i)
if (a[i]^p.a[i]) return a[i]<p.a[i];
return 0;
}
bool operator > (const unsigned_bigint &p)const{return p<*this;}
bool operator <= (const unsigned_bigint &p)const{return !(*this>p);}
bool operator >= (const unsigned_bigint &p)const{return !(*this<p);}
bool operator == (const unsigned_bigint &p)const{return *this<=p&&*this>=p;}
bool operator != (const unsigned_bigint &p)const{return *this<p||*this>p;}
bool operator < (const int &p)const{return *this<unsigned_bigint(p);}
bool operator < (const LL &p)const{return *this<unsigned_bigint(p);}
bool operator < (const string &p)const{return *this<unsigned_bigint(p);}
bool operator < (const char *p)const{return *this<unsigned_bigint(p);}
bool operator > (const int &p)const{return *this>unsigned_bigint(p);}
bool operator > (const LL &p)const{return *this>unsigned_bigint(p);}
bool operator > (const string &p)const{return *this>unsigned_bigint(p);}
bool operator > (const char *p)const{return *this>unsigned_bigint(p);}
bool operator <= (const int &p)const{return *this<=unsigned_bigint(p);}
bool operator <= (const LL &p)const{return *this<=unsigned_bigint(p);}
bool operator <= (const string &p)const{return *this<=unsigned_bigint(p);}
bool operator <= (const char *p)const{return *this<=unsigned_bigint(p);}
bool operator >= (const int &p)const{return *this>=unsigned_bigint(p);}
bool operator >= (const LL &p)const{return *this>=unsigned_bigint(p);}
bool operator >= (const string &p)const{return *this>=unsigned_bigint(p);}
bool operator >= (const char *p)const{return *this>=unsigned_bigint(p);}
bool operator == (const int &p)const{return *this==unsigned_bigint(p);}
bool operator == (const LL &p)const{return *this==unsigned_bigint(p);}
bool operator == (const string &p)const{return *this==unsigned_bigint(p);}
bool operator == (const char *p)const{return *this==unsigned_bigint(p);}
bool operator != (const int &p)const{return *this!=unsigned_bigint(p);}
bool operator != (const LL &p)const{return *this!=unsigned_bigint(p);}
bool operator != (const string &p)const{return *this!=unsigned_bigint(p);}
bool operator != (const char *p)const{return *this!=unsigned_bigint(p);}
unsigned_bigint operator + (const unsigned_bigint &p)const{
unsigned_bigint res;
res.n=max(n,p.n);
for (int i=1;i<=res.n;++i){
res.a[i]+=a[i]+p.a[i];
if (res.a[i]>=10000) ++res.a[i+1],res.a[i]-=10000;
}
if (res.a[res.n+1]) ++res.n;
return res;
}
unsigned_bigint operator + (const int &p)const{return *this+unsigned_bigint(p);}
unsigned_bigint operator + (const LL &p)const{return *this+unsigned_bigint(p);}
unsigned_bigint operator + (const char *s)const{return *this+unsigned_bigint(s);}
unsigned_bigint operator + (const string s)const{return *this+unsigned_bigint(s);}
unsigned_bigint &operator += (const unsigned_bigint &p){return *this=*this+p;}
unsigned_bigint &operator += (const int &p){return *this=*this+p;}
unsigned_bigint &operator += (const LL &p){return *this=*this+p;}
unsigned_bigint &operator += (const char &p){return *this=*this+p;}
unsigned_bigint &operator += (const string &p){return *this=*this+p;}
unsigned_bigint operator - (const unsigned_bigint &p)const{
unsigned_bigint res;
res.n=n;
for (int i=1;i<=res.n;++i){
res.a[i]+=a[i]-p.a[i];
if (res.a[i]<0) --res.a[i+1],res.a[i]+=10000;
}
for (;res.n>1&&!res.a[res.n];--res.n);
return res;
}
unsigned_bigint operator - (const int &p)const{return *this-unsigned_bigint(p);}
unsigned_bigint operator - (const LL &p)const{return *this-unsigned_bigint(p);}
unsigned_bigint operator - (const char *s)const{return *this-unsigned_bigint(s);}
unsigned_bigint operator - (const string s)const{return *this-unsigned_bigint(s);}
unsigned_bigint &operator -= (const unsigned_bigint &p){return *this=*this-p;}
unsigned_bigint &operator -= (const int &p){return *this=*this-p;}
unsigned_bigint &operator -= (const LL &p){return *this=*this-p;}
unsigned_bigint &operator -= (const char &p){return *this=*this-p;}
unsigned_bigint &operator -= (const string &p){return *this=*this-p;}
unsigned_bigint operator * (const unsigned_bigint &p)const{
unsigned_bigint res;
res.n=n+p.n-1;
for (int i=1;i<=n;++i)
for (int j=1;j<=p.n;++j){
res.a[i+j-1]+=a[i]*p.a[j];
res.a[i+j]+=res.a[i+j-1]/10000;res.a[i+j-1]%=10000;
}
if (res.a[res.n+1]) ++res.n;
for (;!res.a[res.n]&&res.n>1;--res.n);
return res;
}
unsigned_bigint operator * (const int &p)const{
if (p>100000) return *this*unsigned_bigint(p);
unsigned_bigint res;
res.n=n+8;
for (int i=1;i<=res.n;++i){
res.a[i]+=a[i]*p;
res.a[i+1]+=res.a[i]/10000;res.a[i]%=10000;
}
for (;!res.a[res.n]&&res.n>1;--res.n);
return res;
}
unsigned_bigint operator * (const LL &p)const{return *this*unsigned_bigint(p);}
unsigned_bigint operator * (const char *s)const{return *this*unsigned_bigint(s);}
unsigned_bigint operator * (const string s)const{return *this*unsigned_bigint(s);}
unsigned_bigint &operator *= (const unsigned_bigint &p){return *this=*this*p;}
unsigned_bigint &operator *= (const int &p){return *this=*this*p;}
unsigned_bigint &operator *= (const LL &p){return *this=*this*p;}
unsigned_bigint &operator *= (const char &p){return *this=*this*p;}
unsigned_bigint &operator *= (const string &p){return *this=*this*p;}
unsigned_bigint operator / (const unsigned_bigint &p)const{
unsigned_bigint res,now=unsigned_bigint(0);
for (int i=n;i>=1;--i){
now=now*10000+a[i];
for (;now>=p;now-=p) ++res.a[i];
}
for (res.n=n;!res.a[res.n]&&res.n>1;--res.n);
return res;
}
unsigned_bigint operator / (const int &p)const{
if (p>100000) return *this/unsigned_bigint(p);
unsigned_bigint res;
int now=0;
for (int i=n;i>=1;--i){
now=now*10000+a[i];
res.a[i]=now/p;now%=p;
}
for (res.n=n;!res.a[res.n]&&res.n>1;--res.n);
return res;
}
unsigned_bigint operator / (const LL &p)const{return *this/unsigned_bigint(p);}
unsigned_bigint operator / (const char *s)const{return *this/unsigned_bigint(s);}
unsigned_bigint operator / (const string s)const{return *this/unsigned_bigint(s);}
unsigned_bigint &operator /= (const unsigned_bigint &p){return *this=*this/p;}
unsigned_bigint &operator /= (const int &p){return *this=*this/p;}
unsigned_bigint &operator /= (const LL &p){return *this=*this/p;}
unsigned_bigint &operator /= (const char &p){return *this=*this/p;}
unsigned_bigint &operator /= (const string &p){return *this=*this/p;}
unsigned_bigint operator % (const unsigned_bigint &p)const{
unsigned_bigint res=unsigned_bigint(0);
for (int i=n;i>=1;--i){
res=res*10000+a[i];
for (;res>=p;res-=p);
}
return res;
}
unsigned_bigint operator % (const int &p)const{
if (p>100000) return *this%unsigned_bigint(p);
int res=0;
for (int i=n;i>=1;--i) res=(res*10000+a[i])%p;
return unsigned_bigint(res);
}
unsigned_bigint operator % (const LL &p)const{return *this%unsigned_bigint(p);}
unsigned_bigint operator % (const char *s)const{return *this%unsigned_bigint(s);}
unsigned_bigint operator % (const string s)const{return *this%unsigned_bigint(s);}
unsigned_bigint &operator %= (const unsigned_bigint &p){return *this=*this%p;}
unsigned_bigint &operator %= (const int &p){return *this=*this%p;}
unsigned_bigint &operator %= (const LL &p){return *this=*this%p;}
unsigned_bigint &operator %= (const char &p){return *this=*this%p;}
unsigned_bigint &operator %= (const string &p){return *this=*this%p;}
int Get_int(){
int res=0;
for (int i=n;i>=1;--i) res=res*10000+a[i];
return res;
}
};
unsigned_bigint operator * (const int &k,const unsigned_bigint &a){return a*k;}
struct bigint{
int opt;
unsigned_bigint a;
bigint(int X=0){opt=X<0;a=unsigned_bigint(abs(X));}
bigint(LL X){opt=X<0;a=unsigned_bigint(abs(X));}
bigint(char *S,int N){opt=S[1]=='-';a=unsigned_bigint(S[1]=='-'?(S+1,N-1):(S,N));}
bigint(char *S){opt=S[1]=='-';a=unsigned_bigint(S[1]=='-'?S+1:S);}
bigint(string S){opt=S[0]=='-';if (S[0]=='-') S.erase(0,1);a=unsigned_bigint(S);}
bigint(int Opt,unsigned_bigint A){opt=Opt;a=A;}
int &operator [] (const int &p){return a[p];}
friend istream &operator >> (istream &in,bigint &p){string s;in>>s;p=bigint(s);return in;}
friend ostream &operator << (ostream &out,const bigint &p){
if (p.opt&&(p.a.n^1||p.a.a[1]^0)) out<<'-';
out<<p.a;
return out;
}
bool operator < (const bigint &p)const{if (opt^p.opt) return opt>p.opt;return opt?a>p.a:a<p.a;}
bool operator > (const bigint &p)const{return p<*this;}
bool operator <= (const bigint &p)const{return !(*this>p);}
bool operator >= (const bigint &p)const{return !(*this<p);}
bool operator == (const bigint &p)const{return *this<=p&&*this>=p;}
bool operator != (const bigint &p)const{return *this<p||*this>p;}
bool operator < (const int &p)const{return *this<bigint(p);}
bool operator < (const LL &p)const{return *this<bigint(p);}
bool operator < (const string &p)const{return *this<bigint(p);}
bool operator < (const char *p)const{return *this<bigint(p);}
bool operator > (const int &p)const{return *this>bigint(p);}
bool operator > (const LL &p)const{return *this>bigint(p);}
bool operator > (const string &p)const{return *this>bigint(p);}
bool operator > (const char *p)const{return *this>bigint(p);}
bool operator <= (const int &p)const{return *this<=bigint(p);}
bool operator <= (const LL &p)const{return *this<=bigint(p);}
bool operator <= (const string &p)const{return *this<=bigint(p);}
bool operator <= (const char *p)const{return *this<=bigint(p);}
bool operator >= (const int &p)const{return *this>=bigint(p);}
bool operator >= (const LL &p)const{return *this>=bigint(p);}
bool operator >= (const string &p)const{return *this>=bigint(p);}
bool operator >= (const char *p)const{return *this>=bigint(p);}
bool operator == (const int &p)const{return *this==bigint(p);}
bool operator == (const LL &p)const{return *this==bigint(p);}
bool operator == (const string &p)const{return *this==bigint(p);}
bool operator == (const char *p)const{return *this==bigint(p);}
bool operator != (const int &p)const{return *this!=bigint(p);}
bool operator != (const LL &p)const{return *this!=bigint(p);}
bool operator != (const string &p)const{return *this!=bigint(p);}
bool operator != (const char *p)const{return *this!=bigint(p);}
bigint operator - (){return bigint(opt^1,a);}
bigint operator + (const bigint &p)const{
return opt==p.opt?bigint(opt,a+p.a):bigint(opt^a<p.a,a<p.a?p.a-a:a-p.a);
}
bigint operator + (const int &p)const{return *this+bigint(p);}
bigint operator + (const LL &p)const{return *this+bigint(p);}
bigint operator + (const char *s)const{return *this+bigint(s);}
bigint operator + (const string s)const{return *this+bigint(s);}
bigint &operator += (const bigint &p){return *this=*this+p;}
bigint &operator += (const int &p){return *this=*this+p;}
bigint &operator += (const LL &p){return *this=*this+p;}
bigint &operator += (const char &p){return *this=*this+p;}
bigint &operator += (const string &p){return *this=*this+p;}
bigint operator - (const bigint &p)const{return *this+bigint(p.opt^1,p.a);}
bigint operator - (const int &p)const{return *this-bigint(p);}
bigint operator - (const LL &p)const{return *this-bigint(p);}
bigint operator - (const char *s)const{return *this-bigint(s);}
bigint operator - (const string s)const{return *this-bigint(s);}
bigint &operator -= (const bigint &p){return *this=*this-p;}
bigint &operator -= (const int &p){return *this=*this-p;}
bigint &operator -= (const LL &p){return *this=*this-p;}
bigint &operator -= (const char &p){return *this=*this-p;}
bigint &operator -= (const string &p){return *this=*this-p;}
bigint operator * (const bigint &p)const{return bigint(opt^p.opt,a*p.a);}
bigint operator * (const int &p)const{return bigint(opt^p<0,a*abs(p));}
bigint operator * (const LL &p)const{return *this*bigint(p);}
bigint operator * (const char *s)const{return *this*bigint(s);}
bigint operator * (const string s)const{return *this*bigint(s);}
bigint &operator *= (const bigint &p){return *this=*this*p;}
bigint &operator *= (const int &p){return *this=*this*p;}
bigint &operator *= (const LL &p){return *this=*this*p;}
bigint &operator *= (const char &p){return *this=*this*p;}
bigint &operator *= (const string &p){return *this=*this*p;}
bigint operator / (const bigint &p)const{return bigint(opt^p.opt,a/p.a);}
bigint operator / (const int &p)const{return bigint(opt^p<0,a/abs(p));}
bigint operator / (const LL &p)const{return *this/bigint(p);}
bigint operator / (const char *s)const{return *this/bigint(s);}
bigint operator / (const string s)const{return *this/bigint(s);}
bigint &operator /= (const bigint &p){return *this=*this/p;}
bigint &operator /= (const int &p){return *this=*this/p;}
bigint &operator /= (const LL &p){return *this=*this/p;}
bigint &operator /= (const char &p){return *this=*this/p;}
bigint &operator /= (const string &p){return *this=*this/p;}
bigint operator % (const bigint &p)const{return bigint(opt,a%p.a);}
bigint operator % (const int &p)const{return bigint(opt,a%p);}
bigint operator % (const LL &p)const{return *this%bigint(p);}
bigint operator % (const char *s)const{return *this%bigint(s);}
bigint operator % (const string s)const{return *this%bigint(s);}
bigint &operator %= (const bigint &p){return *this=*this%p;}
bigint &operator %= (const int &p){return *this=*this%p;}
bigint &operator %= (const LL &p){return *this=*this%p;}
bigint &operator %= (const char &p){return *this=*this%p;}
bigint &operator %= (const string &p){return *this=*this%p;}
int Get_int(){return a.Get_int()*(opt?-1:1);}
};
bigint operator * (const int &k,const bigint &a){return a*k;}