跳到正文

题解

Solution of P10502 Matrix Power Series

tder约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年9月3日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 3 分钟
题目
P10502

版本

  1. v1当前

你可以浏览这个题解的中文版本。

Consider the partition.

According to the matrix multiplication distributive law, if we have already computed A+A2+⋯+ApA+A^2+\cdots+A^p, we can multiply it by AqA^{q}, thus obtaining Aq+1+Aq+2+⋯+Aq+pA^{q+1}+A^{q+2}+\cdots+A^{q+p} within the complexity of O(n3log⁡k)\mathcal{O}(n^3\log k).

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 xx chunks, and the partition computes O(log⁡xk)\mathcal{O}(\log_x k) layers, with a complexity of O(x⋅n3log⁡k)\mathcal{O}(x\cdot n^3\log k) for computing each layer, and O(xlog⁡xk⋅n3log⁡k)\mathcal{O}(x\log_x k\cdot n^3\log k) for total complexity.

Take k=1018k=10^{18} and examine the function f(x)=xlog⁡xkf(x)=x\log_x k, which is minimized when xx is about 2.72.7, so x=3x=3 is optimal. Complexity O(log⁡3k⋅n3log⁡k)\mathcal{O}(\log_3 k\cdot n^3\log k) with three times as many constants. Should be the theoretical optimal solution.


C++
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;
}
原文