主要参考《算法竞赛入门经典》第五章。另外还参考了很多别人的博客,魔改的乱七八糟,因此对于除加法以外的四则运算的可靠性都不能保证。还有,用这个会很费时间,最好一般还是不要用,正常的题目应该都会有用long long肯定能解决的方法。

警告

再看一遍引言!

不能有负数。

可能有各种bug。

模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include<iostream>
#include<sstream>
#include<vector>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct big_int{
static const int BASE = 100000000;
static const int WIDTH = 8;
vector<int> s;
//赋值函数
big_int(long long num = 0){
*this = num;
}
//赋值运算符(long long)
big_int operator = (long long num){
s.clear();
do{
s.push_back(num%BASE);
num /= BASE;
}while(num > 0);
return *this;
}
//赋值运算符(string)
big_int operator = (const string& str){
s.clear();
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);
}
return *this;
}
//四则运算
//+
big_int operator + (const big_int& b) const{
big_int c;
c.s.clear();
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;
}
big_int operator += (const big_int& b){
*this = *this + b;
return *this;
}
//-
big_int operator - (const big_int& b){
big_int c;
c.s.clear();
int MAX = max(s.size(),b.s.size());
for(int i = 0 , g = 0 ; ; i++){
if(g == 0 && i >= MAX)
break;
int x = g;
if(i < s.size())
x += s[i];
if(i < b.s.size())
x -= b.s[i];
if(i == MAX-1)
c.s.push_back(x%BASE);
else
c.s.push_back(abs(x%BASE));
g=x / BASE;
}
while(c.s.back() == 0){
c.s.erase(c.s.end()-1);
}
return c;
}
big_int operator -= (const big_int& b){
*this = *this - b;
return *this;
}
//*
big_int operator * (const big_int& b){
stringstream ss;
for(int i = s.size()-1 ; i >= 0 ; i--)
ss << s[i];
string operand1 = ss.str();
ss.str("");
for(int i = b.s.size()-1 ; i >= 0 ; i--)
ss << b.s[i];
string operand2 = ss.str();
vector<int> c, d, temp;
for(int i = operand1.length()-1 ; i >= 0 ; i--)
c.push_back(operand1[i]-'0');
for(int i = operand2.length()-1 ; i >= 0 ; i--)
d.push_back(operand2[i]-'0');

int MAX = max(c.size(),d.size());
for(int i = 0 ; i < MAX*2 + 1 ; i++)
temp.push_back(0);

for(int i = 0 ; i < c.size() ; i++)
for(int j = 0 ; j < d.size() ; j++)
temp[i+j] += c[i]*d[j];
for(int i = 0 ; i < 2*MAX + 1 ; i++)
if(temp[i] > 9){
temp[i+1] += temp[i] / 10;
temp[i] %= 10;
}
int m = 2 * MAX;
while(temp[m] == 0)
m--;

big_int another;
another.s.clear();
int len = (m-1) / WIDTH+1;

for(int i = 0 ; i < len ; i++)
another.s.push_back(0);
for(int i = 0 ; i < len ; i++){
int x = 1;
int k = 0;
int end = min(m+1,(i+1) * WIDTH);
int start = i * WIDTH;
for(int j = start ; j < end ; j++){
k += x * temp[j];
x *= 10;
}
another.s[i] = k;
}
return another;
}
big_int operator *= (const big_int& b)
{
*this = *this * b;
return *this;
}
///
big_int operator / (const big_int& b){
string operand1,operand2,result;
stringstream ss;
for(int i = s.size()-1 ; i >= 0 ; i--)
ss << s[i];
operand1 = ss.str();
ss.str("");
for(int i = b.s.size() - 1 ; i >= 0 ; i--)
ss << b.s[i];
operand2 = ss.str();

int len1, len2;
len1 = operand1.length();
len2 = operand2.length();
if(len1 < len2)
return 0;
if(*this == b)
return 1;

vector<int> c,d;
for(int i = 0 ; i < len1 ; i++){
c.push_back(operand1[i]-'0');
if(i < len2)
d.push_back(operand2[i]-'0');
else
d.push_back(0);
}

int time = len1-len2;
int len = len1;
int k, l = 0;
for(int i = 0 ; i <= time ; i++){
int ok = 1;
k = 0;
do{
if(c[l] == 0){
l++;
ok = 0;
len1--;
}
if(len == len1){
int j = 0;
while(j < len2){
if(c[i+j] > d[j]){
ok = 1;
break;
}
else if(c[i+j] < d[j]){
ok = 0;
break;
}
j++;
}
}
if(ok){
for(int j = 0 ; j < len ; j++){
c[j+i] -= d[j];
if(c[j+i] < 0){
c[j+i-1]--;
c[j+i] += 10;
}
}
k++;
}
}while(ok);
len--;
result += k+'0';
}
big_int temp;
temp = result;
return temp;
}
big_int operator /= (const big_int& b){
*this = *this / b;
return *this;
}
//%
big_int operator % (const big_int& b){
big_int c;
c = *this - (*this / b) * b;
return c;
}
big_int operator %= (const big_int& b){
*this = *this % b;
return *this;
}
//比较
bool operator < (const big_int& b) const{
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 big_int& b) const{
return b < *this;
}
bool operator <= (const big_int& b) const{
return !(b < *this);
}
bool operator >= (const big_int& b) const{
return !(*this < b);
}
bool operator != (const big_int& b) const{
return b < *this || *this < b;
}
bool operator == (const big_int& b) const{
return !(b < *this) && !(*this < b);
}
//big_int <<
friend ostream& operator << (ostream &out, const big_int& x){
out << x.s.back();
for(int i = x.s.size() - 2 ; i >= 0 ; i--){
char buf[20];
sprintf(buf, "%08d", x.s[i]);
for(int j = 0 ; j < strlen(buf) ; j++){
out << buf[j];
}
}
return out;
}
//big_int >>
friend istream& operator >> (istream &in, big_int& x){
string s;
if(!(in >> s)){
return in;
}
x = s;
return in;
}
};
//////////////演示//////////////
int main(){
ios::sync_with_stdio(false);
big_int a, b;
a = "123456789012347";
b = 987654321;
cout << "a " << a << endl;
cout << "b " << b << endl;
cout << "a+b " << a + b << endl;
cout << "a-b " << a - b << endl;
cout << "a*b " << a * b << endl;
cout << "a/b " << a / b << endl;
cout << "a%b " << (a % b) << endl;
cout << "a<b " << (a < b) << endl;
cout << "a>b " << (a > b) << endl;
cout << "a!=b " << (a != b) << endl;
cout << "a==b " << (a == b) << endl;
return 0;
}