码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4920 Matrix multiplication

时间:2015-08-11 11:31:24      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:

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

 

HDU 4920 Matrix multiplication

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4720299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!