Question Link
Matrix Multiplication
Time Limit: 4000/2000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 2143 accepted submission (s): 967
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 input10120 12 34 56 7 sample output00 12 1
After reading this blog post, go to AC. The input optimization is added, and the effect is not obvious.
Accepted code:
1 /************************************** * *********************************** 2> File Name: 1010.cpp 3> author: stomach_ache 4> mail: [email protected] 5> created time: Tuesday, August 05, 2014 6> propose: 7 *************************************** * *******************************/8 9 # include <cmath> 10 # include <string> 11 # include <cstdio> 12 # include <fstream> 13 # include <cstring> 14 # include <Iostream> 15 # include <algorithm> 16 using namespace STD; 17 18 int N; 19 int A [802] [802], B [802] [802], c [802] [802]; 20 21 int read () {22 int res = 0; 23 char c = ''; 24 while (C <'0' | C> '9') C = getchar (); 25 while (C> = '0' & C <= '9 ') res + = C-'0', c = getchar (); 26 return res % 3; 27} 28 29 int main (void) {30 While (~ Scanf ("% d", & N) {31 for (INT I = 0; I <n; I ++) 32 for (Int J = 0; j <N; j ++) 33 A [I] [J] = read (); 34 for (INT I = 0; I <n; I ++) 35 For (Int J = 0; j <n; j ++) 36 B [I] [J] = read (); 37 memset (C, 0, sizeof (c); 38 for (INT I = 0; I <n; I ++) {39 for (int K = 0; k <n; k ++) {40 for (Int J = 0; j <n; j ++) {41 c [I] [J] + = A [I] [k] * B [k] [J]; // note the cyclic order here: 42} 43} 44} 45 for (INT I = 0; I <n; I ++) 46 for (int J = 0; j <n; j ++) 47 printf ("% d % C", C [I] [J] % 3, j = n-1? '\ N': ''); 48} 49 return 0; 50}