-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_theory.h
292 lines (238 loc) · 6.19 KB
/
number_theory.h
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
#ifndef NUMBER_THEORY_H
#define NUMBER_THEORY_H
#include <deque>
#include <iostream>
using namespace std;
/* Big Unsigned Ints */
const size_t __chunksize = sizeof(unsigned int) * 8;
const size_t __chunksizelog = __builtin_log(__chunksize);
const size_t __chunkmask = __chunksize - 1;
const unsigned long long __least_bits = (1ll << __chunksize) - 1;
class buint {
private:
deque<unsigned int> chunks;
public:
buint();
buint(unsigned long long val);
size_t nbChunks() const;
deque<unsigned int>& chunksRef();
const deque<unsigned int>& chunksCRef() const;
unsigned int getChunk(size_t pos) const;
void setChunk(size_t pos, unsigned int val);
void prepareChunks(size_t size);
void clean();
size_t size() const;
bool get(size_t pos) const;
void set(size_t pos, bool val);
unsigned long long to_ulong() const;
};
buint& operator += (buint& a, const buint& b);
buint operator + (const buint &a, const buint &b);
buint& operator |= (buint &a, const buint &b);
buint operator | (const buint &a, const buint &b);
buint& operator &= (buint &a, const buint &b);
buint operator & (const buint &a, const buint &b);
buint& operator ^= (buint &a, const buint &b);
buint operator ^ (const buint &a, const buint &b);
buint& operator <<= (buint &a, size_t dec);
buint operator << (const buint &a, size_t dec);
buint& operator >>= (buint &a, size_t dec);
buint operator >> (const buint &a, size_t dec);
buint::buint() {
}
buint::buint(unsigned long long val) {
unsigned long long chunk1 = val >> __chunksize;
unsigned int chunk0 = val & __least_bits;
setChunk(0, chunk0);
setChunk(1, chunk1);
clean();
}
inline size_t buint::nbChunks() const {
return chunks.size();
}
inline deque<unsigned int>& buint::chunksRef() {
return chunks;
}
inline const deque<unsigned int>& buint::chunksCRef() const {
return chunks;
}
inline unsigned int buint::getChunk(size_t pos) const {
if(pos >= nbChunks())
return 0;
return chunks[pos];
}
inline void buint::setChunk(size_t pos, unsigned int val) {
if(pos >= nbChunks()) {
chunks.resize(pos + 1, 0);
}
chunks[pos] = val;
clean();
}
inline void buint::prepareChunks(size_t size) {
chunks.resize(max(size, nbChunks()));
}
inline void buint::clean() {
size_t cur = nbChunks();
while(cur != 0 && chunks[cur - 1] == 0)
cur--;
if(cur != nbChunks())
chunks.resize(cur);
}
inline size_t buint::size() const {
if(chunks.empty())
return 0;
return __chunksize * nbChunks() - __builtin_clz(chunks.back());
}
inline bool buint::get(size_t pos) const {
size_t chunk = pos >> __chunksizelog;
return (getChunk(chunk) >> (pos & __chunkmask)) & 1;
}
inline void buint::set(size_t pos, bool val) {
if(get(pos) == val)
return;
int chunk = pos >> __chunksizelog;
setChunk(chunk, getChunk(chunk) ^ (1 << (pos & __chunkmask)));
clean();
}
inline unsigned long long buint::to_ulong() const {
return ((unsigned long long)getChunk(1) << __chunksize) + getChunk(0);
}
inline buint& operator += (buint &a, const buint &b) {
a.prepareChunks(max(a.nbChunks(), b.nbChunks()) + 1);
size_t r = 0;
size_t cur = 0;
deque<unsigned int>& chunksA = a.chunksRef();
const deque<unsigned int>& chunksB = b.chunksCRef();
for(;cur < b.nbChunks();cur++) {
size_t s = r + chunksA[cur] + chunksB[cur];
r = s >> __chunksize;
chunksA[cur] = s & __least_bits;
}
if(r != 0) {
chunksA[cur] = r;
}
a.clean();
return a;
}
inline buint operator + (const buint &a, const buint &b) {
buint c = a;
c += b;
return c;
}
inline buint& operator |= (buint &a, const buint &b) {
a.prepareChunks(max(a.nbChunks(), b.nbChunks()) + 1);
deque<unsigned int>& chunksA = a.chunksRef();
const deque<unsigned int>& chunksB = b.chunksCRef();
for(size_t cur = 0;cur < b.nbChunks();cur++) {
chunksA[cur] |= chunksB[cur];
}
a.clean();
return a;
}
inline buint operator | (const buint &a, const buint &b) {
buint c = a;
c |= b;
return c;
}
inline buint& operator &= (buint &a, const buint& b) {
deque<unsigned int>& chunksA = a.chunksRef();
const deque<unsigned int>& chunksB = b.chunksCRef();
chunksA.resize(chunksB.size());
for(size_t cur = 0;cur < b.nbChunks();cur++) {
chunksA[cur] &= chunksB[cur];
}
a.clean();
return a;
}
inline buint operator & (const buint &a, const buint &b) {
buint c = a;
c &= b;
return c;
}
inline buint& operator ^= (buint &a, const buint &b) {
a.prepareChunks(max(a.nbChunks(), b.nbChunks()) + 1);
deque<unsigned int>& chunksA = a.chunksRef();
const deque<unsigned int>& chunksB = b.chunksCRef();
for(size_t cur = 0;cur < b.nbChunks();cur++) {
chunksA[cur] ^= chunksB[cur];
}
a.clean();
return a;
}
inline buint operator ^ (const buint &a, const buint &b) {
buint c = a;
c ^= b;
return c;
}
inline buint& operator <<= (buint &a, size_t dec) {
deque<unsigned int>& chunksA = a.chunksRef();
chunksA.push_back(0);
size_t mod = dec % __chunksize;
if(mod != 0) {
unsigned long long r = 0;
for(size_t iChunk = 0;iChunk < a.nbChunks();iChunk++) {
unsigned long long s = (chunksA[iChunk] << mod) | r;
r = s >> __chunksize;
chunksA[iChunk] = s & __least_bits;
}
}
for(size_t n = 0; n < dec / __chunksize;n++) {
chunksA.push_front(0);
}
a.clean();
return a;
}
inline buint operator << (const buint &a, size_t dec) {
buint c = a;
c <<= dec;
return c;
}
inline buint& operator >>= (buint &a, size_t dec) {
deque<unsigned int>& chunksA = a.chunksRef();
size_t mod = dec % __chunksize;
if(mod != 0) {
unsigned long long r = 0;
size_t iChunk = chunksA.size();
while(iChunk != 0) {
iChunk--;
unsigned long long s = (r << __chunksize) >> mod;
r = chunksA[iChunk] & (__least_bits >> mod);
chunksA[iChunk] >>= mod;
chunksA[iChunk] |= s;
}
}
for(size_t n = 0; n < dec / __chunksize;n++) {
if(!chunksA.empty())
chunksA.pop_front();
}
a.clean();
return a;
}
inline buint operator >> (const buint &a, size_t dec) {
buint c = a;
c >>= dec;
return c;
}
inline size_t log2(buint& a) {
return a.size() - 1;
}
/*PGCD and PPCM*/
template<class T>
T pgcd(T a, T b) {
if(a == 0)
return b;
return pgcd<T>(b % a, a);
}
template<class T>
inline T ppcm(T a, T b) {
return a / pgcd<T>(a, b) * b;
}
/*Factorial*/
template<class T>
inline T factorial(T a) {
T prod = 1;
for(T fact = 1;fact <= a;fact++)
prod *= fact;
return prod;
}
#endif