1 #include <iostream>
2 #include <algorithm>
3 #include <cstdio>
4 #include <string>
5 #include <string.h>
6 #include <vector>
7 using namespace std;
8 struct BigInteger
9 {
10 static const int BASE = 10;
11 static const int WIDTH = 1;
12 vector<int> s;
13 bool flag;
14 BigInteger(long long num = 0) { *this = num; } // 构造函数
15 BigInteger operator=(long long num)
16 { // 赋值运算符
17 s.clear();
18 if (num < 0)
19 {
20 flag = 0;
21 num = -num;
22 }
23 else
24 flag = 1;
25 do
26 {
27 s.push_back(num % BASE);
28 num /= BASE;
29 } while (num > 0);
30 return *this;
31 }
32 BigInteger operator=(const string &str)
33 { // 赋值运算符
34 s.clear();
35 if (str[0] == '-')
36 flag = 0;
37 else
38 flag = 1;
39 string strr = str;
40 if (flag == 0)
41 strr.erase(0, 1);
42 int x, len = (strr.length() - 1) / WIDTH + 1;
43 int i = 0;
44 for (; i < len; i++)
45 {
46 int end = strr.length() - i * WIDTH;
47 int start = max(0, end - WIDTH);
48 sscanf(strr.substr(start, end - start).c_str(), "%d", &x);
49 s.push_back(x);
50 }
51 return *this;
52 }
53 BigInteger operator+(const BigInteger &b) const
54 {
55 BigInteger c, d;
56 if (b.flag == 0)
57 {
58 BigInteger B = b;
59 B.flag = 1;
60 if (flag == 0)
61 {
62 d = *this;
63 d.flag = 1;
64 c = d + B;
65 c.flag = 0;
66 return c;
67 }
68 return *this - B;
69 }
70 if (flag == 0)
71 {
72 c = *this;
73 c.flag = 1;
74 return b - c;
75 }
76 c.s.clear();
77 for (int i = 0, g = 0;; i++)
78 {
79 if (g == 0 && i >= s.size() && i >= b.s.size())
80 break;
81 int x = g;
82 if (i < s.size())
83 x += s[i];
84 if (i < b.s.size())
85 x += b.s[i];
86 c.s.push_back(x % BASE);
87 g = x / BASE;
88 }
89 return c;
90 }
91
92 BigInteger operator*(const BigInteger &b) const
93 {
94 BigInteger c, t;
95 c = 0;
96 int flagg;
97 if (b.flag == flag)
98 flagg = 1;
99 else
100 flagg = 0;
101 t.flag = 1;
102 c.flag = 1;
103 for (int i = 0; i < s.size(); i++)
104 {
105 t.s.clear();
106 long long x, g;
107 int j;
108 t.s.insert(t.s.begin(), i, 0);
109 for (j = g = 0;; j++)
110 {
111 x = g;
112 if (g == 0 && j >= b.s.size())
113 {
114 break;
115 }
116 if (j < b.s.size())
117 x += (long long)s[i] * (long long)b.s[j];
118 t.s.push_back(x % BASE);
119 g = x / BASE;
120 }
121 c = c + t;
122 }
123 c.flag = flagg;
124 if (c.s.back() == 0)
125 {
126 vector<int>::iterator it = c.s.end();
127 for (it--; it != c.s.begin(); it--)
128 {
129 if (*it != 0)
130 break;
131 }
132 it++;
133 c.s.erase(it, c.s.end());
134 }
135 if (c.s.empty())
136 c.s.push_back(0);
137 return c;
138 }
139 BigInteger operator*(const int &b) const
140 {
141 BigInteger c = b;
142 return c * (*this);
143 }
144 BigInteger operator*(const long long int &b) const
145 {
146 BigInteger c = b;
147 return c * (*this);
148 }
149 BigInteger operator^(int b) const
150 {
151 BigInteger ans = 1, base = *this;
152 while (b)
153 {
154 if (b & 1)
155 {
156 ans = base * ans;
157 }
158 base = base * base;
159 b >>= 1;
160 }
161 return ans;
162 }
163 BigInteger operator+=(const BigInteger &b) const
164 {
165 return *this + b;
166 }
167 bool operator<(const BigInteger &b) const
168 {
169 if (b.flag == 0 && b.flag != flag)
170 return false;
171 if (s.size() != b.s.size())
172 return s.size() < b.s.size();
173 for (int i = s.size() - 1; i >= 0; i--)
174 if (s[i] != b.s[i])
175 return s[i] < b.s[i];
176 return false;
177 }
178 bool operator>(const BigInteger &b) const { return b < *this; }
179 bool operator<=(const BigInteger &b) const { return !(b < *this); }
180 bool operator>=(const BigInteger &b) const { return !(*this < b); }
181 bool operator!=(const BigInteger &b) const { return b < *this || *this < b; }
182 bool operator==(const BigInteger &b) const { return !(b < *this) && !(*this < b); }
183
184 BigInteger operator-(const BigInteger &b) const
185 {
186 if (b.flag == 0)
187 {
188 BigInteger B = b;
189 B.flag = 1;
190 return *this + B;
191 }
192 BigInteger A, B;
193 A = *this;
194 B = b;
195 A.flag = 1;
196 B.flag = 1;
197 BigInteger c;
198 if (A < B)
199 {
200 swap(A, B);
201 c.flag = 0;
202 }
203 c.s.clear();
204 int i, g, x;
205 i = g = 0;
206 while (1) //for (int i = 0, g = 0;; i++)
207 {
208 if (i >= A.s.size() && i >= B.s.size())
209 break;
210 x = A.s[i];
211 x -= g;
212 g = 0;
213 if (i < B.s.size())
214 x -= B.s[i];
215 if (x < 0)
216 {
217 x += BASE;
218 g = 1;
219 }
220 c.s.push_back(x % BASE);
221 i++;
222 }
223 if (c.s.back() == 0)
224 {
225 vector<int>::iterator it = c.s.end();
226 for (it--; it != c.s.begin(); it--)
227 {
228 if (*it != 0)
229 break;
230 }
231 it++;
232 c.s.erase(it, c.s.end());
233 }
234 if (c.s.empty())
235 c.s.push_back(0);
236 return c;
237 }
238 BigInteger operator-(const int &b) const
239 {
240 BigInteger c = b;
241 return *this - c;
242 }
243 BigInteger operator-(const long long int &b) const
244 {
245 BigInteger c = b;
246 return *this - c;
247 }
248 BigInteger operator-=(const BigInteger &b) const { return *this - b; }
249 BigInteger operator!(void)const //阶乘
250 {
251 BigInteger ans = (long long)1;
252 for (BigInteger i = ans; i <= *this; i = i + (long long)1)
253 {
254 ans = i * ans;
255 }
256 return ans;
257 }
258 BigInteger operator/(const BigInteger &b) const
259 {
260 //A/B
261 BigInteger A, B, c, ans;
262 if (b.flag == flag)
263 ans.flag = 1;
264 else
265 ans.flag = 0;
266 if (b > *this)
267 return 0;
268 if (b == *this)
269 return 1;
270 c.s.clear();
271 ans.s.clear();
272 A = *this;
273 B = b;
274 A.flag = B.flag = c.flag = 1;
275 int zero = (A.s.size() - b.s.size());
276 B.s.insert(B.s.begin(), zero, 0);
277 while (!B.s.empty() && B >= b)
278 {
279 int x = 0;
280 while (A >= B)
281 {
282 A = A - B;
283 x++;
284 }
285 c.s.push_back(x);
286 B.s.erase(B.s.begin());
287 }
288 if (!c.s.empty())
289 for (vector<int>::reverse_iterator it = c.s.rbegin(); it != c.s.rend(); it++)
290 {
291 ans.s.push_back(*it);
292 }
293 else
294 ans.s.push_back(0);
295 if (ans.s.empty())
296 ans.s.push_back(0);
297 if (ans.s.back() == 0)
298 {
299 vector<int>::iterator it = ans.s.end();
300 for (it--; it != ans.s.begin(); it--)
301 {
302 if (*it != 0)
303 break;
304 }
305 it++;
306 ans.s.erase(it, ans.s.end());
307 }
308 /***************
309 A 为余数
310 ***************/
311 return ans;
312 }
313 BigInteger operator%(const long long &b) const
314 {
315 BigInteger c;
316 long long x;
317 c.s.clear();
318 for (int i = 0; i < s.size(); i++)
319 {
320 x = s[i] % b;
321 c.s.push_back(x);
322 }
323 c.flag = 1;
324 if (b < 0 && flag != 0)
325 c.flag = 0;
326 if (b > 0 && flag == 0)
327 c.flag = 0;
328 return c;
329 }
330 BigInteger operator%(const BigInteger &b) const
331 {
332 //A/B
333 BigInteger A, B;
334 bool flagg;
335 if (b > *this)
336 return 0;
337 if (b == *this)
338 return 1;
339 A = *this;
340 B = b;
341 if (A.flag == B.flag)
342 flagg = 1;
343 else
344 flagg = 0;
345 A.flag = B.flag = 1;
346 int zero = (A.s.size() - b.s.size());
347 B.s.insert(B.s.begin(), zero, 0);
348 while (!B.s.empty() && B >= b)
349 {
350 while (A >= B)
351 {
352 A = A - B;
353 }
354 B.s.erase(B.s.begin());
355 }
356 /***************
357 A 为余数
358 ***************/
359 A.flag = flagg;
360 return A;
361 }
362 };
363 ostream &operator<<(ostream &out, const BigInteger &x)
364 {
365 if (x.flag == 0)
366 out << '-';
367 out << x.s.back();
368 for (int i = x.s.size() - 2; i >= 0; i--)
369 {
370 char buf[20];
371 sprintf(buf, "%d", x.s[i]);
372 for (int j = 0; j < strlen(buf); j++)
373 out << buf[j];
374 }
375 return out;
376 }
377 istream &operator>>(istream &in, BigInteger &x)
378 {
379 string s;
380 if (!(in >> s))
381 return in;
382 x = s;
383 return in;
384 }