Matrix Power Series
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 24975 | Accepted: 10346 |
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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#define max_ 200010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
struct mat
{
ll num[70][70];
int n;
}a;
int m,n,k;
mat mul(struct mat a,struct mat b)
{
struct mat ans;
ans.n=a.n;
memset(ans.num,0,sizeof(ans.num));
for(int i=1;i<=a.n;i++)
{
for(int j=1;j<=a.n;j++)
{
for(int k=1;k<=a.n;k++)
{
ans.num[i][j]+=(a.num[i][k]*b.num[k][j])%m;
ans.num[i][j]%=m;
}
}
}
return ans;
}
void show(struct mat a)
{
printf("%d\n",a.n);
for(int i=1;i<=a.n;i++)
{
for(int j=1;j<=a.n;j++)
{
printf("%d ",a.num[i][j]);
}
printf("\n" );
}
}
mat fpow(struct mat a,int k)
{
struct mat ans,tmp=a;
ans.n=a.n;
for(int i=1;i<=a.n;i++)
{
for(int j=1;j<=a.n;j++)
{
if(i==j)
ans.num[i][j]=1;
else
ans.num[i][j]=0;
}
}
while(k!=0)
{
if(k&1)
ans=mul(ans,tmp);
tmp=mul(tmp,tmp);
k/=2;
}
return ans;
}
int main(int argc, char const *argv[]) {
cin>>n>>k>>m;
a.n=n<<1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a.num[i][j];
if(i==j)
{
a.num[i+n][j+n]=1;
a.num[i][j+n]=1;
}
}
}
mat ans=fpow(a,k+1);
for(int i=1;i<=n;i++)
{
for(int j=1+n;j<=n*2;j++)
{
if(i+n==j)
ans.num[i][j]--;
if(ans.num[i][j]<0)
ans.num[i][j]+=m;
printf("%d",ans.num[i][j]);
if(j!=n<<1)
printf(" ");
}
printf("\n");
}
return 0;
}