hdu 4965 矩阵快速幂 矩阵相乘性质
2014-08-19 19:46
316 查看
Fast Matrix Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170 Accepted Submission(s): 99[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, output the sum of all the elements in M’ in a line.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
14
56
[align=left]Source[/align]
2014 Multi-University Training Contest 9
[align=left]Recommend[/align]
hujie | We have carefully selected several similar problems for you: 4970 4968 4967 4966 4964
题解:
(4 <= N <= 1000), (2 <=K <= 6)
N*K matrix A,K*N matrix B
A*B是N*N,但是B*A为k*k,于是。。。
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<algorithm> #include<cmath> #include<queue> #include<map> #define N 1005 #define M 15 #define mod 6 #define mod2 100000000 #define ll long long #define maxi(a,b) (a)>(b)? (a) : (b) #define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,k; int a [10],b[10] ,d[10][10],f [10],g ,h ; int ans; typedef struct{ int m[10][10]; } Matrix; Matrix e,P; Matrix I = {1,0,0,0,0,0,0,0,0,0, 0,1,0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0,0,0, 0,0,0,1,0,0,0,0,0,0, 0,0,0,0,1,0,0,0,0,0, 0,0,0,0,0,1,0,0,0,0, 0,0,0,0,0,0,1,0,0,0, 0,0,0,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,1,0, 0,0,0,0,0,0,0,0,0,1, }; Matrix matrixmul(Matrix aa,Matrix bb) { int i,j,kk; Matrix c; for (i = 1 ; i <= k; i++) for (j = 1; j <= k;j++) { c.m[i][j] = 0; for (kk = 1; kk <= k; kk++) c.m[i][j] += (aa.m[i][kk] * bb.m[kk][j])%mod; c.m[i][j] %= mod; } return c; } Matrix quickpow(int num) { Matrix m = P, q = I; while (num >= 1) { if (num & 1) q = matrixmul(q,m); num = num >> 1; m = matrixmul(m,m); } return q; } int main() { int i,j,o; //freopen("data.in","r",stdin); //scanf("%d",&T); //for(int cnt=1;cnt<=T;cnt++) //while(T--) while(scanf("%d%d",&n,&k)!=EOF) { if(n==0 && k==0) break; memset(d,0,sizeof(d)); memset(f,0,sizeof(f)); memset(g,0,sizeof(g)); memset(h,0,sizeof(h)); ans=0; for(i=1;i<=n;i++){ for(j=1;j<=k;j++){ scanf("%d",&a[i][j]); } } for(i=1;i<=k;i++){ for(j=1;j<=n;j++){ scanf("%d",&b[i][j]); } } for(i=1;i<=k;i++){ for(o=1;o<=k;o++){ for(j=1;j<=n;j++){ d[i][o]+=(b[i][j]*a[j][o])%6; } d[i][o]%=6; P.m[i][o]=d[i][o]; } } e=quickpow(n*n-1); for(i=1;i<=n;i++){ for(o=1;o<=k;o++){ for(j=1;j<=k;j++){ f[i][o]+=(a[i][j]*e.m[j][o])%6; } f[i][o]%=6; } } for(i=1;i<=n;i++){ for(o=1;o<=n;o++){ for(j=1;j<=k;j++){ g[i][o]+=(f[i][j]*b[j][o])%6; } g[i][o]%=6; } } /* for(i=1;i<=n;i++){ for(o=1;o<=n;o++){ for(j=1;j<=n;j++){ h[i][o]+=(g[i][j]*g[j][o])%6; } h[i][o]%=6; } } */ for(i=1;i<=n;i++){ for(o=1;o<=n;o++){ ans+=g[i][o]; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- 【矩阵快速幂+矩阵运算性质】Fast Matrix Calculation HDU - 4965
- HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)
- hdu 4965 矩阵快速幂
- hdu 4965 Fast Matrix Calculation(矩阵快速幂)
- HDU 4965 Fast Matrix Calculation(利用矩阵运算性质)
- hdu4965——矩阵快速幂优化
- HDU 4965 Fast Matrix Calculation(矩阵快速幂)
- HDU 4965 Fast Matrix Calculation 矩阵快速幂
- HDU 4965 Fast Matrix Calculation(矩阵快速幂)
- HDU 4965 Fast Matrix Calculation 矩阵快速幂
- hdu 4965 矩阵快速幂
- HDU 4965 Fast Matrix Calculation(矩阵快速幂)
- hdu 4965 Fast Matrix Calculation(矩阵快速幂)
- HDU-4965 Fast Matrix Calculation (矩阵快速幂)
- 【HDU】4965 Fast Matrix Calculation 矩阵快速幂
- hdu 4965 Fast Matrix Calculation【矩阵快速幂】
- hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场
- hdu 4965 Fast Matrix Calculation 快速矩阵幂
- Fast Matrix Calculation HDU - 4965 (矩阵快速幂)
- HDU 4965 (矩阵快速幂)