高精算法 - 整数

记录一下高精算法模板

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
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()), 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;
}

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
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()), 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;
}

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
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] += (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) /
10; ans[i + j] = (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) % 10
+ '0';
}
r = 2 * 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 != "0" ? "-" + 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
36
37
38
39
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], remain = mul(remain, "1");
if (remain.size() > b.size() ||
(remain.size() == b.size() && remain >= b))
for (char t = '9'; t >= '0'; --t) {
temp = mul(b, string(1, t));
if (temp.size() < remain.size() ||
(temp.size() == remain.size() && temp <= remain)) {
ans += t, remain = sub(remain, temp);
break;
}
}
else
ans += '0';
}
}
if (ans != "0") {
int i = 0;
while (ans[i] == '0') i++;
ans = ans.substr(i);
}
return neg && ans != "0" ? "-" + 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
class BigInt {
public:
string value;
BigInt(string s) { value = s; };
BigInt() { value = "0"; };
friend BigInt operator-(BigInt a, BigInt b) {
return BigInt(sub(a.value, b.value));
}
friend BigInt operator+(BigInt a, BigInt b) {
return BigInt(add(a.value, b.value));
}
friend BigInt operator/(BigInt a, BigInt b) {
return BigInt(div(a.value, b.value));
}
friend BigInt operator*(BigInt a, BigInt b) {
return BigInt(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;
}
};

UPDATE

  • 优化了实现,使用 reverseinsert 以取得更高的性能
implement
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
string add(string a, string b);
string sub(string a, string b);
string mul(string a, string b);
string div(string a, string b);

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()), r = a.size() - 1;
string ans(maxlen + 1, '0');
if (a.size() > b.size())
b.insert(0, a.size() - b.size(), '0');
else
a.insert(0, b.size() - a.size(), '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
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);
reverse(ans.begin(), ans.end());
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()), r = a.size() - 1;
b.insert(0, a.size() - b.size(), '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
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);
reverse(ans.begin(), ans.end());
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 r = a.size() - 1;
string ans(maxlen * 2, '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
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] += (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) / 10;
ans[i + j] = (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) % 10 + '0';
}
r = 2 * maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg && ans != "0" ? "-" + 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], remain = mul(remain, "1");
if (remain.size() > b.size() ||
(remain.size() == b.size() && remain >= b))
for (char t = '9'; t >= '0'; --t) {
temp = mul(b, string(1, t));
if (temp.size() < remain.size() ||
(temp.size() == remain.size() && temp <= remain)) {
ans += t, remain = sub(remain, temp);
break;
}
}
else
ans += '0';
}
}
if (ans != "0") {
int i = 0;
while (ans[i] == '0') i++;
ans = ans.substr(i);
}
return neg && ans != "0" ? "-" + ans : ans;
}