大数模板
发布时间:2020-12-14 02:46:38 所属栏目:大数据 来源:网络整理
导读:#includeiostream#includestring#includecstring#includevector#includecstdio#includecstdlibusing namespace std;struct BigInteger{? ? static const int BASE =1e1;? ? static const int WIDTH = 1;? ? bool f;? ? vectorint s;? ? BigInteger(long long
#include<iostream> #include<string> #include<cstring> #include<vector> #include<cstdio> #include<cstdlib> using namespace std; struct BigInteger { ? ? static const int BASE =1e1; ? ? static const int WIDTH = 1; ? ? bool f; ? ? vector<int> s; ? ? BigInteger(long long num = 0){*this = num;} ? ? BigInteger operator = (long long num) ? ? { ? ? ? ? s.clear(); ? ? ? ? f=0; ? ? ? ? if(num<0) f=1,num=-num; ? ? ? ? if(num==0) ? ? ? ? { ? ? ? ? ? ? s.push_back(0); ? ? ? ? ? ? return *this; ? ? ? ? } ? ? ? ? while(num)? ? ? ? ? { ? ? ? ? ? ? s.push_back(num % BASE); ? ? ? ? ? ? num/=BASE; ? ? ? ? } ? ? ? ? return *this; ? ? } ? ? BigInteger operator = (const string& a) ? ? { ? ? ? ? string str=a; ? ? ? ? s.clear(); ? ? ? ? f=0; ? ? ? ? if (str[0]=='-') ? ? ? ? { ? ? ? ? ? ? f=1; ? ? ? ? ? ? str.erase(0,1); ? ? ? ? } ? ? ? ? int x,len =(str.length()-1)/WIDTH + 1; ? ? ? ? for(int i=0;i<len;i++) ? ? ? ? { ? ? ? ? ? ? int end =str.length() - i*WIDTH; ? ? ? ? ? ? int start = max(0,end - WIDTH); ? ? ? ? ? ? sscanf(str.substr(start,end-start).c_str(),"%d",&x); ? ? ? ? ? ? s.push_back(x); ? ? ? ? } ? ? ? ? while(s.size()!=1&&s[s.size()-1]==0) s.pop_back();? ? ? ? ? if(s.size()==1&&s[0]==0) f=0; ? ? ? ? ?? ? ? ? ? return *this; ? ? } ? ? BigInteger operator + (const BigInteger& b) const ? ? { ? ? ? ? BigInteger c; ? ? ? ? c.s.clear(); ? ? ? ? c.f=0; ? ? ? ? if(f!=b.f) ? ? ? ? { ? ? ? ? ? ? BigInteger l=*this,r=b; ? ? ? ? ? ? l.f=0;r.f=0; ? ? ? ? ? ? if(l==r)? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? c.s.push_back(0); ? ? ? ? ? ? ? ? return c; ? ? ? ? ? ? } ? ? ? ? ? ? if((l>r&&f)||(r>l&&b.f)) c.f=1; ? ? ? ? ? ? if(r>l) swap(l,r); ? ? ? ? ? ? int len = r.s.size(); ? ? ? ? ? ? //cout<<"!!"<<endl; ? ? ? ? ? ? //cout<<"len:"<<len<<endl; ? ? ? ? ? ? for(int i=0;i<len;i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? c.s.push_back(l.s[i]-r.s[i]); ? ? ? ? ? ? ? ? //cout<<"c["<<i<<"]:"<<c.s[i]<<endl; ? ? ? ? ? ? ? ? //cout<<c.s[i]<<endl; ? ? ? ? ? ? ? ? /*if(c.s[i]<0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? --l.s[i+1]; ? ? ? ? ? ? ? ? ? ? c.s[i]+=BASE; ? ? ? ? ? ? ? ? }*/ ? ? ? ? ? ? } ? ? ? ? ? ? for(int i=len;i<l.s.size();i++) c.s.push_back(l.s[i]); ? ? ? ? ? ? for(int i=0;i<c.s.size();i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? while(c.s[i]<0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? --c.s[i+1]; ? ? ? ? ? ? ? ? ? ? c.s[i]+=BASE; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? while(c.s.size()!=1&&c.s[c.s.size()-1]==0) c.s.pop_back(); ? ? ? ? ? ? return c; ? ? ? ? } ? ? ? ? if(f&&b.f) c.f=1;? ? ? ? ? for(int i=0,g=0;;i++) ? ? ? ? { ? ? ? ? ? ? if(g==0 && i>=s.size()&&i>=b.s.size()) break; ? ? ? ? ? ? int x=g; ? ? ? ? ? ? if(i<s.size()) x+=s[i]; ? ? ? ? ? ? if(i<b.s.size()) x+=b.s[i]; ? ? ? ? ? ? c.s.push_back(x%BASE); ? ? ? ? ? ? g=x/BASE; ? ? ? ? } ? ? ? ? return c; ? ? } ? ? BigInteger operator += (const BigInteger& b) ? ? { ? ? ? ? *this =*this +b; ? ? ? ? return *this; ? ? } ? ? bool operator < (const BigInteger& b) const ? ? { ? ? ? ? if(f>b.f) return true; ? ? ? ? if(f<b.f) return false; ? ? ? ? if(f&&b.f) ? ? ? ? { ? ? ? ? ? ? BigInteger l=*this,r=b; ? ? ? ? ? ? l.f=0;r.f=0; ? ? ? ? ? ? return l>r; ? ? ? ? } ? ? ? ? if(s.size()!=b.s.size()) return s.size()< b.s.size(); ? ? ? ? for(int i= s.size()-1;i>=0;--i) ? ? ? ? ? ? if(s[i]!=b.s[i]) return s[i]< b.s[i]; ? ? ? ? return false; ? ? } ? ? bool operator > (const BigInteger& b) const{return b<*this;} ? ? bool operator <= (const BigInteger& b) const{return !(b<*this);} ? ? bool operator >= (const BigInteger& b) const{return !(*this<b);} ? ? bool operator != (const BigInteger& b) const{return b<*this||*this<b;} ? ? bool operator == (const BigInteger& b) const{return !(b<*this)&&!(*this<b);} ? ? BigInteger operator - (const BigInteger& b) const ? ? { ? ? ? ? //cout<<"@"<<endl; ? ? ? ? BigInteger r=b; ? ? ? ? r.f=!r.f; ? ? ? ? return *this + r; ? ? } ? ? BigInteger operator -= (const BigInteger& b)? ? ? { ? ? ? ? *this=*this - b; ? ? ? ? return *this; ? ? } ? ? BigInteger operator * (const BigInteger& b) const ? ? { ? ? ? ? BigInteger c; ? ? ? ? c.s.clear(); ? ? ? ? long long l; ? ? ? ? int lena=s.size(),lenb=b.s.size(); ? ? ? ? for(int i=0;i<lena;i++) ? ? ? ? ? ? for(int j=0;j<lenb;j++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //cout<<"l:"<<l<<endl; ? ? ? ? ? ? ? ? l=s[i]*b.s[j]; ? ? ? ? ? ? ? ? if(c.s.size()<=i+j) c.s.push_back(l%BASE); ? ? ? ? ? ? ? ? else c.s[i+j]+=l%BASE; ? ? ? ? ? ? ? ? l/=BASE; ? ? ? ? ? ? ? ? if(c.s.size()<=i+j+1) c.s.push_back(l); ? ? ? ? ? ? ? ? else c.s[i+j+1]+=l; ? ? ? ? ? ? } ? ? ? ? ? ? for(int i=0;i<c.s.size();i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(c.s[i]<BASE) continue; ? ? ? ? ? ? ? ? if(c.s.size()<=i+1) c.s.push_back(c.s[i]/BASE); ? ? ? ? ? ? ? ? else c.s[i+1]+=c.s[i]/BASE; ? ? ? ? ? ? ? ? ? ? c.s[i]%=BASE; ? ? ? ? ? ? } ? ? ? ? ? ? while(c.s[c.s.size()-1]==0&&c.s.size()!=1) c.s.pop_back(); ? ? ? ? ? ? if(f==b.f) c.f=0;else c.f=1; ? ? ? ? ? ? if(c.s.size()==1&&c.s[0]==0) c.f=0; ? ? ? ? ? ? return c; ? ? } ? ? BigInteger operator *= (const BigInteger& b) ? ? { ? ? ? ? *this=*this * b; ? ? ? ? return *this; ? ? } ? ? BigInteger operator / (const BigInteger& b) const ? ? { ? ? ? ? BigInteger c,zero; ? ? ? ? if(*this==c||b==c) return c; ? ? ? ? if(*this==b) c=1; ? ? ? ? else if(*this>b) ? ? ? ? { ? ? ? ? ? ? BigInteger Tmp=*this,tmp=b; ? ? ? ? ? ? Tmp.f=0;tmp.f=0; ? ? ? ? ? ? int l=s.size(),r=b.s.size(),j=0; ? ? ? ? ? ? for(int i=0;i<l;i++) c.s.push_back(0); ? ? ? ? ? ? while(Tmp>=tmp) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? l=Tmp.s.size();r=b.s.size();j=0; ? ? ? ? ? ? ? ? //cout<<l<<" "<<r<<endl; ? ? ? ? ? ? ? ? if(l-r>1) ? tmp.s.insert(tmp.s.begin(),l-r-1,0),j=l-r-1; ? ? ? ? ? ? ? ? //cout<<Tmp.s[1]<<Tmp.s[0]<<"||"<<tmp.s.size()<<tmp.s[0]<<endl; ? ? ? ? ? ? ? ? ? ? ? ? while(Tmp>=tmp) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //cout<<"!"<<endl; ? ? ? ? ? ? ? ? ? ? Tmp=Tmp-tmp; ? ? ? ? ? ? ? ? ? ? c.s[j]++; ? ? ? ? ? ? ? ? ? ? //cout<<"j:"<<j<<endl; ? ? ? ? ? ? ? ? ? ? if(c.s[j]>10) break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? tmp=b; ? ? ? ? ? ? } ? ? ? ? ? ? for(int i=0;i<c.s.size();i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(c.s[i]>=BASE) ? ? ? ? ? ? ? ? ? ? if(i>=c.s.size()-1) c.s.push_back(c.s[i]/BASE); ? ? ? ? ? ? ? ? ? ? else c.s[i+1]+=c.s[i]/BASE; ? ? ? ? ? ? ? ? c.s[i]%=BASE; ? ? ? ? ? ? } ? ? ? ? ? ? while(c.s.size()!=1&&c.s[c.s.size()-1]==0) c.s.pop_back(); ? ? ? ? } ? ? ? ? if(f!=b.f&&c!=zero) c.f=1; ? ? ? ? return c; ? ? } ? ? BigInteger operator /= (const BigInteger& b) ? ? { ? ? ? ? *this=*this / b; ? ? ? ? return *this; ? ? } }; ostream& operator << (ostream &out,const BigInteger x) { ? ? if(x.f) out <<'-'; ? ? out << x.s.back(); ? ? for(int i =x.s.size()-2;i>=0;i--) ? ? { ? ? ? ? char buf[20]; ? ? ? ? sprintf(buf,"%01d",x.s[i]); ? ? ? ? for(int j=0;j<strlen(buf);j++) out << buf[j]; ? ? } ? ? return out; }; istream& operator >> (istream &in,BigInteger& x) { ? ? string s; ? ? if(!(in >> s)) return in; ? ? x = s; ? ? return in; } int main() { BigInteger a,b; while(cin>>a>>b) { cout<<a*b<<endl; if(a>=b) cout<<a<<">="<<b; cout<<endl; if(a<=b) cout<<a<<"<="<<b; cout<<endl; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |