how to solve linear recurrences of the type... f(n) = (n-1) * f(n-1) + (2*n-1) * f(n-2) with matrix exponentiation?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
how to solve linear recurrences of the type... f(n) = (n-1) * f(n-1) + (2*n-1) * f(n-2) with matrix exponentiation?
Name |
---|
This type of equations can't be solved with matrix exponentiation.
Question deleted.
If you consider a sequence f(0), f(1), f(2), .... You may get pair <f(i), f(i + 1)> from <f(i — 1), f(i)> by multiplying on matrix:
M(n) = { {0, 2n-1}, {1, n-1} }
Every multiplication depends on n. So you don't have a fixed matrix to power it. And it may appear impossible to get exact f(n).
But if you calculate modulo MOD. You will get repetition of matrix after MOD multiplications. So you may calculate product M(0) * M(1) * ... * M(MOD-1) = P. And then calculate final matrix as P^(n / MOD) * M(0) * M(1) ... M(n % MOD). The complexity will be O(MOD + log(n)).
If you use chinese theorem, you can calculate modulo several small numbers: MOD1, MOD2, ... MODk (where MODk is the maximal one). Then total Complexity will be O(log(n) + MODk + k^2).
You will get repetition of matrix after M multiplications
It seems that it must be
MOD
instead ofM
.Why? It looks like Fermat's little theorem for matrix. Is it correct?
Fixed.
I don't see any connection with the Fermat little theorem. Repetition will be, because if you calculate modulo MOD, then you may take reminder for any member of multiplication. It is easy to prove that: M(n) % MOD = M(n + MOD) % MOD. So when we calculate by modulo repetition will appear.