difficulty,,, indeed,,, was not difficult to
The problem is that there is an optimization of the matrix operation
The title is to give a n*k matrix A to a k*n matrix B (1<=n<=1000 && 1=<k<=6), first multiply them by the C matrix, then calculate c^ (N*n)
Equivalent
ababababababab...= (AB) ^ (n*n)
As
A (BA) ^ (n*n-1) B
K is relatively small because BA is k*k by the matrix.
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm
> #include <iostream> using namespace std;
int a[1111][1111];
int b[1111][1111];
int c[1111][1111];
int tmp[1111][1111];
int sum[1111][1111];
void times (int k,int n,int m,int aa[][1111],int bb[][1111],int cc[][1111]) {memset (tmp,0,sizeof (TMP)); for (int i=1;i<=k;i++) {for (int. j=1;j<=m;j++) {for (int k=1;k<=n;k++) {tmp[i][j]= (Tmp[i][j]+aa[i][k]*bb[k]
[J]%6]%6;
}}} for (int i=1;i<=k;i++) for (int j=1;j<=m;j++) cc[i][j]=tmp[i][j]%6;
} void Quick (int cc[][1111],int mi,int N) {for (Int. i=1;i<=n;i++) for (int j=1;j<=n;j++) if (i==j) sum[i][j]=1;
else sum[i][j]=0;
while (MI) {if (mi&1) times (n,n,n,cc,sum,sum);
Times (N,N,N,CC,CC,CC);
mi>>=1;
}} int main () {#ifndef Online_judge freopen ("G:/in.txt", "R", stdin);
Freopen ("G:/myout.txt", "w", stdout);
#endif int n,k;
while (~SCANF ("%d%d", &n,&k)) { if (n==0 && k==0) return 0;
for (int i=1;i<=n;i++) {for (int j=1;j<=k;j++) {scanf ("%d", &a[i][j]);
}} for (int i=1;i<=k;i++) {for (int j=1;j<=n;j++) {scanf ("%d", &b[i][j]);
}} times (K,n,k,b,a,c);
Quick (c,n*n-1,k);
Times (N,k,k,a,sum,sum);
Times (N,k,n,sum,b,sum);
int ans=0;
for (int. i=1;i<=n;i++) for (int j=1;j<=n;j++) ANS+=SUM[I][J];
cout<<ans<<endl;
}
}