矩阵乘法 --- hdu 4920 : Matrix multiplication

Matrix multiplication

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 820    Accepted Submission(s): 328

Problem Description

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

Author

Xiaoxu Guo (ftiasch)

Source

2014 Multi-University Training Contest 5



Mean:

给你两个矩阵,计算两个矩阵的积。

nalyse:

很多人认为这题用暴力过不了,时间复杂度为O(n^3),800^3=512000000,差不多要接近于10^9了,可能是hdu的评测速度给力吧,再加上这题是单点评测,每个评测点的时限都有2000ms,所以说暴力过了也实属正常。

Time complexity:O(n^3)

Source code:

#include<stdio.h>
int a[800][800],b[800][800],c[800][800],n,i,j,k;
int main(){
    while(scanf("%d",&n)!=EOF){
        for(i=0;i<n;++i)
            for(j=0;j<n;++j)
                scanf("%d",&a[i][j]),a[i][j]%=3;
        for(i=0;i<n;++i)
            for(j=0;j<n;++j)
                scanf("%d",&b[i][j]),b[i][j]%=3;
        for(i=0;i<n;++i)
            for(j=0;j<i;++j)
                k=b[i][j],b[i][j]=b[j][i],b[j][i]=k;
        for(i=0;i<n;++i)
            for(j=0;j<n;++j){
                c[i][j]=0;
                for(k=0;k<n;++k)
                    c[i][j]+=a[i][k]*b[j][k];
                c[i][j]%=3;
            }
        for(i=0;i<n;++i)
            for(j=0;j<n;++j)
                printf(j==n-1?"%d\n":"%d ",c[i][j]);
    }
    return 0;
}

  

矩阵乘法 --- hdu 4920 : Matrix multiplication,布布扣,bubuko.com

时间: 2024-11-10 07:03:33

矩阵乘法 --- hdu 4920 : Matrix multiplication的相关文章

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr

2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)

题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据是不可能会有的,而我们一开始一直在想极端数据能接受的方法......后来看了鹏哥的做法,就是把是0的地方都跳过就可以了,用矩阵保存前一个非0数的位置是多少.二师兄给我看了一个代码,人家根本没用别的优化,直接将最里层k的循环提到了最外层,然后就AC了,对此我表示无语. 1 #include <cstd

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640 

hdu 4920 Matrix multiplication(矩阵坑题)

http://acm.hdu.edu.cn/showproblem.php?pid=4920 被这道题虐了一下午,啥也不说了.继续矩阵吧. 超时就超在每步取余上,要放在最后取余,再者注意三个循环的次序. #include <stdio.h> #include <map> #include <set> #include <stack> #include <queue> #include <vector> #include <cma

hdu 4920 Matrix multiplication(矩阵相乘)多校训练第5场

Matrix multiplication                                                                           Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Given two matrices A and B of size n×n, find the

HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1775    Accepted Submission(s): 796 Problem Description Given two matrices A and B of size n×n, find the product of them.

HDU 4920 Matrix multiplication

Problem Description 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≤

HDU 4920 Matrix multiplication(bitset)

HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑,结果就过了.然后看了官方题解,上面是用了bitset这个东西,可以用来存大的二进制数,那么对于行列相乘,其实就几种情况,遇到0都是0了,1 1得1,2 1,1 2得2,2 2得1,所以只要存下行列1和2存不存在分别表示的二进制数,然后取且bitcount一下的个数,就可以计算出相应的数值了 代码: 暴力: #include <cstdi

hdu 4920 Matrix multiplication (矩阵计算)

题目链接 题意:给两个矩阵a, b, 计算矩阵a*b的结果对3取余. 分析:直接计算时间复杂度是O(n^3),会超时,但是下面第一个代码勉强可以水过,数据的原因. 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 const in