hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965
1006
Fast Matrix CalculationTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 238 Accepted Submission(s): 128 Problem Description
One day, Alice and Bob felt bored again, Bob knows Alice is a girl
who loves math and is just learning something about matrix, so he
decided to make a crazy problem for her.
Bob has a six-faced dice which has numbers 0, 1, 2, 3, 4 and 5 on each face. At first, he will choose a number N (4 <= N <= 1000), and for N times, he keeps throwing his dice for K times (2 <=K <= 6) and writes down its number on the top face to make an N*K matrix A, in which each element is not less than 0 and not greater than 5. Then he does similar thing again with a bit difference: he keeps throwing his dice for N times and each time repeat it for K times to write down a K*N matrix B, in which each element is not less than 0 and not greater than 5. With the two matrix A and B formed, Alice’s task is to perform the following 4-step calculation. Step 1: Calculate a new N*N matrix C = A*B. Step 2: Calculate M = C^(N*N). Step 3: For each element x in M, calculate x % 6. All the remainders form a new matrix M’. Step 4: Calculate the sum of all the elements in M’. Bob just made this problem for kidding but he sees Alice taking it serious, so he also wonders what the answer is. And then Bob turn to you for help because he is not good at math. Input
The input contains several test cases. Each test case starts with two
integer N and K, indicating the numbers N and K described above. Then N
lines follow, and each line has K integers between 0 and 5,
representing matrix A. Then K lines follow, and each line has N integers
between 0 and 5, representing matrix B.
The end of input is indicated by N = K = 0. Output
For each case, output the sum of all the elements in M’ in a line.
Sample Input
4 2
5 5
4 4
5 4
0 0
4 2 5 5
1 3 1 5
6 3
1 2 3
0 3 0
2 3 4
4 3 2
2 5 5
0 5 0
3 4 5 1 1 0
5 3 2 3 3 2
3 1 5 4 5 2
0 0
Sample Output
14
56
Source
Recommend
|
题意:给出n*k的矩阵A和k*n的B,求(AB)^(n*n)结果矩阵中各元素模6 之和。(n<=1000,k<=6)
题解:(A*B)^(n*n)=A * (B*A)^(n*n-1) * B,(B*A)是k*k的矩阵,k最大只有6,简直碉炸,矩阵快速幂就行了。
之前的多校训练也有一题hdu4920,是模3矩阵乘法:http://www.cnblogs.com/yuiffy/p/3893018.html
在那题中我已经研究了各种矩阵乘法的优化,例如要kij循环而不是ijk循环,对一个小数取模的话会有很多0,可以在第二重循环中if(a[i][k]==0)就跳出,而且由于取模后数字很少,可以直接用一个三维数组l[i][j][k]来事先运算好 (i+j*k)%MOD,这样我们就又不用乘法又不用取模,简直极速。
但是这题如果直接(A*B)^(n*n)的话,就算已经极速优化了还是不行,我都怕。
代码:
1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cstring> 6 #include<algorithm> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<queue> 12 using namespace std; 13 #define ll long long 14 #define usll unsigned ll 15 #define mz(array) memset(array, 0, sizeof(array)) 16 #define minf(array) memset(array, 0x3f, sizeof(array)) 17 #define REP(i,n) for(i=0;i<(n);i++) 18 #define FOR(i,x,n) for(i=(x);i<=(n);i++) 19 #define RD(x) scanf("%d",&x) 20 #define RD2(x,y) scanf("%d%d",&x,&y) 21 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) 22 #define WN(x) prllf("%d\n",x); 23 #define RE freopen("D.in","r",stdin) 24 #define WE freopen("1biao.out","w",stdout) 25 #define mp make_pair 26 #define pb push_back 27 28 int A[1111][1111]; 29 int B[1111][1111]; 30 int C[1111][1111]; 31 int D[1111][1111]; 32 int n,K; 33 34 int liu[6][6][6]; 35 36 void check(int A[1111][1111],int n){ 37 int i,j; 38 for(i=0;i<n;i++){ 39 for(j=0;j<n;j++) 40 printf("%2d",A[i][j]); 41 puts(""); 42 } 43 } 44 45 int F[1111][1111]; 46 47 void chen2(int C[1111][1111],const int A[1111][1111],const int B[1111][1111],const int n,const int m,const int K) { 48 int i,j,k; 49 for(i=0;i<n;i++) 50 for(j=0;j<m;j++) 51 F[i][j]=0; 52 //cout<<n<<','<<m<<','<<K<<endl; 53 for(k=0; k<K; k++) 54 for(i=0; i<n; i++){ 55 if(A[i][k]==0)continue; 56 for(j=0; j<m; j++) { 57 //F[i][j]+=A[i][k]*B[k][j]; 58 F[i][j]=liu[ F[i][j] ][ A[i][k] ][ B[k][j] ]; 59 //printf("F[%d][%d]+=A[%d][%d]*B[%d][%d]=%d*%d %d\n",i,j,i,k,k,j,A[i][k],B[k][j],F[i][j]); 60 } 61 } 62 for(i=0;i<n;i++) 63 for(j=0;j<m;j++) 64 C[i][j]=F[i][j]; 65 } 66 67 void powmod(int C[1111][1111],int x,int K,int D[1111][1111]) { 68 int i,j,k; 69 mz(D); 70 for(i=0;i<K;i++) 71 D[i][i]=1; 72 while(x) { 73 if(x&1)chen2(D,D,C,K,K,K); 74 // puts("D:"); 75 // check(D,K); 76 // puts("C:"); 77 // check(C,K); 78 // printf("x=%d=%xH\n",x,x); 79 x>>=1; 80 chen2(C,C,C,K,K,K); 81 } 82 } 83 84 int biu[128]; 85 86 void init(){ 87 int i,j,k; 88 for(i=0;i<6;i++) 89 for(j=0;j<6;j++) 90 for(k=0;k<6;k++) 91 liu[i][j][k]=(i+j*k)%6; 92 for(i=0;i<6;i++) 93 biu['0'+i]=i; 94 } 95 96 char ch; 97 inline void read(int &x){ 98 while(!((((ch = getchar()) >= '0') && (ch <= '5')))); 99 x=biu[ch]; 100 } 101 102 int main() { 103 int i,j; 104 init(); 105 while(scanf("%d%d",&n,&K)!=EOF) { 106 mz(A);mz(B);mz(C);mz(D); 107 if(n==0 && K==0)break; 108 for(i=0; i<n; i++) 109 for(j=0; j<K; j++) 110 read(A[i][j]); 111 for(i=0; i<K; i++) 112 for(j=0; j<n; j++) 113 read(B[i][j]); 114 chen2(C,B,A,K,n,n); 115 //chen2(C,A,B,n,n,K); 116 //check(C,n); 117 powmod(C,n*n-1,K,D); 118 //powmod(C,n*n,n,D); 119 //check(D,K); 120 chen2(D,A,D,n,K,K); 121 chen2(D,D,B,n,n,K); 122 //check(D,n); 123 int ans=0; 124 for(i=0;i<n;i++) 125 for(j=0;j<n;j++) 126 ans+=D[i][j]; 127 printf("%d\n",ans); 128 } 129 return 0; 130 }