-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLarge.cpp
256 lines (236 loc) · 6.96 KB
/
Large.cpp
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
#include "Large.h"
Large::Large(const std::string& value) noexcept {
sign_ = Sign::Plus;
int64_t i;
for(i = 0; i < value.size(); ++i) {
if (value[i] == '-') {
sign_ = sign_ == Sign::Plus ? Sign::Minus : Sign::Plus;
} else if (value[i] != '+') {
break;
}
}
uint64_t max_power = 1;
while (max_power < base_) {
max_power *= 10;
}
uint64_t digit = 0, power = 1;
for(int64_t j = static_cast<int64_t>(value.size()) - 1; j >= i; --j) {
digit += power * (value[j] - '0');
power *= 10;
if (power == max_power) {
digits_.push_back(digit);
power = 1, digit = 0;
}
}
if (digit != 0 || digits_.empty()) {
digits_.push_back(digit);
}
}
bool Large::operator<(const Large& other) const noexcept {
if (*this == other) {
return false;
}
if (other.IsZero()) {
return sign_ == Sign::Minus;
}
if (sign_ != other.sign_) {
return sign_ == Sign::Minus;
}
if (sign_ == Sign::Minus) {
return !(abs(*this) < abs(other));
}
if (digits_.size() != other.digits_.size()) {
return digits_.size() < other.digits_.size();
}
for(int64_t i = static_cast<int64_t>(digits_.size()) - 1; i >= 0; --i) {
if (digits_[i] != other.digits_[i]) {
return digits_[i] < other.digits_[i];
}
}
return false;
}
Large Large::operator+(const Large& other) const noexcept {
if (sign_ != other.sign_) {
return sign_ == Sign::Plus ? *this - (-other) : other - (-*this);
}
if (IsZero()) {
return other;
}
if (other.IsZero()) {
return *this;
}
uint64_t carry = 0;
Large result;
result.sign_ = sign_;
result.digits_.clear();
for(size_t i = 0; i < std::max(digits_.size(), other.digits_.size()); ++i) {
result.digits_.push_back((carry + DigitAt(i) + other.DigitAt(i)) % base_);
carry = (carry + DigitAt(i) + other.DigitAt(i)) / base_;
}
if (carry != 0) {
result.digits_.push_back(carry);
}
return result;
}
Large Large::operator-(const Large& other) const noexcept {
if (sign_ != other.sign_) {
return sign_ == Sign::Minus ? -(-*this + other) : *this + -other;
}
if (digits_ == other.digits_) {
return 0;
}
if (abs(*this) < abs(other)) {
return -(other - *this);
}
Large result;
result.sign_ = sign_;
result.digits_.clear();
bool next_minus = false;
for(size_t i = 0; i < digits_.size(); ++i) {
if (DigitAt(i) < other.DigitAt(i) + (next_minus ? 1 : 0)) {
result.digits_.push_back(DigitAt(i) + base_ - other.DigitAt(i) - (next_minus ? 1 : 0));
next_minus = true;
} else {
result.digits_.push_back(DigitAt(i) - other.DigitAt(i) - (next_minus ? 1 : 0));
next_minus = false;
}
}
while (result.digits_.back() == 0) {
result.digits_.pop_back();
}
return result;
}
Large Large::SimpleMult(const Large& lhs, const Large& rhs) const noexcept {
Large result;
result.digits_.clear();
uint64_t carry = 0;
for(const uint64_t digit : lhs.digits_) {
result.digits_.push_back((digit * rhs.digits_[0] + carry) % base_);
carry = (digit * rhs.digits_[0] + carry) / base_;
}
if (carry != 0) {
result.digits_.push_back(carry);
}
return result;
}
Large Large::MultByBase(int64_t power) const noexcept {
Large other = *this;
std::reverse(other.digits_.begin(), other.digits_.end());
for(int i = 0; i < std::abs(power); ++i) {
if (power > 0) {
other.digits_.push_back(0);
} else if (!other.digits_.empty()) {
other.digits_.pop_back();
}
}
std::reverse(other.digits_.begin(), other.digits_.end());
while (!other.digits_.empty() && other.digits_.back() == 0) {
other.digits_.pop_back();
}
if (other.digits_.empty()) {
other.digits_.push_back(0);
}
return other;
}
Large Large::operator*(const Large& other) const noexcept {
if (IsZero() || other.IsZero()) {
return 0;
}
if (sign_ != other.sign_) {
return -(abs(*this) * abs(other));
}
if (sign_ == Sign::Minus) {
return -*this * -other;
}
if (digits_.size() == 1) {
return SimpleMult(other, *this);
}
if (other.digits_.size() == 1) {
return SimpleMult(*this, other);
}
int64_t n = static_cast<int64_t>(std::max(digits_.size(), other.digits_.size()) / 2);
Large a = this->MultByBase(-n);
Large b = *this - a.MultByBase(n);
Large c = other.MultByBase(-n);
Large d = other - c.MultByBase(n);
return (a * c).MultByBase(2 * n) + ((a + b) * (c + d) - a * c - b * d).MultByBase(n) + b * d;
}
Large Large::operator/(const Large& other) const {
if (other.IsZero()) {
throw std::logic_error("Division by zero");
}
if (sign_ != other.sign_) {
return -(abs(*this) / abs(other));
}
if (sign_ == Sign::Minus) {
return -*this / -other;
}
if (*this < other) {
return 0;
}
Large result;
result.digits_ = std::vector<uint64_t>(digits_.size(), 0);
for(int64_t i = static_cast<int64_t>(digits_.size()) - 1; i >= 0; --i) {
uint64_t L = 0, R = base_, M;
while (L < R - 1) {
M = L + (R - L) / 2;
result.digits_[i] = M;
if (result * other > *this) {
R = M;
} else {
L = M;
}
}
result.digits_[i] = L;
}
while (!result.digits_.empty() && result.digits_.back() == 0) {
result.digits_.pop_back();
}
return result;
}
Large Large::operator%(const Large& other) const {
return *this - *this / other * other;
}
Large abs(const Large& large) noexcept {
Large other = large;
other.sign_ = Large::Sign::Plus;
return other;
}
std::string to_string(const Large& num) noexcept {
std::string result;
if (num.sign_ == Large::Sign::Minus) {
result.push_back('-');
}
for(int64_t i = static_cast<int64_t>(num.digits_.size()) - 1; i >= 0; --i) {
if (i + 1 != num.digits_.size()) {
result += std::string(9 - std::to_string(num.digits_[i]).size(), '0');
}
result += std::to_string(num.digits_[i]);
}
return result;
}
Large pow(const Large& num, const Large& n) {
if (n < 0) {
throw std::logic_error("Power must be positive");
}
if (num.IsZero() || num.digits_.size() == 1 && num.digits_[0] == 1) {
return num;
}
if (n == 0) {
return 1;
}
if (n.digits_[0] % 2 == 1) {
return num * pow(num, n - 1);
}
Large tmp = pow(num, n / 2);
return tmp * tmp;
}
std::istream& operator>>(std::istream& is, Large& num) noexcept {
std::string s;
is >> s;
num = Large(s);
return is;
}
std::ostream& operator<<(std::ostream& os, const Large& num) noexcept {
return os << to_string(num);
}