-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bint.cpp
406 lines (330 loc) · 8.92 KB
/
Bint.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//Bint: -*- C++ -*- Created: Galindo Sep-2014, Modified: Galindo Sep-2014
#include <cstdlib> // exit
#include <limits> // numeric_limits
#include <vector>
#include "Bint.h"
using namespace std;
static bool lt(const string& n1, const string& n2)
{
if(n1.size() < n2.size()) return true;
if(n1.size() > n2.size()) return false;
if(n1.size() == 0) return false;
for(int i = n1.size()-1; i>=0; --i)
{
if(n1[i] < n2[i]) return true;
if(n1[i] > n2[i]) return false;
}
return false;
}
static string longToString(long long a)
{
string n;
if(a<=9999999999) n.reserve(10);
else n.reserve(20);
while(a) {n += a%10 + '0'; a /= 10;}
return n;
}
static const string maxlong = longToString(std::numeric_limits<long long>::max()-1);
static const int maxlongsize = maxlong.size();
static long long stringToLong(const string& n)
{
if(lt(maxlong, n))
{cerr << "Can't be expressed as long long: " << n << endl; exit(1);}
long long res=0, p=1;
for(size_t i=0; i<n.size(); i++) {res += (n[i]-'0')*p; p*=10;}
return res;
}
static string add(const string& n1, const string& n2)
{
const string& min = n1.size()<n2.size() ? n1 : n2;
const string& max = n1.size()<n2.size() ? n2 : n1;
int minsize = min.size();
int maxsize = max.size();
string res;
res.reserve(maxsize+1);
int c=0;
for(int j=0; j<minsize; ++j)
{
int s = c + max[j] + min[j] - 2*'0';
c = s/10;
res += '0' + s%10;
}
for(int j=minsize; j<maxsize; ++j)
{
int s = c + max[j] - '0';
c = s/10;
res += '0' + s%10;
}
if(c) res += '0' + c;
return res;
}
static string sub(const string& n1, const string& n2, bool ltn1n2)
{
const string& min = ltn1n2 ? n1 : n2;
const string& max = ltn1n2 ? n2 : n1;
int minsize = min.size();
int maxsize = max.size();
string res;
res.reserve(maxsize);
int s, c=0;
for(int j=0; j<minsize; ++j)
{
s = c + max[j] - min[j];
if(s<0) {c=-1; s+=10;}
else c=0;
res += '0' + s;
}
for(int j=minsize; j<maxsize; ++j)
{
s = c + max[j] - '0';
if(s<0) {c=-1; s+=10;}
else c=0;
res += '0' + s;
}
int ressize = res.size();
int i=0;
while(res[ressize-1-i]=='0') ++i;
if(i>0) res.erase(ressize-i,i);
return res;
}
static string mul(const string& n1, char n2, int p)
{
int n1size = n1.size();
string res;
res.reserve(n1size+p+1);
while(p--) res += '0';
int f = n2-'0';
int c=0;
for(int j=0; j<n1size; ++j)
{
int s = c + f*(n1[j]-'0');
c = s/10;
res += '0' + s%10;
}
if(c) res += '0'+c;
return res;
}
Bint::Bint(const string& str) : s(1), n()
{
string t(str);
if(t[0] == '-') {s = -1; t.erase(0,1);}
int i=0;
while(t[i]=='0') ++i;
if(i>0) t.erase(0,i);
if(t.size()==0) {s=0; return;}
n.assign(t.rbegin(), t.rend()); // Reverse
}
Bint::Bint(long long a): s(0), n()
{
if(a==0) return;
if(a<0) {s=-1; a*=-1;}
else s=1;
n = longToString(a);
}
Bint Bint::operator+(const Bint& b) const
{
if(s == 0) return b;
if(b.s == 0) return *this;
// long long is slower due to convertions.
Bint sum;
if(s == b.s)
{
sum.n = add(n, b.n);
sum.s = s;
}
else
{
if(n == b.n) return 0;
bool ltnb = lt(n, b.n);
sum.n = sub(n, b.n, ltnb);
sum.s = ltnb ? b.s : s;
}
return sum;
}
Bint Bint::operator*(const Bint& b) const
{
if(s==0 || b.s==0) return 0;
if(b.n=="1" || n=="1")
{
if(b==1) return *this;
if(b==-1) return -*this;
if(*this==1) return b;
if(*this==-1) return -b;
}
int maxLen = n.size()>b.n.size() ? n.size() : b.n.size();
if(maxLen < maxlongsize/2) // Use fast long long
{
return s*stringToLong(n) * b.s*stringToLong(b.n);
}
else // Use slow Bint
{
Bint res;
if(s==b.s) res.s = 1;
else res.s =-1;
res.n = mul(n, b.n[0], 0);
int bnsize = b.n.size();
for(int i=1; i<bnsize; ++i) res.n = add(res.n, mul(n, b.n[i], i));
return res;
}
}
Bint Bint::operator/(const Bint& b) const
{
if(b.s==0) {cerr << "Division by zero error\n"; exit(1);}
if(s==0 || lt(n, b.n)) return 0;
if(b.n=="1")
{
if(b.s== 1) return *this;
if(b.s==-1) return -*this;
}
if(lt(this->n, maxlong) && lt(b.n, maxlong)) // Use fast long long
{
return s*stringToLong(n)/(b.s*stringToLong(b.n));
}
else // Use slow Bint
{
Bint bb = b.abs();
vector<Bint> tab(11);
for(int i = 0; i < 11; ++i) tab[i] = Bint(i)*bb;
Bint ratio(0), resi(1, n.substr(n.size()-b.n.size(),b.n.size()));
for(int i = n.size()-b.n.size(); i >= 0; --i)
{
int count = 0;
while(tab[count] < resi) ++count;
if(tab[count] != resi) --count;
ratio = ratio * Bint(10) + Bint(count);
if(count) resi = resi - tab[count];
if(i>0) resi = resi * Bint(10) + Bint(n[i-1]-'0');
}
if(s!=b.s) ratio.s = -1;
return ratio;
}
}
Bint Bint::operator%(const Bint& b) const
{
if(b.s==0) {cerr << "Division by zero error\n"; exit(1);}
if(s==0 || lt(n, b.n)) return *this;
if(b.n=="1") return 0;
if(lt(this->n, maxlong) && lt(b.n, maxlong)) // Use fast long long
{
return s*stringToLong(n)%stringToLong(b.n);
}
else // Use slow Bint
{
Bint bb = b.abs();
vector<Bint> tab(11);
for(int i = 0; i < 11; ++i) tab[i] = Bint(i)*bb;
Bint ratio(0), resi(1, n.substr(n.size()-b.n.size(),b.n.size()));
for(int i = n.size()-b.n.size(); i >= 0; --i)
{
int count = 0;
while(tab[count] < resi) ++count;
if(tab[count] != resi) --count;
ratio = ratio * Bint(10) + Bint(count);
if(count) resi = resi - tab[count];
if(i>0) resi = resi * Bint(10) + Bint(n[i-1]-'0');
}
if(resi.s!=0) resi.s = s;
return resi;
}
}
bool Bint::operator<(const Bint& b) const
{
if(s<b.s) return true;
if(s==-1 && b.s==-1 && !lt(n, b.n) && n!=b.n) return true;
if(s==1 && b.s==1 && lt(n, b.n)) return true;
return false;
}
ostream& operator<<(ostream& os, const Bint& b)
{
if(b.s == 0) {os << "0"; return os;}
if(b.s == -1) os << "-";
string t; t.assign(b.n.rbegin(), b.n.rend());
os << t;
return os;
}
istream& operator>>(istream& is, Bint& b)
{
string t; is >> t;
b = Bint(t);
return is;
}
long long Bint::toLong() const
{
return s*stringToLong(n);
}
Bint Bint::Bpow(int p) const
{
if(p < 0) {cerr << "Negative power not implemented error: Bpow("<<*this<<","<<p<<")\n"; exit(1);}
if(p == 0)
{
if(*this==0) {cerr << "Undefined Bpow(0,0) error\n"; exit(1);}
else return 1;
}
if(*this == 1) return *this;
int sign = 1;
if(p%2==1) sign = this->s;
Bint res(*this), a(1);
while(p != 1)
{
if(p%2==0) {res*=res; p/=2;} // r^(2p) = (r^2)^p
else {--p; a*=res;} // r^(2p+1) = r * r^(2p)
}
res *= a;
res.s = sign;
return res;
}
long long Bint::sumOfDigits() const
{
int nsize = n.size();
long long sum = 0;
for(int i = 0; i<nsize; ++i) sum += n[i]-'0';
return sum;
}
Bint Bint::factorial() const
{
if(s <= 0) return 1;
Bint fact(*this), i(1);
for(; i != *this; ++i) fact *= i;
return fact;
}
bool Bint::isPrime() const // All primes are of the form 6k-1 or 6k+1
{
if(*this <= 10)
{
if(*this <= 1) return false; // 0 or 1
if(*this <= 3) return true; // 2 or 3
if(*this == 5) return true; // 5
if(*this == 7) return true; // 7
return false; // 4, 6, 8, 9 or 10
}
if(this->isEven()) return false; // *this is multiple of 2
if(this->sumOfDigits()%3 == 0) return false; // *this is multiple of 3
if(n[0] == '5') return false; // *this is multiple of 5
if(lt(this->n, maxlong)) // Use fast long long
{
long long test = this->toLong();
long long num = 7;
int w = 4;
while(num*num <= test)
{
if(test%num==0) return false; // *this is multiple of num -> not prime
num += w; // 11, 13, 17, 19, 23, 25, 29, 31, 35, ...
w = 6-w; // num skips multiples of 2 and 3.
}
}
else // Use never ending very slow Bint.
{
// cout << "Warning very slow \"isPrime\" function.\n";
Bint num(7);
int w = 4;
while(num*num <= *this)
{
if(num.n[0]!='5' && // Skip num multiple of 5 mod test loops
*this%num==0) return false; // *this is multiple of num -> not prime
num += w; // 11, 13, 17, 19, 23, 25, 29, 31, 35, ...
w = 6-w; // num skips multiples of 2 and 3.
}
}
return true;
}
//==================================================================== END