C++#ifndef BIGINT_H //bigint.h 1.1
#define BIGINT_H
#include "algorithm"
#include "iostream"
#include "cstring"
#define SIZE int(st.size())
#define BIT int(1e8)
using namespace std;
struct bigint{
long long s[1010];
int f=1,l=1,tl=1;
inline bool zero(){
return (l<2&&!s[1]);
}
inline void tzero(){
memset(s,0,sizeof(s));
f=l=tl=1;
}
inline void resize(){
l=1000;
while (!s[l]&&l>1) l--;
tl=(l-1)<<3;
for (int i=8;i;i--)
if ((s[l]%BIT)/pow(10,i)){
tl+=i;
break;
}
}
inline void get(){
string st;
tzero();
cin>>st;
if (st[0]=='-'){
f=-1;
st.erase(0,1);
}
if (st[0]=='0'){
f=1;
return;
}
tl=SIZE;
while (SIZE>8){
for (int j=SIZE-8;j<SIZE;j++)
s[l]=(s[l]<<3)+(s[l]<<1)+(st[j]-'0');
st.erase(SIZE-8,8);
l++;
}
for (int i=0;i<SIZE;i++)
s[l]=(s[l]<<3)+(s[l]<<1)+(st[i]-'0');
}
inline void put(){
if (f==-1) printf("-");
printf("%llu",s[l]);
for (int i=l-1;i;i--) printf("%08llu",s[i]);
printf("\n");
}
inline void push(long long x){
string st;
tzero();
if (x<0) f=-1;
x*=f;
while (x){
st=char(x%10+'0')+st;
x/=10;
}
tl=SIZE;
while (SIZE>8){
for (int j=SIZE-8;j<SIZE;j++)
s[l]=(s[l]<<3)+(s[l]<<1)+(st[j]-'0');
st.erase(SIZE-8,8);
l++;
}
for (int i=0;i<SIZE;i++)
s[l]=(s[l]<<3)+(s[l]<<1)+(st[i]-'0');
}
};
inline bool operator==(bigint a,bigint b){
if (a.f!=b.f||a.tl!=b.tl) return false;
for (int i=a.l;i;i--)
if (a.s[i]!=b.s[i]) return false;
return true;
}
inline bool operator==(bigint a,long long b){
bigint c;
c.push(b);
return (a==c);
}
inline bool operator==(long long a,bigint b){
bigint c;
c.push(a);
return (c==b);
}
inline bool operator<(bigint a,bigint b){
if (a.f!=b.f) return (a.f<b.f);
if (a.tl<b.tl) return (a.f==1);
if (a.tl>b.tl) return (a.f==-1);
for (int i=a.l;i;i--)
if (a.s[i]!=b.s[i]) return (a.s[i]<b.s[i]);
return false;
}
inline bool operator<(bigint a,long long b){
bigint c;
c.push(b);
return (a<c);
}
inline bool operator<(long long a,bigint b){
bigint c;
c.push(a);
return (c<b);
}
inline bool operator>(bigint a,bigint b){
if (a.f!=b.f) return (a.f>b.f);
if (a.tl<b.tl) return (a.f==-1);
if (a.tl>b.tl) return (a.f==1);
for (int i=a.l;i;i--)
if (a.s[i]!=b.s[i]) return (a.s[i]>b.s[i]);
return false;
}
inline bool operator>(bigint a,long long b){
bigint c;
c.push(b);
return (a>c);
}
inline bool operator>(long long a,bigint b){
bigint c;
c.push(a);
return (c>b);
}
inline bool operator!=(bigint a,bigint b){
if (a.f!=b.f||a.tl!=b.tl) return true;
for (int i=a.l;i;i--)
if (a.s[i]!=b.s[i]) return true;
return false;
}
inline bool operator!=(bigint a,long long b){
bigint c;
c.push(b);
return (a!=c);
}
inline bool operator!=(long long a,bigint b){
bigint c;
c.push(a);
return (c!=b);
}
inline bool operator<=(bigint a,bigint b){
if (a.f!=b.f) return (a.f<b.f);
if (a.tl<b.tl) return (a.f==1);
if (a.tl>b.tl) return (a.f==-1);
for (int i=a.l;i;i--)
if (a.s[i]>b.s[i]) return false;
return true;
}
inline bool operator<=(bigint a,long long b){
bigint c;
c.push(b);
return (a<=c);
}
inline bool operator<=(long long a,bigint b){
bigint c;
c.push(a);
return (c<=b);
}
inline bool operator>=(bigint a,bigint b){
if (a.f!=b.f) return (a.f>b.f);
if (a.tl<b.tl) return (a.f==-1);
if (a.tl>b.tl) return (a.f==1);
for (int i=a.l;i;i--)
if (a.s[i]<b.s[i]) return false;
return true;
}
inline bool operator>=(bigint a,long long b){
bigint c;
c.push(b);
return (a>=c);
}
inline bool operator>=(long long a,bigint b){
bigint c;
c.push(a);
return (c>=b);
}
inline bigint operator+(bigint a,bigint b){
bigint c;
c.tzero();
if (a.f==1&&b.f==1){
for (int i=1;i<1001;i++) c.s[i]=a.s[i]+b.s[i];
for (int i=1;i<1001;i++){
c.s[i+1]+=c.s[i]/BIT;
c.s[i]%=BIT;
}
c.resize();
return c;
}
if (a.f==1&&b.f==-1){
b.f=1;
if (a<b){
swap(a,b);
a.f=-1;
}
for (int i=1;i<1001;i++){
if (a.s[i]<b.s[i]){
a.s[i+1]--;
a.s[i]+=BIT;
}
a.s[i]-=b.s[i];
}
a.resize();
return a;
}
if (a.f==-1&&b.f==1){
swap(a,b);
b.f=1;
if (a<b){
swap(a,b);
a.f=-1;
}
for (int i=1;i<1001;i++){
if (a.s[i]<b.s[i]){
a.s[i+1]--;
a.s[i]+=BIT;
}
a.s[i]-=b.s[i];
}
a.resize();
return a;
}
a.f=b.f=1;
c=a+b;
c.f=-1;
return c;
}
inline bigint operator+(bigint a,long long b){
bigint c;
c.push(b);
return (a+c);
}
inline bigint operator+(long long a,bigint b){
bigint c;
c.push(a);
return (c+b);
}
inline void operator+=(bigint &a,bigint b){
a=a+b;
}
inline void operator+=(bigint &a,long long b){
a=a+b;
}
inline bigint operator-(bigint a,bigint b){
bigint c;
c.tzero();
if (a.f!=b.f){
int bo=(a.f<b.f?-1:1);
a.f=b.f=1;
c=a+b;
c.f=bo;
return c;
}
if (a.f==1){
if (a<b){
swap(a,b);
a.f=-1;
}
for (int i=1;i<1001;i++){
if (a.s[i]<b.s[i]){
a.s[i+1]--;
a.s[i]+=BIT;
}
a.s[i]-=b.s[i];
}
a.resize();
return a;
}
a.f=b.f=1;
swap(a,b);
if (a<b){
swap(a,b);
a.f=-1;
}
for (int i=1;i<1001;i++){
if (a.s[i]<b.s[i]){
a.s[i+1]--;
a.s[i]+=BIT;
}
a.s[i]-=b.s[i];
}
a.resize();
return a;
}
inline bigint operator-(bigint a,long long b){
bigint c;
c.push(b);
return (a-c);
}
inline bigint operator-(long long a,bigint b){
bigint c;
c.push(a);
return (c-b);
}
inline void operator-=(bigint &a,bigint b){
a=a-b;
}
inline void operator-=(bigint &a,long long b){
a=a-b;
}
inline bigint operator*(bigint a,bigint b){
bigint c;
c.tzero();
c.f=(a.f==b.f?1:-1);
for (int i=1;i<=a.l;i++){
for (int j=1;j<=b.l;j++)
c.s[i+j-1]+=a.s[i]*b.s[j];
}
for (int i=1;i<1001;i++){
c.s[i+1]+=c.s[i]/BIT;
c.s[i]%=BIT;
}
c.resize();
return c;
}
inline bigint operator*(bigint a,long long b){
bigint c;
c.push(b);
return (a*c);
}
inline bigint operator*(long long a,bigint b){
bigint c;
c.push(a);
return (c*b);
}
inline void operator*=(bigint &a,bigint b){
a=a*b;
}
inline void operator*=(bigint &a,long long b){
a=a*b;
}
inline bigint operator/(bigint a,bigint b){
bigint c=b,f;
f.f=a.f*b.f;
a.f=b.f=1;
if (b.zero()){
printf("Division by zero\n");
exit(1);
}
while (b*BIT<=a) b*=BIT;
int cnt,k=1;
while (!c.s[k]) k++;
while (1){
cnt=b.l-c.l+1;
while (a>=b){
a-=b;
f.s[cnt]++;
}
if (b==c||a.zero()) break;
for (int i=1;i<=b.l;i++) b.s[i]=b.s[i+1];
b.l--;
}
f.resize();
return f;
}
inline bigint operator/(bigint a,long long b){
bigint c;
c.push(b);
return (a/c);
}
inline bigint operator/(long long a,bigint b){
bigint c;
c.push(a);
return (c/b);
}
inline void operator/=(bigint &a,bigint b){
a=a/b;
}
inline void operator/=(bigint &a,long long b){
a=a/b;
}
inline bigint operator%(bigint a,bigint b){
return a-a/b*b;
}
inline bigint operator%(bigint a,long long b){
bigint c;
c.push(b);
return (a%c);
}
inline bigint operator%(long long a,bigint b){
bigint c;
c.push(a);
return (c%b);
}
inline void operator%=(bigint &a,bigint b){
a=a%b;
}
inline void operator%=(bigint &a,long long b){
a=a%b;
}
inline bigint operator++(bigint &a){
a+=1;
return a;
}
inline bigint operator--(bigint &a){
a-=1;
return a;
}
inline bigint operator<<(bigint a,bigint b){
while (!b.zero()){
--b;
a*=2;
}
return a;
}
inline bigint operator<<(bigint a,long long b){
while (b--) a*=2;
return a;
}
inline void operator<<=(bigint &a,bigint b){
a=a<<b;
}
inline void operator<<=(bigint &a,long long b){
a=a<<b;
}
inline bigint operator>>(bigint a,bigint b){
while (!b.zero()){
--b;
a/=2;
}
return a;
}
inline bigint operator>>(bigint a,long long b){
while (b--) a/=2;
return a;
}
inline void operator>>=(bigint &a,bigint b){
a=a>>b;
}
inline void operator>>=(bigint &a,long long b){
a=a>>b;
}
/*
可直接使用:
1.max(a,b);
2.min(a,b);
3.sort(a+1,a+n+1,cmp);
4.stable_sort(a+1,a+n+1,cmp);
5.__gcd(a,b);(暂时无法使用)
*/
#endif //BIGINT_H