Http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 20851 # overview
A set of matrix and Gaussian elimination questions are introduced.
POJ3233 gives A matrix A and then calculates (A + A ^ 2 + A ^ 3 + ...... + A ^ k) % m
The first thought of this is the sum of the proportional series, but the Matrix cannot be divided, so another solution should be considered.
Split the matrix into two parts: (A + A ^ 2 + A ^ 3 + ...... + A ^ (k/2) + A ^ (k/2) * (A + A ^ 2 + A ^ 3 + ...... + A ^ (k/2), so that you can pair (A + A ^ 2 + A ^ 3 + ...... + A ^ (k/2) for Recursive calculation. If k is an odd number, then + A ^ k.
This method can also be used for the first N terms of the proportional series and the modulo problem. If we want to use the first N terms and formulas of the proportional series, it will involve difficult problems such as inverse element or Lucas theorem, it is easier to use the binary method.
Matrix expmod (matrix a, int n) // fast power {e. init (); while (n) {if (n & 1) e = e * a; a = a * a; n >>= 1;} return e ;} matrix solve (int k) // binary {if (k = 1) return a; c = solve (k> 1 ); matrix ans = c + c * expmod (a, (k> 1); if (k & 1) ans = ans + expmod (a, k); return ans ;}