Solution of P10502 Matrix Power Series
你可以浏览这个题解的中文版本。
Consider the partition.
According to the matrix multiplication distributive law, if we have already computed
This enlightens us to refer to the chunking idea, where the sum sought is divided into a number of intervals of equal length, and a number of loose chunks. The whole block recursively computes the next level of the answer and multiplies by the coefficients just fine, and the loose blocks are computed violently.
Let there be
Take
Martix solve(int r) {
if(r == 1) return a;
if(r == 2) return a + a * a;
auto p = solve(r / 3);
if(r % 3 == 1) return p + pow(a, r / 3) * p + pow(a, r / 3 * 2) * p + pow(a, r);
else if(r % 3 == 2) return p + pow(a, r / 3) * p + pow(a, r / 3 * 2) * p + pow(a, r) + pow(a, r - 1);
else return p + pow(a, r / 3) * p + pow(a, r / 3 * 2) * p;
}