Description
Given a n x 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 C4>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- A is given.
Sample Input
2 2 40) 11 1
Sample Output
1 22 3
Code:
#include <iostream> #include <cstdio> #include <cstring>using namespace std;struct mat{int t[65][65]; void set () {memset (t,0,sizeof (t)); }} A,b;mat multiple (Mat a,mat b,int n,int p) {int i,j,k; Mat temp; Temp.set (); For (i=0, i<n; i++) for (j=0; j<n; J + +) {if (a.t[i][j]>0) for (k=0; k<n; k++) temp.t[i][k]= (temp.t[i][k]+a.t[i][j]*b.t[j][k])%p; } return temp;} Mat Quick_mod (Mat b,int n,int m,int p) {Mat t; T.set (); for (int i=0;i<n;i++) t.t[i][i]=1; while (m) {if (m&1) {t=multiple (t,b,n,p); } m>>=1; B=multiple (B,B,N,P); } return t;} void init (int n,int m,int p) {a.set (); B.set (); for (int i=0;i<n;i++) for (int j=0;j<n;j++) {scanf ("%d", &b.t[i][j]); B.T[I][J+N]=B.T[I][J]; A.T[I][J]=B.T[I][J]; } for (int i=n;i<2*n;i++) for (int j=n;j< 2*n;j++) if (i==j) b.t[i][j]=1; for (int i=n;i<2*n;i++) for (int j=0;j<n;j++) if (j+n==i) a.t[i][j]=1; B=quick_mod (B,2*N,M-1,P); Mat temp; Temp.set (); for (int i=0, i<n; i++) for (int j=0; j<n; J + +) {for (int k=0; k<2*n; k++) Temp.t[i][j]= (Temp.t[i][j]+b.t[i][k]*a.t[k][j])%p; Error point, Infinite wa} for (int i=0;i<n;i++) {for (int j=0;j<n;j++) printf ("%d", temp.t[i][j]); Puts (""); }}int Main () {int n,m,p; while (cin>>n>>m>>p) {init (n,m,p); } return 0;}