# POJ - 3233 Matrix Power Series

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<iostream>
using namespace std;

int n, k, m;
long long in[30][30];
long long list[30][30];
long long temp[30][30];
long long sum[30][30];

void pro(long long l1[][30], long long l2[][30], long long t[][30])	//t=l1*l2
{
for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
{
t[i][j] = 0;
for (int k = 0; k < n; k++)t[i][j] = (t[i][j] + l1[i][k]%m * l2[k][j]%m) % m;
}
}

void f(int num)		//list=in^num
{
if (num == 1)
{
for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
			list[i][j] = in[i][j] % m;
return;
}
f(num / 2);
pro(list, list, temp);
if (num % 2)pro(temp, in, list);
else for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)list[i][j] = temp[i][j];
}

void getsum(int num)
{
if (num == 1)
{
for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)sum[i][j] = in[i][j]%m;
return;
}
getsum(num / 2);
f(num / 2);
pro(sum, list, temp);
for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
		sum[i][j] = (sum[i][j] + temp[i][j]) % m;
if (num % 2)
{
pro(sum, in, temp);
for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
			sum[i][j] = (temp[i][j] + in[i][j]) % m;
}
}

int main()
{
cin >> n >> k >> m;
for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)cin >> in[i][j];
getsum(k);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j)cout << " ";
cout << sum[i][j];
}
cout << endl;
}
return 0;
}

S（k）=S（k-1）*A +A

S（k）=CA

#include<iostream>
using namespace std;

int n, k, m;
long long in[60][60];
long long list[60][60];
long long temp[60][60];

void pro(long long l1[][60], long long l2[][60], long long t[][60])	//t=l1*l2
{
for (int i = 0; i < n*2; i++)for (int j = 0; j < n*2; j++)
{
t[i][j] = 0;
for (int k = 0; k < n * 2; k++)t[i][j] = (t[i][j] + l1[i][k] * l2[k][j]) % m;
}
}
void f(int num)		//list=in^num
{
if (num == 0)
{
for (int i = 0; i < n*2; i++)for (int j = 0; j < n*2; j++)
			list[i][j] = (i == j);
return;
}
f(num / 2);
pro(list, list, temp);
if (num % 2)pro(temp, in, list);
else for (int i = 0; i < n*2; i++)for (int j = 0; j < n*2; j++)list[i][j] = temp[i][j];
}

int main()
{
cin >> n >> k >> m;
for (int i = 0; i < n * 2; i++)for (int j = 0; j < n * 2; j++)
		in[i][j] = (i == j || i+n == j );
for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)cin >> in[i][j];
f(k);
long long r;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j)cout << " ";
r = 0;
for (int k = 0; k < n; k++)r += list[i][k + n] * in[k][j];
cout << r%m;
}
cout << endl;
}
return 0;
}

#### 矩阵十大经典题目之三-POJ-3233-Matrix Power Series-两次二分

2014-03-11 16:48:02

#### poj 3233 Matrix Power Series

2014-07-02 13:49:22

#### poj 3233 Matrix Power Series

2013-08-28 16:25:12

#### POJ 3233 Matrix Power Series

2014-02-23 11:05:06

#### Matrix Power Series POJ - 3233

2017-08-22 10:12:04

#### POJ 3233 - Matrix Power Series

2017-03-09 20:19:12

#### poj 3233——Matrix Power Series

2014-06-23 17:00:20

#### poj 3233：Matrix Power Series

2014-03-08 15:59:56

#### poj 3233 Matrix Power Series

2013-10-16 20:19:47

#### POJ 3233 Matrix Power Series

2014-09-10 22:37:34