POJ 3233 Matrix Power Series(矩陣+二分)
題目大意:求由矩陣 A構成的矩陣 S = A + A^2 + A^3 + … + A^k。k的取值范圍是:10^9數據很大,應該二分。
對於一個k來說,s(k) = (1+A^(k/2)) *( A+A^2+……+A^(k/2))。如果k為奇數的話需要加上A^(k/2 + 1)。
所以二分求和,復雜度就降下來了,當然還得用到矩陣快速冪。
Matrix Power Series
Time Limit: 3000MS
Memory Limit: 131072K
Total Submissions: 15477
Accepted: 6621
Description
Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
Input
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing nnonnegative
integers below 32,768, giving A’s elements in row-major order.
Output
Output the elements of S modulo m in the same way as A is given.
Sample Input
2 2 4
0 1
1 1
Sample Output
1 2
2 3
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include