Matrix Power Series【Matrix Multiplication】

Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 35006 Accepted: 14029


Description

Given a n × n n × n n×n matrix A and a positive integer k, find the sum S = A + A 2 + A 3 + … + A k S = A + A2 + A3 + … + Ak S=A+A 2+A 3++A k .


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


Topic translation

Given a n × nn × nn×n matrix a and a positive integer k, sumS = a + A 2 + A 3 +… + A k S=a+A2+A3+…+AkS=a+A 2+A 3++A k

The input
contains only one test case. The first line of input contains three positive integers n (n ≤ 30 ), k (k ≤ 1 0 9) and m (m <1 0 4) n (n ≤ 30), k (k ≤ 10^9) and m (m<10^4)nn30kk109 )andm(m<104 ). Then follow n lines, each line contains32768 32768For n non-negative integers below 3 2 7 6 8 , the elements of A are given in the main order of the row.

Output Output
the elements of S modulo m in the same way as A.


Problem solving ideas

Matrix multiplication

Consider 1 × 2 1 × 21×Matrix of 2 [A n − 1, S [n − 2]] [A^n-1,S[n-2]]An1,S[n2 ] , note that the 2 elements of this 1×2 matrix are all square matrices of order r! We hope to obtain a 1×2 matrix[A n, S [n − 1]] = [A n − 1 ∗ A, A n − 1 + S [n − 2]by multiplying by a certain 2×2 matrix M】 【A^n,S[n-1]】=【A^n-1*A, An-1+S[n-2]】An,S[n1]An1A,An1+S[n2 ] ] It is
easy to construct this matrix M as:
Insert picture description here
where 4 elements are all square matrices of order r, O means a matrix of all 0s of order r, and E means an identity matrix (1 on the main diagonal, and all 0 on the other).
problem solved. Our complexity is:(2 r) 3 ∗ logn (2r)3*logn( 2 r ) 3l o g n

In fact, this matrix is ​​used in that popular method. Here we use the ideas of the previous questions to easily construct this 2 r ∗ 2 r 2r*2r2 r2 r matrix. Therefore: This idea is efficient, general, unified and harmonious!


Code

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
long long nn,k,mm;
struct c{
    
    
	long long n,m;
	long long a[100][100];
}A,B,CC;
c operator *(c A,c B){
    
    
	c C;
	C.n=A.n,C.m=B.m;
	for(int i=1;i<=C.n;i++)
		for(int j=1;j<=C.m;j++)
			C.a[i][j]=0;
	for(int k=1;k<=A.m;k++)
	{
    
    
		for(int i=1;i<=C.n;i++)
			for(int j=1;j<=C.m;j++)
				C.a[i][j]=(C.a[i][j]+(A.a[i][k]*B.a[k][j])%mm)%mm;
	}	
	return C;
}
void poww(long long x){
    
    
	if(x==1)
	{
    
    
		B=A;
		return; 
	}
	poww(x>>1);
	B=B*B;
	if(x&1)
		B=B*A;
	
}
int main(){
    
    
	scanf("%lld%lld%lld",&nn,&k,&mm);
	CC.n=nn,CC.m=2*nn;
	A.n=2*nn,A.m=2*nn;
	for(int i=1;i<=nn;i++)
		for(int j=1;j<=nn;j++)
		{
    
    
			scanf("%lld",&CC.a[i][j]);
			CC.a[i][j]=CC.a[i][j]%mm;
			A.a[i][j]=CC.a[i][j];
		}	
	for(int i=1;i<=nn;i++)
		for(int j=nn+1;j<=2*nn;j++)
			if(j-nn==i)
				A.a[i][j]=1;	
	for(int i=nn+1;i<=2*nn;i++)
		for(int j=nn+1;j<=2*nn;j++)
			if(j==i)
				A.a[i][j]=1;
	poww(k);
	CC=CC*B;
	
	for(int i=1;i<=nn;i++)
	{
    
    
		for(int j=nn+1;j<=2*nn;j++)
			printf("%lld ",CC.a[i][j]);
		printf("\n");
	}		
}

Guess you like

Origin blog.csdn.net/kejin2019/article/details/111074044