高精算法 - 整数

记录一下高精算法模板

Addition

原理

没什么好说的,小学数学——竖式计算,位数不足补零,当然实现里为了方便起见会将原数倒序,以便进位

实现

Addition
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
string add(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] == '-') {
neg = true;
a = a.substr(1);
b = b.substr(1);
} else if (a[0] == '-' && b[0] != '-')
return sub(b, a.substr(1));
else if (a[0] != '-' && b[0] == '-')
return sub(a, b.substr(1));
ull maxlen = max(a.size(), b.size());
ull l = 0, r = a.size() - 1;
string ans(maxlen + 1, '0');
while (l < r) {
swap(a[l], a[r]);
l++, r--;
}
l = 0, r = b.size() - 1;
while (l < r) {
swap(b[l], b[r]);
l++, r--;
}
a += string(maxlen - a.size(), '0');
b += string(maxlen - b.size(), '0');
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') / 10 + '0';
ans[i] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') % 10 + '0';
}
r = maxlen;
while (r > 0 && ans[r] == '0') r--; // 当结果只有一位时,不能再去掉0了
ans = ans.substr(0, r + 1);
l = 0;
while (l < r) {
swap(ans[l], ans[r]);
l++, r--;
}
return neg ? "-" + ans : ans;
}

Subtraction

原理

A + B 竖式计算,不够借位,同样倒序实现。

实现

Subtraction
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
string sub(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (b[0] == '-') return add(a, b.substr(1));
if (a[0] == '-') return "-" + add(a.substr(1), b);
if (a.size() < b.size() || (a.size() == b.size() && a < b)) {
neg = true, swap(a, b);
}
ull maxlen = max(a.size(), b.size());
ull l = 0, r = a.size() - 1;
while (l < r) {
swap(a[l], a[r]);
l++, r--;
}
l = 0, r = b.size() - 1;
while (l < r) {
swap(b[l], b[r]);
l++, r--;
}
a += string(maxlen - a.size(), '0');
b += string(maxlen - b.size(), '0');
string ans = a;
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] -= ans[i] >= b[i] ? 0 : 1;
ans[i] = (ans[i] - b[i] + 10) % 10 + '0';
}
r = maxlen - 1;
while (r > 0 && ans[r] == '0') r--; // 当结果只有一位时,不能再去掉0了
ans = ans.substr(0, r + 1);
l = 0;
while (l < r) {
swap(ans[l], ans[r]);
l++, r--;
}
return neg ? "-" + ans : ans;
}

Multiplication

原理

竖式计算,位数不足补零,同样倒序实现。

实现

Multiplication
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
string mul(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true;
a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true;
b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1);
b = b.substr(1);
}
ull maxlen = max(a.size(), b.size());
ull l = 0, r = a.size() - 1;
string ans(maxlen * 2, '0');
while (l < r) {
swap(a[l], a[r]);
l++, r--;
}
l = 0, r = b.size() - 1;
while (l < r) {
swap(b[l], b[r]);
l++, r--;
}
if (a.size() < b.size()) swap(a, b);
for (ull i = 0; i < b.size(); ++i) {
for (ull j = 0; j < a.size(); ++j) {
ans[i + j + 1] += (a[j] - '0') * (b[i] - '0') / 10;
ans[i + j] = (a[j] - '0') * (b[i] - '0') % 10 + '0';
}
}
r = maxlen;
while (r > 0 && ans[r] == '0') r--; // 当结果只有一位时,不能再去掉0了
ans = ans.substr(0, r + 1);
l = 0;
while (l < r) {
swap(ans[l], ans[r]);
l++, r--;
}
return neg ? "-" + ans : ans;
}

Division

原理

竖式计算

实现

  • 仅整除,不计算小数
Division
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
string div(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true;
a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true;
b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1);
b = b.substr(1);
}
string ans;
if (a.size() < b.size())
ans = "0";
else {
string remain = a.substr(0, b.size() - 1), temp;
for (ull i = b.size() - 1; i < a.size(); ++i) {
remain += a[i];
if (remain.size() >= b.size() && remain >= b) {
for (int t = '9'; t > '0'; --t) {
if ((temp = mul(b, string(1, t))) <= remain &&
temp.size() <= remain.size()) {
ans += t;
remain = sub(remain, temp);
break;
}
}
}
}
}
return neg ? "-" + ans : ans;
}

BigInt类

  • 运算符重载不太会用,可能有问题,以下代码仅供参考
BigInt
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
class BigInt {
public:
string value;
BigInt(string s) { value = s; };
BigInt() { value = "0"; };
friend BigInt operator-(BigInt a, BigInt b) {
return BigInt(a.sub(a.value, b.value));
}
friend BigInt operator+(BigInt a, BigInt b) {
return BigInt(a.add(a.value, b.value));
}
friend BigInt operator/(BigInt a, BigInt b) {
return BigInt(a.div(a.value, b.value));
}
friend BigInt operator*(BigInt a, BigInt b) {
return BigInt(a.mul(a.value, b.value));
}
BigInt& operator+=(BigInt b) {
value = add(value, b.value);
return *this;
}
BigInt& operator-=(BigInt b) {
value = sub(value, b.value);
return *this;
}
BigInt& operator*=(BigInt b) {
value = mul(value, b.value);
return *this;
}
BigInt& operator/=(BigInt b) {
value = div(value, b.value);
return *this;
}

private:
string add(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] == '-') {
neg = true;
a = a.substr(1);
b = b.substr(1);
} else if (a[0] == '-' && b[0] != '-')
return sub(b, a.substr(1));
else if (a[0] != '-' && b[0] == '-')
return sub(a, b.substr(1));
ull maxlen = max(a.size(), b.size());
ull l = 0, r = a.size() - 1;
string ans(maxlen + 1, '0');
while (l < r) {
swap(a[l], a[r]);
l++, r--;
}
l = 0, r = b.size() - 1;
while (l < r) {
swap(b[l], b[r]);
l++, r--;
}
a += string(maxlen - a.size(), '0');
b += string(maxlen - b.size(), '0');
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') / 10 + '0';
ans[i] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') % 10 + '0';
}
r = maxlen;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
l = 0;
while (l < r) {
swap(ans[l], ans[r]);
l++, r--;
}
return neg ? "-" + ans : ans;
}
string sub(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (b[0] == '-') return add(a, b.substr(1));
if (a[0] == '-') return "-" + add(a.substr(1), b);
if (a.size() < b.size() || (a.size() == b.size() && a < b)) {
neg = true, swap(a, b);
}
ull maxlen = max(a.size(), b.size());
ull l = 0, r = a.size() - 1;
while (l < r) {
swap(a[l], a[r]);
l++, r--;
}
l = 0, r = b.size() - 1;
while (l < r) {
swap(b[l], b[r]);
l++, r--;
}
a += string(maxlen - a.size(), '0');
b += string(maxlen - b.size(), '0');
string ans = a;
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] -= ans[i] >= b[i] ? 0 : 1;
ans[i] = (ans[i] - b[i] + 10) % 10 + '0';
}
r = maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
l = 0;
while (l < r) {
swap(ans[l], ans[r]);
l++, r--;
}
return neg ? "-" + ans : ans;
}
string mul(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true;
a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true;
b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1);
b = b.substr(1);
}
ull maxlen = max(a.size(), b.size());
ull l = 0, r = a.size() - 1;
string ans(maxlen * 2, '0');
while (l < r) {
swap(a[l], a[r]);
l++, r--;
}
l = 0, r = b.size() - 1;
while (l < r) {
swap(b[l], b[r]);
l++, r--;
}
if (a.size() < b.size()) swap(a, b);
for (ull i = 0; i < b.size(); ++i) {
for (ull j = 0; j < a.size(); ++j) {
ans[i + j + 1] += (a[j] - '0') * (b[i] - '0') / 10;
ans[i + j] = (a[j] - '0') * (b[i] - '0') % 10 + '0';
}
}
r = maxlen;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
l = 0;
while (l < r) {
swap(ans[l], ans[r]);
l++, r--;
}
return neg ? "-" + ans : ans;
}
string div(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true;
a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true;
b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1);
b = b.substr(1);
}
string ans;
if (a.size() < b.size())
ans = "0";
else {
string remain = a.substr(0, b.size() - 1), temp;
for (ull i = b.size() - 1; i < a.size(); ++i) {
remain += a[i];
if (remain.size() >= b.size() && remain >= b) {
for (int t = '9'; t > '0'; --t) {
if ((temp = mul(b, string(1, t))) <= remain &&
temp.size() <= remain.size()) {
ans += t;
remain = sub(remain, temp);
break;
}
}
}
}
}
return neg ? "-" + ans : ans;
}
};