Repository navigation
Expand file tree
/
Copy pathfind_recurrence_relation.cpp
More file actions
502 lines (405 loc) · 11.8 KB
/
Copy pathfind_recurrence_relation.cpp
File metadata and controls
502 lines (405 loc) · 11.8 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") //Optimization flags
//#pragma GCC option("arch=native", "tune=native", "no-zero-upper") //Enable AVX
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4") //Enable AVX
#include <memory>
#include <bits/stdc++.h>
#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define forn(i, n) for(int i = 0; i < (int)(n); ++i)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; --i)
#define for1(i, n) for(int i = 1; i <= (int)(n); ++i)
using namespace std;
#ifndef LOCAL
#define cerr while(0) cerr
#endif
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
//typedef pair<long long, long long> pii;
typedef vector<long long> vll;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef int itn;
typedef unsigned int uint;
const ld PI = 3.1415926535897932384626433832795L;
template<class T>
bool uin(T &, const T &);
template<class T>
bool uax(T &, const T &);
template<class T>
T gcd(T, T);
template<class T>
T lcm(T, T);
template<class _T>
inline string tostr(const _T &);
template<typename T>
void input(T &);
template<typename T = long long>
T nxt();
bool checkp(long long);
template<typename T, typename N>
T pw(T a, N n, T m);
template<typename T>
T inv(T a, T p);
template<class _T>
_T sqr(const _T &x);
template<class T, class... Args>
inline auto make_vec(size_t n, Args &&... args) {
if constexpr(sizeof...(args) == 1)
return vector<T>(n, T(args...));
else
return vector(n, make_vec<T>(args...));
}
template<class... Args>
inline auto make_vec(size_t n, Args &&... args) {
if constexpr(sizeof...(args) == 1)
return vector(n, args...);
else
return vector(n, make_vec(args...));
}
int TTT;
void pre() {}
bool checkp(long long x) {
if (x == 1) return true;
for (int i = 2; i * i <= x; ++i) {
if (x % i == 0) {
return false;
}
}
return true;
}
const ll mod = 1000000007;
template<long long modulo>
struct Number {
long long value;
Number<modulo>(const long long &v = 0) : value(((v % modulo) + modulo) % modulo) { }
Number<modulo> operator+(const Number<modulo> &r) const {
return Number<modulo>{(value + r.value) % modulo};
}
Number<modulo>& operator+=(const Number<modulo> &r) {
return *this = operator+(r);
}
Number<modulo> operator-(const Number<modulo> &r) const {
return Number<modulo>{(value - r.value + modulo) % modulo};
}
Number<modulo>& operator-=(const Number<modulo> &r) {
return *this = operator-(r);
}
Number<modulo> operator*(const Number<modulo> &r) const {
return Number<modulo>{(value * r.value) % modulo};
}
Number<modulo>& operator*=(const Number<modulo> &r) {
return *this = operator*(r);
}
Number<modulo> reciprocal() const {
long long x = value;
long long res = 1;
while (x != 1) {
res = (res * (-modulo / x + modulo)) % modulo;
x = modulo % x;
}
assert(res * value % modulo == 1);
return res;
}
Number<modulo> pow(long long n) const {
Number<modulo> res = 1;
Number<modulo> a = *this;
while (n) {
if (n & 1) {
res = res * a;
}
a = a * a;
n >>= 1;
}
return res;
}
Number<modulo> operator/(const Number<modulo> &r) const {
return operator*(r.reciprocal());
}
Number<modulo>& operator/=(const Number<modulo> &r) {
return *this = operator/(r.reciprocal());
}
bool operator==(const Number<modulo> &r) const {
return value == r.value;
}
bool operator!=(const Number<modulo> &r) const {
return value != r.value;
}
friend ostream &operator<<(ostream &os, const Number<modulo> &r) {
return os << r.value;
}
};
template <long long mod>
struct LinearRecurrenceFinder {
using num = Number<mod>;
vector<num> extended(int n,
const vector< vector<num> >& coeffs, const vector<num>& terms) {
vector<num> ret(max<int>(n + 1, terms.size()));
copy(terms.begin(), terms.end(), ret.begin());
const int order = coeffs.size() - 1;
const int deg = coeffs[0].size() - 1;
assert((int) terms.size() >= order);
for (int m = terms.size(); m <= n; ++m) {
num s = 0;
for (int i = 1; i <= order; ++i) {
int k = m - i;
num t = ret[k];
for (int d = 0; d <= deg; ++d) {
s = s + coeffs[i][d] * t;
t *= k;
}
}
num denom = 0, mpow = 1;
for (int d = 0; d <= deg; ++d) {
denom = denom + mpow * coeffs[0][d];
mpow = mpow * m;
}
ret[m] = s * (-1) / denom;
}
return ret;
}
vector< vector<num> > find_recurrence_relation(
const vector<num>& terms, const int deg, bool verify=true) {
const int n = terms.size();
const int B = (n + 2) / (deg + 2); // number of blocks
const int C = B * (deg + 1); // number of columns
const int R = n - (B - 1); // number of rows
assert(B >= 2); assert(R >= C - 1);
auto error = [] (int order, int deg) {
fprintf(stderr,
"Error: Could not find a recurrence relation "
"of order <= %d and degree <= %d.\n\n",
order, deg);
assert(0);
};
vector< vector<num> > mat(R, vector<num>(C));
for (int y = 0; y < R; ++y) {
for (int b = 0; b < B; ++b) {
num v = terms[y + b];
for (int d = 0; d <= deg; ++d) {
mat[y][b * (deg + 1) + d] = v;
v *= y + b;
}
}
}
int rank = 0;
for (int x = 0; x < C; ++x) {
int pivot = -1;
for (int y = rank; y < R; ++y) if (mat[y][x] != 0) {
pivot = y; break;
}
if (pivot < 0) break;
if (pivot != rank) swap(mat[rank], mat[pivot]);
num inv = mat[rank][x].reciprocal();
for (int x2 = x; x2 < C; ++x2) mat[rank][x2] *= inv;
for (int y = rank + 1; y < R; ++y) if (mat[y][x] != 0) {
num c = mat[y][x];
for (int x2 = x; x2 < C; ++x2) {
mat[y][x2] -= c * mat[rank][x2];
}
}
++rank;
}
if (rank == C) error(B - 1, deg);
for (int y = rank - 1; y >= 0; --y) if (mat[y][rank] != 0) {
assert(mat[y][y] == 1);
num c = mat[y][rank];
for (int y2 = 0; y2 < y; ++y2) {
mat[y2][rank] -= c * mat[y2][y];
}
}
int order = rank / (deg + 1);
vector< vector<num> > ret(order + 1, vector<num>(deg + 1));
ret[0][rank % (deg + 1)] = 1;
for (int y = rank - 1; y >= 0; --y) {
int k = order - y / (deg + 1), d = y % (deg + 1);
ret[k][d] = mat[y][rank] * (-1);
}
if (verify) {
auto extended_terms = extended(n - 1, ret,
vector<num>(terms.begin(), terms.begin() + order));
for (int i = 0; i < (int) terms.size(); ++i) {
if (terms[i] != extended_terms[i]) error(B - 1, deg);
}
}
auto verbose = [&] {
int last = verify ? n - 1 : order + R - 1;
fprintf(stderr,
"[ Found a recurrence relation ]\n"
"- order %d\n"
"- degree %d\n"
"- verified up to a(%d) (number of non-trivial terms: %d)\n",
order, deg, last, (last + 1) - ((deg + 2) * (order + 1) - 2)
);
fprintf(stderr, "{\n");
for (int k = 0; k <= order; ++k) {
fprintf(stderr, " {");
for (int d = 0; d <= deg; ++d) {
if (d) fprintf(stderr, ", ");
fprintf(stderr, "%lld", ret[k][d].value <= mod / 2 ? ret[k][d].value : ret[k][d].value - mod);
}
fprintf(stderr, "}%s\n", k == order ? "" : ",");
}
fprintf(stderr, "}\n\n");
};
verbose();
return ret;
}
void show_extended_sequence(int n, const vector<num>& terms, int degree) {
auto coeffs = find_recurrence_relation(terms, degree);
auto extended_terms = extended(n, coeffs, terms);
for (int i = 0; i < (int) extended_terms.size(); ++i) {
cout << i << " " << extended_terms[i] << "\n";
}
cout << "\n";
}
};
void solve(int _) {
LinearRecurrenceFinder<mod> l;
// l.show_extended_sequence(10, {4, 4, 5, 9, 27, 123, 723, 5040}, 2);
// Factorial + 3
l.show_extended_sequence(10, {4, 4, 5, 9, 27, 123, 723, 5043, 40323}, 3);
}
int main(int argc, char **argv) {
::TTT = 1;
pre();
for (int test = 1; test <= ::TTT; ++test) {
solve(test);
}
return 0;
}
#ifdef LOCAL
struct timer {
clock_t init;
timer() {
init = clock();
}
clock_t time() const {
return clock() - init;
}
~timer() {
cerr << "Time elapsed: " << (double) (time()) / CLOCKS_PER_SEC * 1000 << " ms." << endl;
}
};
#include <sys/resource.h>
struct init_str {
timer t{};
init_str() {
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
rlimit R{};
getrlimit(RLIMIT_STACK, &R);
R.rlim_cur = R.rlim_max;
setrlimit(RLIMIT_STACK, &R);
}
} init_global_;
#endif // LOCAL
//#define AUTO_MALLOC 0
#ifdef AUTO_MALLOC
namespace MALLOC {
const size_t MAX_HEAP = 768 * 1000 * 1000;
char buf[MAX_HEAP];
size_t ptr;
}
void *operator new(size_t size) {
void *result = MALLOC::buf + MALLOC::ptr;
MALLOC::ptr += size;
return result;
}
void operator delete(void *ptr) noexcept { /* DO NOTHING */ }
#endif //AUTO_MALLOC
template<typename T>
T gcd(T x, T y) {
while (y > 0) {
x %= y;
swap(x, y);
}
return x;
}
template<class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template<class _T>
inline _T sqr(const _T &x) {
return x * x;
}
template<class _T>
inline string tostr(const _T &a) {
ostringstream os("");
os << a;
return os.str();
}
template<typename T>
inline void input(T &a) {
static int ed;
a = 0;
while (!isdigit(ed = getchar()) && ed != '-') {}
char neg = 0;
if (ed == '-') {
neg = 1;
ed = getchar();
}
while (isdigit(ed)) {
a = 10 * a + ed - '0';
ed = getchar();
}
if (neg) a = -a;
}
template<typename T>
inline T nxt() {
T res;
input(res);
return res;
}
//bool checkp(long long v) {
// if (v < 2) return false;
// for (long long i = 2; i * i <= v; ++i) {
// if (v % i == 0) {
// return false;
// }
// }
// return true;
//}
template<typename T, typename N>
T pw(T a, N n, T m) {
T res = 1;
while (n) {
if (n & 1) {
res = res * a % m;
}
a = a * a % m;
n >>= 1;
}
return res;
}
template<typename T>
T inv(T a, T p) {
T res = 1;
while (a > 1) {
res = res * (p - p / a) % p;
a = p % a;
}
return res;
}
template<class T>
bool uin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool uax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}