Problem Descriptiongiven, matrices A and B of size nxn, find the product of them.
Bobo hates big integers. So is only asked to find the result modulo 3.
Inputthe input consists of several tests. For each tests:
The first line contains N (1≤n≤800). Each of the following n lines contain n integers-the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤aij,bij≤109).
Outputfor each tests:
Print n lines. Each of them contain n integers – The matrix AXB in similar format.
Sample Input10120 12 34 56 7
Sample Output00 12 1
Matrix multiplication is just a three for loop, nothing to say. But this problem good pit, to be careful, how to pit, please look at the code.
1#include <cstdio>2#include <cstring>3 using namespacestd;4 intn,a[804][804],b[804][804],c[804][804];5 intMain ()6 {7 inti,j,k;8 while(~SCANF ("%d",&N))9 {Ten for(i=1; i<=n;i++) One for(j=1; j<=n;j++) c[i][j]=0; A for(i=1; i<=n;i++) - for(j=1; j<=n;j++) - { thescanf"%d",&a[i][j]); -a[i][j]%=3;//each input to the% 3, so that the data is smaller, the following can not be% 3. - } - for(i=1; i<=n;i++) + for(j=1; j<=n;j++) - { +scanf"%d",&b[i][j]); Ab[i][j]%=3; at } - for(k=1; k<=n;k++)//This loop cannot be swapped under the J loop, or it will time out. What reason is not clear at the moment. - for(i=1; i<=n;i++) - for(j=1; j<=n;j++) -C[I][J]+=A[I][K]*B[K][J];//This can not be% 3, or it will time out, because more than a step. - for(i=1; i<=n;i++) in { - for(j=1; j<n;j++) toprintf"%d", c[i][j]%3); +printf"%d\n", c[i][j]%3); - } the } * return 0; $}
Hdu 4920 matrix multiplication (matrix multiplication)