poj3233(快速求幂+二分)

Matrix Power Series
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 12322   Accepted: 5255

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
快速求幂加二分,快速矩阵求幂可以套用模板;
对于二分:
当k为奇数时:S[7]=A^1+A^2+A^3……+A^7=A^4+(1+A^4)(A^1+A^2+A^3)=A^4+S[3]+A^4 *S[3];
当k为偶数时:S[6]=S[3]+S[3] *A^3;
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn=50;
typedef struct
{
    int m[maxn][maxn];
}Matrax;
Matrax a,per;
int n,m;
void inital()
{
    int i,j;
    for(i=0;i


你可能感兴趣的:(数学知识-数论)