1. 程式人生 > >大數模板(正整數)

大數模板(正整數)

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 #define MAXN 9999
  5 #define MAXSIZE 500   //最大長度
  6 #define DLEN 4
  7 
  8 class BigNum
  9 {
 10 private:
 11     int a[210];    //控制大數的位數
 12     int len;       //長度
 13 public:
 14     BigNum(){ len = 1;memset(a,0,sizeof(a)); }   //
建構函式 15 void XD(); 16 BigNum(const int); 17 BigNum(const long long int); 18 BigNum(const char*); 19 BigNum(const string &); 20 BigNum(const BigNum &); //拷貝建構函式 21 BigNum &operator = (const BigNum &); //過載賦值運算子 22 BigNum &operator = (const
int &); 23 BigNum &operator = (const long long int &); 24 25 friend istream& operator>>(istream&, BigNum&); //過載輸入運算子 26 friend ostream& operator<<(ostream&, BigNum&); //過載輸出運算子 27 28 BigNum operator << (const int
&) const; 29 BigNum operator >> (const int &) const; 30 31 BigNum operator + (const BigNum &) const; //過載加法運算子,大數加大數 32 BigNum operator - (const BigNum &) const; //過載減法運算子,大數減大數 33 BigNum operator * (const BigNum &) const; //過載乘法運算子,大數乘大數 34 BigNum operator / (const int &) const; //過載除法運算子,大數除整數 35 BigNum operator ^ (const int &) const; //大數的n次方 36 int operator % (const int &) const; //大數對int取模 37 bool operator > (const BigNum& b)const; //過載大於 38 bool operator < (const BigNum& b) const; //過載小於 39 40 BigNum operator + (const int& b) {BigNum t = b; *this = *this+t; return *this;} 41 BigNum operator - (const int& b) {BigNum t = b; *this = *this-t; return *this;} 42 BigNum operator * (const int& b) {BigNum t = b; *this = (*this)*t; return *this;} 43 bool operator < (const int& b) const; 44 bool operator > (const int& b) const; 45 46 bool operator <= (const BigNum& b) const{return !(b < *this);} 47 bool operator >= (const BigNum& b) const{return !(*this < b);} 48 bool operator != (const BigNum& b) const{return b < *this || *this < b;} 49 bool operator == (const BigNum& b) const{return !(b < *this) && !(b > *this);} 50 51 bool operator >= (const int& b) const{return !(*this < b);} 52 bool operator <= (const int& b) const{return *this < b || *this == b;} 53 bool operator != (const int& b) const{return *this > b || *this < b;} 54 bool operator == (const int& b) const{return !(*this != b);} 55 56 BigNum& operator += (const BigNum& b) {*this = *this+b; return *this;} 57 BigNum& operator -= (const BigNum& b) {*this = *this-b; return *this;} 58 BigNum& operator *= (const BigNum& b) {*this = *this*b; return *this;} 59 BigNum& operator /= (const int& b) {*this = *this/b; return *this;} 60 BigNum& operator %= (const int& b) {*this = *this/b; return *this;} 61 BigNum& operator += (const int& b) {*this = *this+b; return *this;} 62 BigNum& operator -= (const int& b) {*this = *this-b; return *this;} 63 BigNum& operator *= (const int& b) {*this = *this*b; return *this;} 64 BigNum& operator ^= (const int& b) {*this = *this^b; return *this;} 65 66 //下面是關於long long的過載 不用可以刪除相關仿函式 67 BigNum operator + (const long long int& b) {BigNum t = b; *this = *this+t; return *this;} 68 BigNum operator - (const long long int& b) {BigNum t = b; *this = *this-t; return *this;} 69 BigNum operator * (const long long int& b) {BigNum t = b; *this = (*this)*t; return *this;} 70 BigNum operator / (const long long int& b) const; 71 long long int operator % (const long long int& b) const; 72 bool operator > (const long long int& b) const; 73 bool operator < (const long long int& b) const; 74 75 BigNum& operator /= (const long long int& b) {*this = *this/b; return *this;} 76 BigNum& operator %= (const long long int& b) {*this = *this/b; return *this;} 77 BigNum& operator += (const long long int& b) {*this = *this+b; return *this;} 78 BigNum& operator -= (const long long int& b) {*this = *this-b; return *this;} 79 BigNum& operator *= (const long long int& b) {*this = *this*b; return *this;} 80 81 bool operator >= (const long long int& b) const{return !(*this < b);} 82 bool operator <= (const long long int& b) const{return *this < b || *this == b;} 83 bool operator != (const long long int& b) const{return *this > b || *this < b;} 84 bool operator == (const long long int& b) const{return !(*this != b);} 85 86 BigNum operator ++ (int) {BigNum t = *this; *this += 1; return t;} 87 BigNum operator -- (int) {BigNum t = *this; *this -= 1; return t;} 88 BigNum& operator -- () {*this -= 1; return *this;} 89 BigNum& operator ++ () {*this += 1; return *this;} 90 91 BigNum& operator <<= (const int& b) {*this = *this << b; return *this;} 92 BigNum& operator >>= (const int& b) {*this = *this >> b; return *this;} 93 94 void print(); //輸出大數 95 int Size(); //返回大數長度 96 int the_first(); //返回第一個數字 97 int the_last(); //返回最後一位數字 98 int to_int(); //轉化為整數 99 long long int to_long(); 100 string to_String(); //轉化為string型別 101 //char* to_char(); 102 }; 103 104 BigNum operator + (const int& a, BigNum& b) {return b+a;} 105 BigNum operator + (const long long int& a, BigNum& b) {return b+a;} 106 BigNum operator - (const int& a, BigNum& b) {BigNum t(a); t -= b; return t;} 107 BigNum operator - (const long long int& a, BigNum& b) {BigNum t(a); t -= b; return t;} 108 BigNum operator * (const int& a, BigNum& b) {return b*a;} 109 BigNum operator * (const long long int& a, BigNum& b) {return b*a;} 110 bool operator < (const int& a, BigNum& b) {return b>a;} 111 bool operator < (const long long int& a, BigNum& b) {return b>a;} 112 bool operator > (const int& a, BigNum& b) {return b<a;} 113 bool operator > (const long long int& a, BigNum& b) {return b<a;} 114 bool operator <= (const int& a, BigNum& b) {return b>=a;} 115 bool operator <= (const long long int& a, BigNum& b) {return b>=a;} 116 bool operator >= (const int& a, BigNum& b) {return b<=a;} 117 bool operator >= (const long long int& a, BigNum& b) {return b<=a;} 118 bool operator == (const int& a, BigNum& b) {return b==a;} 119 bool operator == (const long long int& a, BigNum& b) {return b==a;} 120 bool operator != (const int& a, BigNum& b) {return b!=a;} 121 bool operator != (const long long int& a, BigNum& b) {return b!=a;} 122 123 BigNum::BigNum(const int b) //將一個int型別的變數轉化為大數 124 { 125 int c,d = b; 126 len = 0; 127 memset(a,0,sizeof(a)); 128 while(d > MAXN){ 129 c = d - (d / (MAXN+1)) * (MAXN+1); 130 d = d / (MAXN+1); 131 a[len++] = c; 132 } 133 a[len++] = d; 134 } 135 BigNum::BigNum(const long long int b) 136 { 137 long long int c,d = b; 138 len = 0; 139 memset(a,0,sizeof(a)); 140 while(d > MAXN){ 141 c = d - (d / (MAXN+1)) * (MAXN+1); 142 d = d / (MAXN+1); 143 a[len++] = c; 144 } 145 a[len++] = d; 146 } 147 BigNum::BigNum(const string& s) 148 { 149 int t,k,index,l,i; 150 memset(a,0,sizeof(a)); 151 l = s.size(); 152 len = l/DLEN; 153 if(l%DLEN) 154 len++; 155 index = 0; 156 for(i = l-1; i >=0 ;i -= DLEN){ 157 t = 0; 158 k = i-DLEN+1; 159 if(k < 0) k = 0; 160 for(int j = k; j <= i; j++) 161 t = t*10 + s[j]-'0'; 162 a[index++] = t; 163 } 164 } 165 BigNum::BigNum(const char* s) //將一個字串型別的變數轉化為大數 166 { 167 int t,k,index,l,i; 168 memset(a,0,sizeof(a)); 169 l = strlen(s); 170 len = l/DLEN; 171 if(l%DLEN) 172 len++; 173 index = 0; 174 for(i = l-1; i >= 0; i -= DLEN){ 175 t = 0; 176 k = i - DLEN + 1; 177 if(k < 0) k = 0; 178 for(int j = k; j <= i; j++) 179 t = t*10 + s[j] - '0'; 180 a[index++] = t; 181 } 182 } 183 BigNum::BigNum(const BigNum & b) : len(b.len) //拷貝建構函式 184 { 185 memset(a,0,sizeof(a)); 186 for(int i = 0 ; i < len ; i++) 187 a[i] = b.a[i]; 188 } 189 BigNum & BigNum::operator = (const BigNum& n) //過載賦值運算子,大數之間進行賦值運算 190 { 191 len = n.len; 192 memset(a,0,sizeof(a)); 193 for(int i = 0 ; i < len ; i++) 194 a[i] = n.a[i]; 195 return *this; 196 } 197 BigNum & BigNum::operator = (const int& num) 198 { 199 BigNum t(num); 200 *this = t; 201 return *this; 202 } 203 BigNum & BigNum::operator = (const long long int& num) 204 { 205 BigNum t(num); 206 *this = t; 207 return *this; 208 } 209 void XD() 210 { 211 cout << "A hidden egg! Good luck for u!" << endl; 212 } 213 istream& operator >> (istream & in, BigNum & b) //過載輸入運算子 214 { 215 char ch[MAXSIZE*4]; 216 int i = -1; 217 in>>ch; 218 int l = strlen(ch); 219 int cnt = 0, sum = 0; 220 for(i = l-1; i >= 0; ){ 221 sum = 0; 222 int t = 1; 223 for(int j = 0; j < 4 && i >= 0; j++,i--,t *= 10) 224 sum += (ch[i]-'0')*t; 225 b.a[cnt] = sum; 226 cnt++; 227 } 228 b.len = cnt++; 229 return in; 230 231 } 232 ostream& operator << (ostream& out, BigNum& b) //過載輸出運算子 233 { 234 int i; 235 cout << b.a[b.len - 1]; 236 for(i = b.len - 2 ; i >= 0 ; i--){ 237 cout.width(DLEN); 238 cout.fill('0'); 239 cout << b.a[i]; 240 } 241 return out; 242 } 243 244 BigNum BigNum::operator << (const int& b) const 245 { 246 int temp = 1; 247 for(int i = 0; i < b; i++) 248 temp *= 2; 249 BigNum t = (*this) * temp; 250 return t; 251 } 252 BigNum BigNum::operator >> (const int& b) const 253 { 254 int temp = 1; 255 for(int i = 0; i < b; i++) 256 temp *= 2; 257 BigNum t = (*this) / temp; 258 return t; 259 } 260 261 BigNum BigNum::operator + (const BigNum& b) const //兩個大數之間的相加運算 262 { 263 BigNum t(*this); 264 int i,big; 265 big = b.len > len ? b.len : len; 266 for(i = 0 ; i < big ; i++){ 267 t.a[i] += b.a[i]; 268 if(t.a[i] > MAXN){ 269 t.a[i + 1]++; 270 t.a[i] -=MAXN+1; 271 } 272 } 273 if(t.a[big] != 0) 274 t.len = big + 1; 275 else 276 t.len = big; 277 return t; 278 } 279 BigNum BigNum::operator - (const BigNum& b) const //兩個大數之間的相減運算 280 { 281 int i,j,big; 282 bool flag; 283 BigNum t1,t2; 284 if(*this>b){ 285 t1 = *this; 286 t2 = b; 287 flag = 0; 288 } 289 else{ 290 t1 = b; 291 t2 = *this; 292 flag = 1; 293 } 294 big = t1.len; 295 for(i = 0 ; i < big ; i++){ 296 if(t1.a[i] < t2.a[i]){ 297 j = i + 1; 298 while(t1.a[j] == 0) 299 j++; 300 t1.a[j--]--; 301 while(j > i) 302 t1.a[j--] += MAXN; 303 t1.a[i] += MAXN + 1 - t2.a[i]; 304 } 305 else 306 t1.a[i] -= t2.a[i]; 307 } 308 t1.len = big; 309 while(t1.a[t1.len - 1] == 0 && t1.len > 1){ 310 t1.len--; 311 big--; 312 } 313 if(flag) 314 t1.a[big-1] = 0-t1.a[big-1]; 315 return t1; 316 } 317 318 BigNum BigNum::operator * (const BigNum& b) const //兩個大數之間的相乘運算 319 { 320 BigNum ret; 321 int i,j,up; 322 int temp,temp1; 323 for(i = 0 ; i < len ; i++){ 324 up = 0; 325 for(j = 0 ; j < b.len ; j++){ 326 temp = a[i] * b.a[j] + ret.a[i + j] + up; 327 if(temp > MAXN){ 328 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1); 329 up = temp / (MAXN + 1); 330 ret.a[i + j] = temp1; 331 } 332 else{ 333 up = 0;