HDU4920: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≤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

对于二维数组,按行取比按列取要快

真心被逗了

不过也算涨了姿势。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int a[805][805],b[805][805],c[805][805];

int main()
{
    int n,i,j,k;
    while(~scanf("%d",&n))
    {
        for(i = 0; i<n; i++)
            for(j = 0; j<n; j++)
            {
                scanf("%d",&a[i][j]);
                a[i][j]%=3;
                c[i][j] = 0;
            }
        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<n; j++)
            {
                if(!a[i][j])
                continue;
                for(k = 0; k<n; k++)
                    c[i][k] = c[i][k]+a[i][j]*b[j][k];
            }
        for(i = 0; i<n; i++)
        {
            for(j = 0; j<n; j++)
                if(j==n-1)
                    printf("%d\n",c[i][j]%3);
                else
                    printf("%d ",c[i][j]%3);
        }
    }

    return 0;
}

HDU4920:Matrix multiplication

时间: 2024-10-19 18:23:33

HDU4920:Matrix multiplication的相关文章

矩阵乘法 --- 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. b

POJ 3318:Matrix Multiplication(随机算法)

http://poj.org/problem?id=3318 题意:问A和B两个矩阵相乘能否等于C. 思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的. 这里的随机算法指的是随机枚举矩阵C的一个位置,然后通过A*B计算是否能够得到矩阵C相应位置的数,如果不等,就直接退出了,如果跑过一定的数量后能够相等,那么就可以判断这个矩阵C等于A*B的.第一次见这样的题目...有点新奇. 暴力算法: 1 #include <cstdio> 2 using namespace std;

hdu4920 Matrix multiplication 模3矩阵乘法

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

HDU4920 Matrix multiplication 矩阵

不要问我 为什么过了 窝也不造为什么就过了 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include<iostream> #inclu

Matrix multiplication hdu4920

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≤

Matrix multiplication|File I/Os|Random accesses to a file程序代写C++辅导(服务编号:cplus00045)

· c++ basics: file I/Os mechanisms to create user-defined data types the problem-solving process Matrix multiplication: Write a C++ program to compute the product of two matrices. You are required to use the template class vector to represent a matri

ACDream 1213 Matrix Multiplication (01矩阵处理)

Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of

Matrix multiplication

题目链接 题意: 给两个n*n的矩阵,求乘积后对3取摸的结果(1≤n≤800) 分析: 考虑一下为什么给3呢,对3取摸只可能得到0.1.2,都可以看作两位的,那么在乘法的时候我们可以用分配率将原来的矩阵乘法分成四个矩阵乘法,每个矩阵都只包括0和1.对于0/1矩阵的乘法,可以使用bitset来快速运算 const int MAXN = 801; bitset<MAXN> r1[MAXN], r2[MAXN], c1[MAXN], c2[MAXN]; int a[MAXN][MAXN]; int

[SGU 196] Matrix Multiplication

196. Matrix Multiplication time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard output: standard Description Let us consider an undirected graph G = <V, E> which has N vertices and M edges. Incidence matrix of this graph is