题意:
求解矩阵等比数列 A1+A2+...+An
思路:
解法1)
构造矩阵B,直接算B的快速幂。
解法2)
用二分等比数列的方法
PS:注意减少%运算
LL n, k, m;
struct mat{
int sz;
LL m[30][30];
};
mat A;
inline mat operator * (const mat & a, const mat & b) {
mat c;
c.sz = a.sz;
rep(i, 0, a.sz-1)
rep(j, 0, a.sz-1) {
c.m[i][j] = 0;
rep(k, 0, a.sz-1)
c.m[i][j] = (c.m[i][j] + a.m[i][k]*b.m[k][j])%m;
}
return c;
}
inline mat operator + (const mat & a, const mat & b) {
mat c;
c.sz = a.sz;
rep(i, 0, a.sz-1)
rep(j, 0, a.sz-1) {
c.m[i][j] = (a.m[i][j]+b.m[i][j])%m;
}
return c;
}
mat fast_pow(mat & src, int n) {
mat ans, x = src;
ans.sz = src.sz;
rep(i, 0, ans.sz-1) rep(j, 0, ans.sz-1) ans.m[i][j] = i==j;
while (n) {
if (n&1) ans = ans*x;
x = x*x;
n >>= 1;
}
return ans;
}
mat work(int cur) {
if (cur == 1) return A;
mat B = work(cur/2);
if (cur&1) return B+B*fast_pow(A, cur/2)+fast_pow(A, cur);
else return B+B*fast_pow(A, cur/2);
}