1 /*支持加减乘除、取模、赋值、输入输出*/ 2 const int MAX_SIZE = 5000 + 10; //最大位数 3 4 struct BigInteger 5 { 6 int len, s[MAX_SIZE]; 7 BigInteger() 8 { 9 memset(s, 0, sizeof(s)); 10 len = 1; 11 } 12 BigInteger(int num) { *this = num; } 13 BigInteger(const char *num) { *this = num; } 14 void clean() { while (len > 1 && !s[len - 1])len--; } 15 BigInteger operator = (const int num) 16 { 17 char s[MAX_SIZE]; 18 sprintf(s, "%d", num); 19 *this = s; 20 return *this; 21 } 22 BigInteger operator = (const char *num) 23 { 24 len = strlen(num); 25 for (int i = 0; i < len; i++)s[i] = num[len - i - 1] - '0'; 26 return *this; 27 } 28 BigInteger operator + (const BigInteger& b) 29 { 30 BigInteger c; 31 c.len = 0; 32 for (int i = 0, g = 0; g || i < max(len, b.len); i++) 33 { 34 int x = g; 35 if (i < b.len)x += b.s[i]; 36 if (i < len)x += s[i]; 37 c.s[c.len++] = x % 10; 38 g = x / 10; 39 } 40 return c; 41 } 42 BigInteger operator - (const BigInteger& b) 43 { 44 BigInteger c; 45 c.len = 0; 46 for (int i = 0, g = 0; i < len; i++) 47 { 48 int x = s[i] - g; 49 if (i < b.len)x -= b.s[i]; 50 if (x >= 0)g = 0; 51 else { g = 1; x += 10; } 52 c.s[c.len++] = x; 53 } 54 c.clean(); 55 return c; 56 } 57 BigInteger operator * (const BigInteger& b) 58 { 59 BigInteger c; 60 c.len = len + b.len; 61 for (int i = 0; i < len; i++) 62 { 63 for (int j = 0; j < b.len; j++) 64 { 65 c.s[i + j] += s[i] * b.s[j]; 66 } 67 } 68 for (int i = 0; i < c.len; i++) 69 { 70 c.s[i + 1] += c.s[i] / 10; 71 c.s[i] %= 10; 72 } 73 c.clean(); 74 return c; 75 } 76 BigInteger operator * (const int& b) 77 { 78 BigInteger c; 79 c.len = 0; 80 for (int i = 0, g = 0; g || i < len; i++) 81 { 82 int x; 83 if (i < len)x = s[i] * b + g; 84 else x = g; 85 c.s[c.len++] = x % 10; 86 g = x / 10; 87 } 88 return c; 89 } 90 BigInteger operator / (const BigInteger& b) 91 { 92 BigInteger c, f = 0; 93 for (int i = len - 1; i >= 0; i--) 94 { 95 f = f * 10; 96 f.s[0] = s[i]; 97 while (f >= b) 98 { 99 f = f - b; 100 c.s[i]++; 101 } 102 } 103 c.len = len; 104 c.clean(); 105 return c; 106 } 107 BigInteger operator / (const int& b) 108 { 109 BigInteger c, d = *this; 110 c.len = len; 111 for (int i = len - 1, g = 0; i >= 0; i--) 112 { 113 d.s[i] += g * 10; 114 c.s[i] = d.s[i] / b; 115 g = d.s[i] % b; 116 } 117 c.clean(); 118 return c; 119 } 120 BigInteger operator % (const BigInteger& b) 121 { 122 BigInteger c = *this / b; 123 c = *this - c * b; 124 return c; 125 } 126 BigInteger operator += (const BigInteger& b) 127 { 128 *this = *this + b; return *this; 129 } 130 BigInteger operator -= (const BigInteger& b) 131 { 132 *this = *this - b; return *this; 133 } 134 BigInteger operator *= (const BigInteger& b) 135 { 136 *this = *this*b; return *this; 137 } 138 BigInteger operator /= (const BigInteger& b) 139 { 140 *this = *this / b; return *this; 141 } 142 BigInteger operator *= (const int& b) 143 { 144 *this = *this*b; return *this; 145 } 146 BigInteger operator /= (const int& b) 147 { 148 *this = *this / b; return *this; 149 } 150 BigInteger operator %= (const BigInteger& b) 151 { 152 *this = *this%b; return *this; 153 } 154 bool operator < (const BigInteger& b) 155 { 156 if (b.len != len)return len < b.len; 157 for (int i = len - 1; i >= 0; i--) 158 { 159 if (s[i] != b.s[i])return s[i] < b.s[i]; 160 } 161 return 0; 162 } 163 bool operator > (const BigInteger& b) 164 { 165 if (b.len != len)return len > b.len; 166 for (int i = len - 1; i >= 0; i--) 167 { 168 if (s[i] != b.s[i])return s[i] > b.s[i]; 169 } 170 return 0; 171 } 172 bool operator == (const BigInteger& b) 173 { 174 return !(*this > b) && !(*this < b); 175 } 176 bool operator <= (const BigInteger& b) 177 { 178 return !(*this > b); 179 } 180 bool operator >= (const BigInteger& b) 181 { 182 return !(*this < b); 183 } 184 bool operator != (const BigInteger& b) 185 { 186 return !(*this == b); 187 } 188 string str() const 189 { 190 string res; 191 for (int i = 0; i < len; i++) 192 res = char(s[i] + '0') + res; 193 return res; 194 } 195 }; 196 istream& operator >> (istream&in, BigInteger &x) 197 { 198 string s; 199 in >> s; 200 x = s.c_str(); 201 return in; 202 } 203 ostream& operator << (ostream&out, BigInteger x) 204 { 205 out << x.str(); 206 return out; 207 }