namespace LONGER{ typedef long long ll; typedef unsigned long long UTYPE; typedef ll TYPE; const TYPE sll=sizeof(TYPE); const TYPE MAXLENGTH=50; inline TYPE max(const TYPE&a,const TYPE&b){return a<b?b:a;} inline void Swap(long long &a,long long &b){a=a^b;b=a^b;a=a^b;} struct Longer{ #define LOWBIT 1000000000 int size; TYPE num[MAXLENGTH]; Longer(const TYPE&a=0){size=1;memset(num,0,sizeof(num));num[1]=a;} TYPE &operator[](int x){return num[x];} }; Longer operator +(const Longer&,const Longer&); Longer operator -(const Longer&,const Longer&); Longer operator *(const Longer&,const Longer&); Longer operator /(const Longer&,const Longer&); Longer operator /(const Longer&,const TYPE&); Longer operator % (const Longer&,const Longer&); Longer operator <<(const Longer&,const TYPE&); Longer operator >>(const Longer&,const TYPE&); bool operator <(const Longer&,const Longer&); bool operator >(const Longer&,const Longer&); bool operator >=(const Longer&,const Longer&); bool operator <=(const Longer&,const Longer&); bool operator !=(const Longer&,const Longer&); bool operator ==(const Longer&,const Longer&); void read(Longer &); void fread(Longer &,FILE*); void put(const Longer&); void fput(const Longer&,FILE*); Longer exchange(const Longer&); Longer exchange(const Longer&a){ Longer c; c.size=a.size; for(int i=1;i<=a.size;++i) c.num[i]=a.num[a.size-i+1]; return c; } Longer operator <<(const Longer&a,const TYPE&b){ Longer c; c.size=a.size+b; for(int i=1;i<=a.size;++i) if( i+b <MAXLENGTH )c.num[i+b]=a.num[i]; return c; } Longer operator >>(const Longer&a,const TYPE&b){ Longer c; c.size=max(1,a.size-b); for(int i=b+1;i<=a.size;++i) c.num[i-b]=a.num[i]; return c; } Longer operator +(const Longer &a,const Longer &b){ Longer c; c.size=max(a.size,b.size); for(int i=1;i<=c.size;++i) c.num[i]=a.num[i]+b.num[i]; for(int i=1;i<=c.size;++i){ c.num[i+1]+=c.num[i]/LOWBIT; c.num[i]%=LOWBIT; } while(c.num[c.size+1]!=0)c.size++; return c; } Longer operator -(const Longer &a,const Longer &b){ if(a<b)return 0; Longer c; c.size=max(a.size,b.size); for(int i=1;i<=c.size;++i) c.num[i]=a.num[i]-b.num[i]; for(int i=1;i<=c.size;++i) if(c.num[i]<0){ c.num[i]+=LOWBIT; --c.num[i+1]; } while(c.num[c.size]==0&&c.size>0)--c.size; return c; } Longer operator *(const Longer &a,const Longer &b){ Longer c; c.size=a.size+b.size-1; for(int i=1;i<=a.size;++i) for(int j=1;j<=b.size;++j) c.num[i+j-1]+=a.num[i]*b.num[j]; for(int i=1;i<=c.size;++i){ c.num[i+1]+=c.num[i]/LOWBIT; c.num[i]%=LOWBIT; } if(c.num[c.size+1]!=0)c.size++; return c; } Longer operator / (const Longer&a,const TYPE&b){ if(b==1)return a; TYPE d=0; Longer c; c.size=a.size; for(int i=c.size;i>=1;--i){ c.num[i]=(d*LOWBIT+a.num[i])/b; d=(d*LOWBIT+a.num[i])%b; } while(c.num[c.size]==0&&c.size>0)--c.size; return c; } Longer operator / (const Longer &_a,const Longer &_b){ if(_b==1)return _a; Longer l=1,r=1,mid,p,a=_a,b=_b; if(b==0){printf("Runtime Error in Big Integer :: the devided number cannot be zero. ");abort();} if(a==0||a<b)return 0; TYPE k=max(a.size-b.size,0); l=l<<(k-1); r=r<<(k+1); //put(l);put(r); while(l<r){ mid=((l+r)/2); p=mid*b; if(p==a)return mid; else if(p>a)r=mid+(-1); else l=mid+1; } if(l*b>a)return l-1; return l; } Longer operator %(const Longer &_a,const Longer&_b){ return _a-(_a/_b)*_b; } bool operator <(const Longer&a,const Longer&b){ if(a.size!=b.size)return a.size<b.size; for(int i=a.size;i>=1;--i) if(a.num[i]!=b.num[i]) return a.num[i]<b.num[i]; return 0; } bool operator >(const Longer&a,const Longer&b){return b<a;} bool operator >=(const Longer&a,const Longer&b){return a>b||a==b;} bool operator <=(const Longer&a,const Longer&b){return a<b||a==b;} bool operator !=(const Longer&a,const Longer&b){return !(a==b);} bool operator ==(const Longer &a,const Longer&b){return !(a<b)&&!(b<a);} void read(Longer &t){t=0; std::string s; char str[1024]; scanf("%s",str); s=str; int sizen=s.length(); // printf(" sizen->%d ",sizen); int p=sizen%9,i=0;TYPE k=0; t.size=sizen/9+(sizen%9!=0); if(p!=0){for(i=0;i<p;++i)k=k*10+s[i]-'0'; t.num[t.size--]=k; k=0;} while(t.size){ p=i+9; for(;i<p;++i)k=k*10+s[i]-'0'; t.num[t.size--]=k;k=0; }t.size=sizen/9+(sizen%9!=0); t=t+0; } void put(const Longer &k){ printf("%lld",k.num[k.size]); for(int i=k.size-1;i>=1;--i) printf("%.9lld",k.num[i]); } void fread(Longer &t,FILE*IR){t=0; std::string s; char str[1024]; fscanf(IR,"%s",str); s=str; int sizen=s.length(); int p=sizen%9,i=0;TYPE k=0; t.size=sizen/9+(sizen%9!=0); if(p!=0){for(i=0;i<p;++i)k=k*10+s[i]-'0'; t.num[t.size--]=k; k=0;} while(t.size){ p=i+9; for(;i<p;++i)k=k*10+s[i]-'0'; t.num[t.size--]=k;k=0; }t.size=sizen/9+(sizen%9!=0); t=t+0; } void fput(const Longer &k,FILE*OR){ fprintf(OR,"%lld",k.num[k.size]); for(int i=k.size-1;i>=1;--i) fprintf(OR,"%09lld",k.num[i]); } }