HDU 4920 Matrix multiplication

Matrix multiplication

Time Limit: 2000ms
Memory Limit: 131072KB
This problem will be judged on HDU. Original ID: 4920
64-bit integer IO format: %I64d      Java class name: Main
 
Given 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.
 

Input

The 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).
 

Output

For each tests:

Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
 

Sample Input

1
0
1
2
0 1
2 3
4 5
6 7

Sample Output

0
0 1
2 1

Source

 
解题:利用cache进行加速。。
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 810;
 5 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
 6 int main() {
 7     int n;
 8     while(~scanf("%d",&n)) {
 9         memset(c,0,sizeof c);
10         for(int i = 0; i < n; ++i)
11             for(int j = 0; j < n; ++j) {
12                 scanf("%d",a[i]+j);
13                 a[i][j] %= 3;
14             }
15         for(int i = 0; i < n; ++i)
16             for(int j = 0; j < n; ++j) {
17                 scanf("%d",b[i]+j);
18                 b[i][j] %= 3;
19             }
20         for(int i = 0; i < n; ++i)
21             for(int k = 0; k < n; ++k)
22                 for(int j = 0; j < n; ++j)
23                     c[i][j] += a[i][k]*b[k][j];
24         for(int i = 0; i < n; ++i) {
25             for(int j = 0; j + 1 < n; ++j)
26                 printf("%d ",c[i][j]%3);
27             printf("%d\n",c[i][n-1]%3);
28         }
29     }
30     return 0;
31 }
View Code

 

posted @ 2015-08-11 10:33  狂徒归来  阅读(194)  评论(0编辑  收藏  举报