版本比较
模板
标题
增加 1 行删除 1 行
1模版1模板
正文
增加 93 行删除 0 行
291 行未改动全部展开292293294295291 行未改动全部展开int sub(int *a,int *b,int La,int Lb){if(La<Lb) return -1;if(La==Lb){for(int i=La-1;i>=0;i--)if(a[i]>b[i]) break;else if(a[i]<b[i]) return -1;}for(int i=0;i<La;i++){ a[i]-=b[i]; if(a[i]<0) a[i]+=10,a[i+1]--; } for(int i=La-1;i>=0;i--) if(a[i]) return i+1;return 0;}string div(string n1,string n2){string s,v;int a[L],b[L],r[L],La=n1.size(),Lb=n2.size(),i,tp=La;fill(a,a+L,0);fill(b,b+L,0);fill(r,r+L,0);for(i=La-1;i>=0;i--) a[La-1-i]=n1[i]-'0'; for(i=Lb-1;i>=0;i--) b[Lb-1-i]=n2[i]-'0'; if(La<Lb || (La==Lb && n1<n2)){return n1;} int t=La-Lb;for(int i=La-1;i>=0;i--)if(i>=t) b[i]=b[i-t]; else b[i]=0;Lb=La;for(int j=0;j<=t;j++){int temp;while((temp=sub(a,b+j,La,Lb-j))>=0){La=temp;r[t-j]++;} }for(i=0;i<L-10;i++) r[i+1]+=r[i]/10,r[i]%=10;while(!r[i]) i--;while(i>=0) s+=r[i--]+'0'; i=tp; while(!a[i]) i--;while(i>=0) v+=a[i--]+'0'; if(v.empty()) v="0"; return v; }```291 行未改动全部展开292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388291 行未改动全部展开int sub(int *a,int *b,int La,int Lb){if(La<Lb) return -1;if(La==Lb){for(int i=La-1;i>=0;i--)if(a[i]>b[i]) break;else if(a[i]<b[i]) return -1;}for(int i=0;i<La;i++){ a[i]-=b[i]; if(a[i]<0) a[i]+=10,a[i+1]--; } for(int i=La-1;i>=0;i--) if(a[i]) return i+1;return 0;}string div(string n1,string n2){string s,v;int a[L],b[L],r[L],La=n1.size(),Lb=n2.size(),i,tp=La;fill(a,a+L,0);fill(b,b+L,0);fill(r,r+L,0);for(i=La-1;i>=0;i--) a[La-1-i]=n1[i]-'0'; for(i=Lb-1;i>=0;i--) b[Lb-1-i]=n2[i]-'0'; if(La<Lb || (La==Lb && n1<n2)){return n1;} int t=La-Lb;for(int i=La-1;i>=0;i--)if(i>=t) b[i]=b[i-t]; else b[i]=0;Lb=La;for(int j=0;j<=t;j++){int temp;while((temp=sub(a,b+j,La,Lb-j))>=0){La=temp;r[t-j]++;} }for(i=0;i<L-10;i++) r[i+1]+=r[i]/10,r[i]%=10;while(!r[i]) i--;while(i>=0) s+=r[i--]+'0'; i=tp; while(!a[i]) i--;while(i>=0) v+=a[i--]+'0'; if(v.empty()) v="0"; return v; }```# 高精度```cppconstexpr ll M=5000/8+5;const int base=1e8;int aux[M<<3];struct bignum{int s[M],l;bool neg;void clr(){l=0;memset(s,0,sizeof(s));neg=false;}bignum(){clr();}void write(){if(neg&&!(l==1&&s[1]==0))putchar('-');printf("%d",s[l]);for(int i=l-1;i;i--)printf("%08d",s[i]);}void read(){char c=getchar();bool sign=false;while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-'){sign=true;c=getchar();}int i,x=0,k=1,L=0,fl,o;for(;c>='0'&&c<='9';c=getchar()){if(!(L-1)&&!aux[L])L--;aux[++L]=c-'0';}clr();neg=sign;l=L/8+((o=L%8)>0);for(i=1;i<=o;i++)x=x*10+aux[i];if(o)s[l]=x;for(fl=!o?l+1:l,i=o+1,x=0;i<=L;i++,k++){x=x*10+aux[i];if(!(k^8))s[--fl]=x,x=k=0;}if(!l)l=1;if(l==1&&s[1]==0)neg=false;}ll toint()const{ll x=0;for(int i=l;i;i--)x=x*base+s[i];return neg?-x:x;}bignum& operator=(int b){clr();if(b<0){neg=true;b=-b;}do{s[++l]=b%base;b/=base;}while(b>0);return *this;}bignum& operator=(ll b){clr();if(b<0){neg=true;b=-b;}do{s[++l]=b%base;b/=base;}while(b>0);return *this;}bool abs_less(const bignum& b)const{if(l^b.l)return l<b.l;for(int i=l;i;i--)if(s[i]^b.s[i])return s[i]<b.s[i];return false;}bignum abs_add(const bignum& b)const{bignum c;c.clr();ll x=0;int k=max(l,b.l);c.l=k;for(int i=1;i<=k;i++){x=x+(i<=l?s[i]:0)+(i<=b.l?b.s[i]:0);c.s[i]=x%base;x/=base;}if(x)c.s[++c.l]=x;return c;}bignum abs_sub(const bignum& b)const{bignum c,d=*this;c.clr();ll x=0;for(int i=1;i<=l;i++){if((x=d.s[i])<b.s[i]){d.s[i+1]--;x+=base;}c.s[i]=x-b.s[i];}c.l=l;for(;!c.s[c.l]&&c.l>1;c.l--);return c;}bignum operator-()const{bignum c=*this;if(!(l==1&&s[1]==0))c.neg=!neg;return c;}bignum operator+(const bignum& b)const{if(neg==b.neg){bignum c=abs_add(b);c.neg=neg;if(c.l==1&&c.s[1]==0)c.neg=false;return c;}else{if(abs_less(b)){bignum c=b.abs_sub(*this);c.neg=b.neg;if(c.l==1&&c.s[1]==0)c.neg=false;return c;}else{bignum c=abs_sub(b);c.neg=neg;if(c.l==1&&c.s[1]==0)c.neg=false;return c;}}}bignum operator-(const bignum& b)const{bignum neg_b=b;neg_b.neg=!b.neg;return *this+neg_b;}bignum operator*(const bignum& b)const{if(b.l<2)return *this*b.toint();bignum c;ll x;int i,j,k;c.clr();for(i=1;i<=l;i++){x=0;for(j=1;j<=b.l;j++){x=x+1LL*s[i]*b.s[j]+c.s[k=i+j-1];c.s[k]=x%base;x/=base;}if(x)c.s[i+b.l]=x;}c.l=l+b.l;for(;!c.s[c.l]&&c.l>1;c.l--);c.neg=neg^b.neg;if(c.l==1&&c.s[1]==0)c.neg=false;return c;}bignum operator*(const ll& b)const{ll bb=b<0?-b:b;bignum c;c.clr();ll x=0;for(int i=1;i<=l;i++){x=x+1LL*s[i]*bb;c.s[i]=x%base;x/=base;}c.l=l;while(x){c.s[++c.l]=x%base;x/=base;}c.neg=neg^(b<0);if(c.l==1&&c.s[1]==0)c.neg=false;return c;}bignum operator*(const int& b)const{return *this*(ll)b;}bignum operator/(const bignum& b)const{if(b.l<2)return *this/b.toint();bignum c,d;int i,j,le,r,mid,k;c.clr();d.clr();bignum abs_this=*this,abs_b=b;abs_this.neg=abs_b.neg=false;for(i=l;i;i--){for(j=++d.l;j>1;j--)d.s[j]=d.s[j-1];d.s[1]=abs_this.s[i];if(d<abs_b)continue;le=k=0;r=base-1;while(le<=r){mid=(le+r)>>1;bignum tmp=abs_b*mid;(tmp<=d)?(le=mid+1,k=mid):(r=mid-1);}c.s[i]=k;d=d-abs_b*k;}c.l=l;for(;!c.s[c.l]&&c.l>1;c.l--);c.neg=neg^b.neg;if(c.l==1&&c.s[1]==0)c.neg=false;return c;}bignum operator%(const bignum& b)const{if(b.l<2)return *this%b.toint();bignum c;int i,j,le,r,mid,k;c.clr();bignum abs_this=*this,abs_b=b;abs_this.neg=abs_b.neg=false;for(i=l;i;i--){for(j=++c.l;j>1;j--)c.s[j]=c.s[j-1];c.s[1]=abs_this.s[i];if(c<abs_b)continue;le=k=0;r=base-1;while(le<=r){mid=(le+r)>>1;bignum tmp=abs_b*mid;(tmp<=c)?(le=mid+1,k=mid):(r=mid-1);}c=c-abs_b*k;}for(;!c.s[c.l]&&c.l>1;c.l--);c.neg=neg;if(c.l==1&&c.s[1]==0)c.neg=false;return c;}bignum operator+(const int& b)const{bignum tmp;tmp=b;return *this+tmp;}bignum operator+(const ll& b)const{bignum tmp;tmp=b;return *this+tmp;}bignum operator-(const int& b)const{bignum tmp;tmp=b;return *this-tmp;}bignum operator-(const ll& b)const{bignum tmp;tmp=b;return *this-tmp;}bignum operator/(const int& b)const{bignum c;ll x=0;c.clr();for(int i=l;i;i--){c.s[i]=(x*base+s[i])/b;x=(x*base+s[i])%b;}for(c.l=l;!c.s[c.l]&&c.l>1;c.l--);return c;}bignum operator/(const ll & b)const{bignum c;ll x=0;c.clr();for(int i=l;i;i--){c.s[i]=(x*base+s[i])/b;x=(x*base+s[i])%b;}for(c.l=l;!c.s[c.l]&&c.l>1;c.l--);return c;}bignum operator%(const int& b)const{bignum c;ll x=0;c.clr();for(int i=l;i;i--)x=(x*base+s[i])%b;return c=x;}bignum operator%(const ll & b)const{bignum c;ll x=0;c.clr();for(int i=l;i;i--)x=(x*base+s[i])%b;return c=x;}bignum operator+=(const bignum& b){return *this=*this+b;}bignum operator+=(int b){bignum tmp;tmp=b;return *this+=tmp;}bignum operator+=(ll b){bignum tmp;tmp=b;return *this+=tmp;}bignum operator-=(const bignum& b){return *this=*this-b;}bignum operator-=(int b){bignum tmp;tmp=b;return *this-=tmp;}bignum operator-=(ll b){bignum tmp;tmp=b;return *this-=tmp;}bignum operator*=(const bignum& b){return *this=*this*b;}bignum operator*=(int b){return *this=*this*(ll)b;}bignum operator*=(ll b){return *this=*this*b;}bignum operator/=(const bignum& b){return *this=*this/b;}bignum operator/=(int b){bignum tmp;tmp=b;return *this/=tmp;}bignum operator/=(ll b){bignum tmp;tmp=b;return *this/=tmp;}bignum operator%=(const bignum& b){return *this=*this%b;}bignum operator%=(int b){bignum tmp;tmp=b;return *this%=tmp;}bignum operator%=(ll b){bignum tmp;tmp=b;return *this%=tmp;}bool operator<(const bignum& b)const{if(neg!=b.neg)return neg>b.neg;if(neg==false)return abs_less(b);else return b.abs_less(*this);}bool operator<=(const bignum& b)const{return !(*this>b);}bool operator>(const bignum& b)const{return b<*this;}bool operator>=(const bignum& b)const{return !(*this<b);}bool operator==(const bignum& b)const{if(neg!=b.neg)return false;if(l!=b.l)return false;for(int i=l;i;i--)if(s[i]!=b.s[i])return false;return true;}bool operator!=(const bignum& b)const{return !(*this==b);}bool operator<(int b)const{bignum tmp;tmp=b;return *this<tmp;}bool operator<=(int b)const{bignum tmp;tmp=b;return *this<=tmp;}bool operator>(int b)const{bignum tmp;tmp=b;return *this>tmp;}bool operator>=(int b)const{bignum tmp;tmp=b;return *this>=tmp;}bool operator==(int b)const{bignum tmp;tmp=b;return *this==tmp;}bool operator!=(int b)const{bignum tmp;tmp=b;return *this!=tmp;}bool operator<(ll b)const{bignum tmp;tmp=b;return *this<tmp;}bool operator<=(ll b)const{bignum tmp;tmp=b;return *this<=tmp;}bool operator>(ll b)const{bignum tmp;tmp=b;return *this>tmp;}bool operator>=(ll b)const{bignum tmp;tmp=b;return *this>=tmp;}bool operator==(ll b)const{bignum tmp;tmp=b;return *this==tmp;}bool operator!=(ll b)const{bignum tmp;tmp=b;return *this!=tmp;}};```# 哈希```cppconstexpr ll base1=233,base2=131,mod1=998244353,mod2=1e9+7;struct hash{ll n;string s;vector<ll> pw1,pw2,h1,h2;inline void init(){n=s.size();s=" "+s;pw1.resize(n+1,0);pw2.resize(n+1,0);h1.resize(n+1,0);h2.resize(n+1,0);pw1[0]=pw2[0]=1;for(int i=1;i<=n;i++) pw1[i]=pw1[i-1]*base1%mod1,pw2[i]=pw2[i-1]*base2%mod2;for(int i=1;i<=n;i++){h1[i]=(h1[i-1]*base1+s[i])%mod1;h2[i]=(h2[i-1]*base2+s[i])%mod2;}}inline pll gethash(int l,int r){return {(h1[r]-h1[l-1]*pw1[r-l+1]%mod1+mod1)%mod1,(h2[r]-h2[l-1]*pw2[r-l+1]%mod2+mod2)%mod2};}};```