Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 18904 | Accepted: 7991 |
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 n nonnegative 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
题目大意:
给出A的n*n的放成,计算出累加和,然后求余即可。
遇到的问题和思路:
首先看到这道题和单纯的矩阵的幂的3070感觉不同,如果要一个一个都计算了,那么要O(k * n * n * n),对于如此的数据肯定是超时。然后看了一下程序挑战的p204页。。。就是重新创建一个矩阵,是由A矩阵,0矩阵和两个单位矩阵构成的,然后每一次把和都放在左下角的矩阵中就可以了。具体的看书吧^0^。