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

1,忽视0  去做。

 1 #include"stdio.h"
 2 #include"string.h"
 3 int a[801][801],b[801][801];
 4 int a1[801][801],b1[801][801];
 5 int c[801][801];
 6 int main()
 7 {
 8     int n,i,j,k;
 9     while(scanf("%d",&n)==1)
10     {
11         memset(a,0,sizeof(a));
12         memset(b,0,sizeof(b));
13         memset(c,0,sizeof(c));
14         memset(a1,0,sizeof(a1));
15         memset(b1,0,sizeof(b1));
16         for(i=1;i<=n;i++)
17             for(j=1;j<=n;j++)
18             {
19                 scanf("%d",&a[i][j]);
20                 a[i][j]%=3;
21             }
22         for(i=1;i<=n;i++)
23             for(j=1;j<=n;j++)
24             {
25                 scanf("%d",&b[i][j]);
26                 b[i][j]%=3;
27             }
28         for(i=1;i<=n;i++)
29         {
30             int pre=-1;
31             for(j=n;j>=0;j--)
32             {
33                 a1[i][j]=pre;
34                 if(a[i][j])
35                     pre=j;
36             }
37         }
38         for(i=1;i<=n;i++)
39         {
40             int pre=-1;
41             for(j=n;j>=0;j--)
42             {
43                 b1[i][j]=pre;
44                 if(b[i][j])
45                     pre=j;
46             }
47         }
48         for(i=1;i<=n;i++)
49             for(j=a1[i][0];j+1;j=a1[i][j])
50                 for(k=b1[j][0];k+1;k=b1[j][k])
51                     c[i][k]+=a[i][j]*b[j][k];
52         for(i=1;i<=n;i++)
53         {
54             for(j=1;j<n;j++)
55                 printf("%d ",c[i][j]%3);
56             printf("%d\n",c[i][j]%3);
57         }
58     }
59     return 0;
60 }

我们知道内存中二维数组是以行为单位连续存储的,逐列访问将会每次跳1000*4(bytes)。根据cpu cache的替换策略,将会有大量的cache失效。

时间居然会相差很多。 可见利用好cpu cache优化我们的程序,是非常有必要掌握的技能。
平时写程序时,也应当尽量使cpu对内存的访问,是尽可能连续的

/*
    Name: Matrix multiplication
    Copyright: Shangli Cloud
    Author: Shangli Cloud
    Date: 05/08/14 20:46
    Description: 转置
*/
/*
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int ms=801;
const int mod=3;
*/
#include"stdio.h"
#include"string.h"
//int a[ms][ms],b[ms][ms],c[ms][ms];
#define mod 3
int a[801][801],b[801][801],c[801][801];
int main()
{
    int n,x,i,j,k;
    while(scanf("%d",&n)==1)
    {
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                scanf("%d",&x);
                a[i][j]=x%mod;
            }
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                scanf("%d",&x);
                b[j][i]=x%mod;
            }
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                c[i][j]=0;
                for(k=1;k<=n;k++)
                {
                    //c[i][j]+=a[i][k]*b[j][k]%mod;多了个mod就超时,
                    c[i][j]+=a[i][k]*b[j][k];//1656ms,多个Mod就超过2s.
                }
            if(j<n)
                printf("%d ",c[i][j]%mod);
            else
                printf("%d\n",c[i][j]%mod);
            }
    }
    return 0;
}

Matrix multiplication hdu4920,布布扣,bubuko.com

时间: 2024-10-25 20:44:51

Matrix multiplication hdu4920的相关文章

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

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

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≤

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

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];

[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