Click the open link matrix multiplication.
Time Limit: 4000/2000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 2113 accepted submission (s): 956
Problem descriptiongiven two matrices A and B of size n × N, find the product of them.
Bobo hates big integers. So you are 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. 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 A × B in similar format.
Sample Input
10120 12 34 56 7
Sample output
00 12 1
Authorxiaoxu Guo (ftiasch)
Source2014 multi-university training contest 5
Directly multiply the matrix, decisive TLE. To solve this problem, you must use the sparse matrix method.
// 1703ms7868k # include <stdio. h> # include <string. h> # define M 3int A [807] [807], B [807] [807], C [807] [807]; int N; void Init () // create a matrix {for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) {scanf ("% d", & A [I] [J]); A [I] [J] % = m ;}for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) {scanf ("% d ", & B [I] [J]); B [I] [J] % = m ;}} int main () {While (scanf ("% d ", & N )! = EOF) {Init (); memset (C, 0, sizeof (c); For (INT I = 0; I <n; I ++) for (int K = 0; k <n; k ++) if (a [I] [k]) for (Int J = 0; j <n; j ++) c [I] [J] = (C [I] [J] + A [I] [k] * B [k] [J]) % 3; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n-1; j ++) printf ("% d ", c [I] [J]); printf ("% d \ n", C [I] [n-1]) ;}} return 0 ;}