-
Notifications
You must be signed in to change notification settings - Fork 0
/
BIGNUM.cpp
189 lines (153 loc) · 4.51 KB
/
BIGNUM.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
struct Bignum {
static const int MAX_DIGIT = 1000;
static const int BASE = (int) 1e9;
int digits[MAX_DIGIT], numDigit;
Bignum(ll x = 0) {
numDigit = 0;
memset(digits, 0, sizeof digits);
if (!x) numDigit = 1;
while (x > 0) {
digits[numDigit++] = x % BASE;
x /= BASE;
}
}
Bignum(string s) {
numDigit = 0;
memset(digits, 0, sizeof digits);
ll x(0);
int i(s.length() - 1), l(i + 1);
for (int i = l - 1; i >= 8; i -= 9) {
digits[numDigit++] = stoll(s.substr(i - 8, 9));
}
if(l % 9) {
digits[numDigit++] = stoll(s.substr(0, l % 9));
}
}
Bignum& operator += (const Bignum &x) {
int carry(0);
numDigit = max(numDigit, x.numDigit);
for (int i = 0; i < numDigit; ++i) {
digits[i] += x.digits[i] + carry;
if (digits[i] >= BASE) {
digits[i] -= BASE;
carry = 1;
} else {
carry = 0;
}
}
if (carry)
digits[numDigit++] = carry;
return *this;
}
Bignum operator + (const Bignum &x) const {
Bignum res(*this);
return res += x;
}
Bignum& operator -= (const Bignum &x) {
int carry(0);
for (int i = 0; i < numDigit; ++i) {
digits[i] -= x.digits[i] + carry;
if (digits[i] < 0) {
digits[i] += BASE;
carry = 1;
} else carry = 0;
}
while (numDigit > 1 && !digits[numDigit - 1]) --numDigit;
return *this;
}
Bignum operator - (const Bignum &x) const {
Bignum res(*this);
res -= x;
return res;
}
Bignum& operator *= (int x) {
if (!x) {
*this = Bignum(0);
return *this;
}
ll remain = 0;
for (int i = 0; i < numDigit; ++i) {
remain += 1LL * digits[i] * x;
digits[i] = remain % BASE;
remain /= BASE;
}
while (remain > 0) {
digits[numDigit++] = remain % BASE;
remain /= BASE;
}
return *this;
}
Bignum operator * (int x) const {
Bignum res(*this);
res *= x;
return res;
}
Bignum operator * (const Bignum &x) const {
Bignum res(0);
for (int i = 0; i < numDigit; ++i) {
if (digits[i] > 0)
for (int j = 0; j < x.numDigit; ++j) {
if(x.digits[j] > 0) {
ll tmp = 1LL * digits[i] * x.digits[j];
int pos(i + j);
while (tmp > 0) {
tmp += res.digits[pos];
res.digits[pos] = tmp % BASE;
tmp /= BASE;
pos++;
}
}
}
}
res.numDigit = MAX_DIGIT - 1;
while (res.numDigit > 1 && !res.digits[res.numDigit - 1]) --res.numDigit;
return res;
}
ll operator % (ll x) const {
ll res(0);
for (int i = numDigit - 1; i >= 0; i--)
res = (res * BASE + digits[i]) % x;
return res;
}
Bignum operator / (ll x) const {
Bignum res(0);
ll rem(0);
for (int i = numDigit - 1; i >= 0; i--) {
res.digits[i] = (BASE * rem + digits[i]) / x;
rem = (BASE * rem + digits[i]) % x;
}
res.numDigit = numDigit;
while (res.numDigit > 1 && !res.digits[res.numDigit - 1]) --res.numDigit;
return res;
}
#define COMPARE(a, b) (((a) > (b)) - ((a) < (b)))
int compare(const Bignum &x) const {
if (numDigit != x.numDigit) {
return COMPARE(numDigit, x.numDigit);
}
for (int i = numDigit - 1; i >= 0; --i) {
if (digits[i] != x.digits[i]) {
return COMPARE(digits[i], x.digits[i]);
}
}
return 0;
}
#define DEF_OPER(o) bool operator o (const Bignum &x) const { return compare(x) o 0; }
DEF_OPER(<) DEF_OPER(>) DEF_OPER(>=) DEF_OPER(<=) DEF_OPER(==) DEF_OPER(!=)
#undef DEF_OPER
string toString(void) const {
string res;
for (int i = 0; i < numDigit; ++i) {
int tmp = digits[i];
for (int j = 0; j < 9; ++j) {
res.push_back('0' + tmp % 10);
tmp /= 10;
}
}
while (res.size() > 1 && res.back() == '0') {
res.pop_back();
}
reverse(res.begin(), res.end());
return res;
}
};