你可以浏览这个题解的中文版本。
Consider the partition.
According to the matrix multiplication distributive law, if we have already computed , we can multiply it by , thus obtaining within the complexity of .
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 chunks, and the partition computes layers, with a complexity of for computing each layer, and for total complexity.
Take and examine the function , which is minimized when is about , so is optimal. Complexity with three times as many constants. Should be the theoretical optimal solution.
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;
}