高精度模板

版权声明:本文转自BeiYu-oi's Blog 转载请保留该文字

 

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<vector>
  4 #include<iostream>
  5 using namespace std;
  6 struct Big {
  7     static const int BASE = 100000000; // change
  8     static const int WIDTH = 8; // change
  9     /*
 10       WIDTH可随需求不同而变动:
 11        ①:没有乘除mod时,WIDTH为8时效率最高,在8以上可能出错。
 12        ②:WIDTH增大时,加减乘都会变快,但除和mod会变慢,所以没有除和mod时,尽量把WIDTH开大,有乘法时最大为4。
 13        ③:如果乘数都很小像2,3,4之类的数,WIDTH也可以开到8。 
 14        ④:如果运算符要求很全,那么WIDTH为3比较合适。
 15       如果要改WIDTH的话,需要改三处地方,已在代码中标记上“change”。 
 16     */
 17     vector<int> s;
 18     Big(long long num = 0){*this=num;}
 19     Big operator = (long long num) {
 20         s.clear();
 21         do {
 22             s.push_back(num%BASE);
 23             num/=BASE;
 24         }while(num>0);
 25         return *this;
 26     }
 27     Big operator = (const string& str) {
 28         s.clear();
 29         int x,len=(str.length()-1)/WIDTH+1;
 30         for(int i=0;i<len;i++) {
 31             int end=str.length()-i*WIDTH;
 32             int start=max(0,end-WIDTH);
 33             sscanf(str.substr(start,end-start).c_str(),"%d",&x);
 34             s.push_back(x);
 35         }
 36         return *this;
 37     }
 38     Big operator + (const Big& b) const {
 39         Big c;
 40         c.s.clear();
 41         for(int i=0,g =0;;i++) {
 42             if(g==0&&i>=s.size()&&i>=b.s.size()) break;
 43             int x=g;
 44             if(i<s.size()) x+=s[i];
 45             if(i<b.s.size()) x+=b.s[i];
 46             c.s.push_back(x%BASE);
 47             g=x/BASE;
 48         }
 49         return c;
 50     }
 51     Big operator - (const Big& b) const {  
 52         Big c;
 53         c.s.clear();
 54         int i,g,n=s.size(),m=b.s.size();
 55         for(i=0,g=0;i<n;i++) {
 56             int x=s[i]-g;  
 57             if(i<m) x-=b.s[i];  
 58             if(x>=0) g=0;  
 59             else {  
 60                 g=1;  
 61                 x+=BASE;
 62             }
 63             c.s.push_back(x);  
 64         }
 65         i=c.s.size()-1;
 66         while(c.s[i]==0&&i) c.s.pop_back(),i--;
 67         return c; 
 68     }
 69     Big operator * (const Big &b) const {
 70         Big c;
 71         int i,j,n=s.size(),m=b.s.size(),size=m+n;
 72         c.s.resize(size,0);
 73         for(i=0;i<n;i++)
 74             for(j=0;j<m;j++)
 75                 c.s[i+j] += s[i] * b.s[j];
 76         for(i=0;i<size;i++) {
 77             c.s[i+1]+=c.s[i]/BASE;
 78             c.s[i]%=BASE;
 79         }
 80         i=size-1;
 81         while(c.s[i]==0&&i) c.s.pop_back(),i--;
 82         return c;
 83     }
 84     Big operator / (const Big& b) const {
 85         Big c,f=0;
 86         int n=s.size(),i;
 87         c.s.resize(n,0);
 88         for(i=n-1;i>=0;i--) {
 89             f=f*BASE;
 90             f.s[0]=s[i];
 91             while(f>=b) {
 92                 f-=b;
 93                 c.s[i]++;
 94             }
 95         }
 96         i=n-1;
 97         while(c.s[i]==0&&i) c.s.pop_back(),i--;
 98         return c;
 99     }
100     Big operator % (const Big &b) const {
101         Big r=*this/b;
102         r=*this-r*b;
103         return r;
104     }
105 
106     Big operator += (const Big& b){*this=*this+b;return *this;}
107     Big operator -= (const Big& b){*this=*this-b;return *this;}
108     Big operator *= (const Big& b){*this=*this*b;return *this;}
109     Big operator /= (const Big& b){*this=*this/b;return *this;}
110     Big operator %= (const Big& b){*this=*this%b;return *this;}
111 
112     bool operator < (const Big& b) const {
113         int n=s.size(),m=b.s.size();
114         if(m>n) return 1;
115         if(m<n) return 0;
116         for(int i=s.size()-1;i>=0;i--) {
117             if(s[i]<b.s[i]) return 1;
118             if(s[i]>b.s[i]) return 0;
119         }
120         return 0;
121     }
122     bool operator > (const Big& b) const {return b<*this;}
123     bool operator >= (const Big& b) const {return !(*this<b);}
124     bool operator <= (const Big& b) const {return !(b<*this);}
125     bool operator == (const Big& b) const {return !(*this<b)&&!(b<*this);}
126     bool operator != (const Big& b) const {return (*this<b)||(b<*this);}
127 };
128 
129 ostream& operator << (ostream &out, const Big& x) {
130     out<<x.s.back();
131     for(int i=x.s.size()-2;i>=0;i--) {
132         char buf[10];
133         sprintf(buf,"%08d",x.s[i]); // change
134         for(int j=0;j<strlen(buf);j++) out<<buf[j];
135     }
136     return out;
137 }
138 istream& operator >> (istream &in,Big& x) {
139     string s;
140     if(!(in>>s)) return in;
141     x=s;
142     return in;
143 }
144 int main() {
145     ios_base::sync_with_stdio(0); // 关闭流同步可以变快些 
146     Big a,b;
147     cin>>a>>b;
148     cout<<a<<endl;
149     cout<<b<<endl;
150     cout<<a+b<<endl;
151     cout<<a-b<<endl;
152     cout<<a*b<<endl;
153     cout<<a/b<<endl;
154     cout<<a%b<<endl;
155     if(a<b) cout<<"Yes"<<endl;
156     else cout<<"No"<<endl;
157     return 0;
158 }

 

posted @ 2016-09-11 10:20  张瑯小强  阅读(577)  评论(0编辑  收藏  举报