-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigint.cpp
More file actions
236 lines (197 loc) · 7.01 KB
/
Copy pathBigint.cpp
File metadata and controls
236 lines (197 loc) · 7.01 KB
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
#include <Arduino.h>
#include <vector>
#include <string>
class BigInt {
private:
std::vector<int> digits;
static const int BASE = 1000000000; // 10^9
void removeLeadingZeros() {
while (digits.size() > 1 && digits.back() == 0) {
digits.pop_back();
}
}
public:
BigInt() : digits({0}) {}
BigInt(unsigned long long n) {
if (n == 0) {
digits.push_back(0);
} else {
while (n > 0) {
digits.push_back(n % BASE);
n /= BASE;
}
}
}
BigInt(const String& s) {
if (s.length() == 0) {
digits.push_back(0);
return;
}
int start = 0;
while (start < s.length() && s[start] == '0') {
start++;
}
if (start == s.length()) {
digits.push_back(0);
return;
}
// ������������ ��������� �� �����
for (int i = s.length(); i > start; ) {
int end = max(start, i - 9);
String block = s.substring(end, i);
i = end;
unsigned long num = 0;
for (int j = 0; j < block.length(); j++) {
num = num * 10 + (block[j] - '0');
}
digits.push_back(num);
}
removeLeadingZeros();
}
BigInt(const std::vector<int>& d, bool removeZeros = true) : digits(d) {
if (removeZeros) {
removeLeadingZeros();
}
}
String toString() const {
if (digits.empty()) return "0";
String result;
char buffer[10];
// ������ ���� ��� ������� �����
snprintf(buffer, sizeof(buffer), "%d", digits.back());
result += buffer;
// ��������� ����� � ����������� ������
for (int i = (int)digits.size() - 2; i >= 0; i--) {
// ������������ ��������������
snprintf(buffer, sizeof(buffer), "%09d", digits[i]);
result += buffer;
}
return result;
}
bool operator<(const BigInt& rhs) const {
if (digits.size() != rhs.digits.size()) {
return digits.size() < rhs.digits.size();
}
for (int i = digits.size() - 1; i >= 0; i--) {
if (digits[i] != rhs.digits[i]) {
return digits[i] < rhs.digits[i];
}
}
return false;
}
bool operator==(const BigInt& rhs) const {
if (digits.size() != rhs.digits.size()) return false;
for (size_t i = 0; i < digits.size(); i++) {
if (digits[i] != rhs.digits[i]) return false;
}
return true;
}
bool operator<=(const BigInt& rhs) const {
return *this < rhs || *this == rhs;
}
bool operator>(const BigInt& rhs) const {
return !(*this <= rhs);
}
bool operator>=(const BigInt& rhs) const {
return !(*this < rhs);
}
BigInt operator+(const BigInt& rhs) const {
std::vector<int> res;
int carry = 0;
size_t maxSize = max(digits.size(), rhs.digits.size());
for (size_t i = 0; i < maxSize || carry; ++i) {
long sum = carry; // ������������� long ��� �����
if (i < digits.size()) sum += digits[i];
if (i < rhs.digits.size()) sum += rhs.digits[i];
carry = sum >= BASE;
if (carry) sum -= BASE;
res.push_back(sum);
}
return BigInt(res);
}
BigInt operator-(const BigInt& rhs) const {
if (*this < rhs) {
Serial.println("Error: Subtraction would result in negative");
return BigInt(0);
}
std::vector<int> res;
int borrow = 0;
for (size_t i = 0; i < digits.size(); ++i) {
long sub = borrow; // ������������� long
borrow = 0;
if (i < rhs.digits.size())
sub += rhs.digits[i];
if (digits[i] < sub) {
res.push_back(BASE + digits[i] - sub);
borrow = 1;
} else {
res.push_back(digits[i] - sub);
}
}
return BigInt(res);
}
BigInt multiplyByDigit(int digit) const {
if (digit == 0) return BigInt(0);
if (digit == 1) return *this;
std::vector<int> res;
long long carry = 0; // ������������� long long
for (size_t i = 0; i < digits.size() || carry; ++i) {
long long product = carry;
if (i < digits.size())
product += static_cast<long long>(digits[i]) * digit;
carry = product / BASE;
res.push_back(static_cast<int>(product % BASE));
}
return BigInt(res);
}
BigInt operator*(const BigInt& rhs) const {
BigInt result(0);
for (size_t i = 0; i < digits.size(); ++i) {
BigInt temp = rhs.multiplyByDigit(digits[i]);
std::vector<int> shifted(i, 0);
shifted.insert(shifted.end(), temp.digits.begin(), temp.digits.end());
result = result + BigInt(shifted, false);
}
result.removeLeadingZeros();
return result;
}
std::pair<BigInt, BigInt> divmod(const BigInt& rhs) const {
if (rhs == BigInt(0)) {
Serial.println("Error: Division by zero");
return {BigInt(0), BigInt(0)};
}
if (*this < rhs) {
return {BigInt(0), *this};
}
BigInt current;
std::vector<int> quotientDigits;
for (int i = static_cast<int>(digits.size()) - 1; i >= 0; --i) {
current.digits.insert(current.digits.begin(), digits[i]);
current.removeLeadingZeros();
int left = 0, right = BASE;
while (left < right) {
int mid = (left + right) / 2;
BigInt product = rhs.multiplyByDigit(mid);
if (product <= current) {
left = mid + 1;
} else {
right = mid;
}
}
int digit = left - 1;
quotientDigits.push_back(digit);
BigInt product = rhs.multiplyByDigit(digit);
current = current - product;
}
std::reverse(quotientDigits.begin(), quotientDigits.end());
BigInt quotient(quotientDigits);
quotient.removeLeadingZeros();
return {quotient, current};
}
BigInt operator/(const BigInt& rhs) const {
return divmod(rhs).first;
}
BigInt operator%(const BigInt& rhs) const {
return divmod(rhs).second;
}
};