2014 Multi-University Training Contest 9#6
2014 Multi-University Training Contest 9#6
Fast Matrix CalculationTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 424 Accepted Submission(s): 219
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 25 54 45 40 04 2 5 51 3 1 56 31 2 30 3 02 3 44 3 22 5 50 5 03 4 5 1 1 05 3 2 3 3 23 1 5 4 5 20 0
Sample Output
1456
问题:一开始的矩阵快速幂模板好像有点问题,改了之后还不对,原来是初始化的问题,又是没有初始化为0,导致加的时候出错。关键是它第一组是对的,后面就不对了,真实奇怪!
这一题思路还是很简单的,只是比赛的时候没去认真想。。。就去做其他题了。。。
(A*B)^1000*1000矩阵相乘,可以转化为A*(B*A)^(100*100-1)*B 运算次数减少很多,然后不要忘记是(1000*1000-1)
1 #include <cstring> 2 3 #include <iostream> 4 5 #include <algorithm> 6 7 #include <cstdio> 8 9 #include <cmath> 10 11 #include <map> 12 13 #include <cstdlib> 14 15 #define M(a,b) memset(a,b,sizeof(a)) 16 17 using namespace std; 18 19 20 21 const int SMod = 6; 22 23 int N,K; 24 25 26 27 struct Matrix 28 29 { 30 31 int m[1001][6]; 32 33 }; 34 35 36 37 Matrix Mul(Matrix a,Matrix b,int mm,int kk,int nn) 38 39 { 40 41 Matrix c; 42 43 memset(c.m,0,sizeof(c.m)); 44 45 for(int i=0;i<mm;i++) 46 47 for(int j=0;j<nn;j++) 48 49 for(int k=0;k<kk;k++) 50 51 c.m[i][j] += (a.m[i][k]*b.m[k][j])% SMod, c.m[i][j]%=6; 52 53 return c; 54 55 } 56 57 58 59 Matrix MPow(Matrix a,int n,int nn) 60 61 { 62 63 Matrix res; 64 65 memset(res.m,0,sizeof(res.m)); 66 67 for(int i = 0;i<nn;i++) 68 69 res.m[i][i] = 1; 70 71 while(n) 72 73 { 74 75 //cout<<n<<'!'<<endl; 76 77 if(n&1) 78 79 res = Mul(res,a,nn,nn,nn); 80 81 n>>=1; 82 83 a = Mul(a,a,nn,nn,nn); 84 85 86 87 } 88 89 90 91 return res; 92 93 } 94 95 96 97 int A[1001][6],B[6][1001],C[1001][1001],D[1001][1001]; 98 99 100 101 int main() 102 103 { 104 105 while(scanf("%d%d",&N,&K)==2&&N+K!=0) 106 107 { 108 109 Matrix M,te; 110 111 M(M.m,0); 112 113 M(te.m,0); 114 115 M(A,0); 116 117 M(B,0); 118 119 M(C,0); 120 121 M(D,0); 122 123 for(int i = 0;i<N;i++) 124 125 for(int j = 0;j<K;j++) 126 127 { 128 129 scanf("%d",&A[i][j]); 130 131 } 132 133 for(int i = 0;i<K;i++) 134 135 for(int j = 0;j<N;j++) 136 137 { 138 139 scanf("%d",&B[i][j]); 140 141 } 142 143 for(int i=0;i<K;i++) 144 145 for(int j=0;j<K;j++) 146 147 for(int k=0;k<N;k++) 148 149 M.m[i][j] += (B[i][k]*A[k][j])%SMod, M.m[i][j]%=SMod; 150 151 152 153 int ans = 0; 154 155 te = MPow(M,N*N-1,K); 156 157 for(int i=0;i<N;i++) 158 159 for(int j=0;j<K;j++) 160 161 for(int k=0;k<K;k++) 162 163 C[i][j] += ((A[i][k]*te.m[k][j])%SMod + SMod) % SMod; 164 165 166 167 for(int i=0;i<N;i++) 168 169 for(int j=0;j<N;j++) 170 171 for(int k=0;k<K;k++) 172 173 D[i][j] += ((C[i][k]*B[k][j])%SMod + SMod) % SMod; 174 175 176 177 for(int i = 0;i<N;i++) 178 179 {for(int j = 0;j<N;j++) 180 181 { 182 183 //cout<<D[i][j]%SMod<<' '; 184 185 ans+=D[i][j]%SMod; 186 187 } 188 189 //cout<<endl; 190 191 } 192 193 printf("%d\n",ans); 194 195 } 196 197 return 0; 198 199 }