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  去做。
 #include"stdio.h"
#include"string.h"
int a[][],b[][];
int a1[][],b1[][];
int c[][];
int main()
{
int n,i,j,k;
while(scanf("%d",&n)==)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(c,,sizeof(c));
memset(a1,,sizeof(a1));
memset(b1,,sizeof(b1));
for(i=;i<=n;i++)
for(j=;j<=n;j++)
{
scanf("%d",&a[i][j]);
a[i][j]%=;
}
for(i=;i<=n;i++)
for(j=;j<=n;j++)
{
scanf("%d",&b[i][j]);
b[i][j]%=;
}
for(i=;i<=n;i++)
{
int pre=-;
for(j=n;j>=;j--)
{
a1[i][j]=pre;
if(a[i][j])
pre=j;
}
}
for(i=;i<=n;i++)
{
int pre=-;
for(j=n;j>=;j--)
{
b1[i][j]=pre;
if(b[i][j])
pre=j;
}
}
for(i=;i<=n;i++)
for(j=a1[i][];j+;j=a1[i][j])
for(k=b1[j][];k+;k=b1[j][k])
c[i][k]+=a[i][j]*b[j][k];
for(i=;i<=n;i++)
{
for(j=;j<n;j++)
printf("%d ",c[i][j]%);
printf("%d\n",c[i][j]%);
}
}
return ;
}

我们知道内存中二维数组是以行为单位连续存储的,逐列访问将会每次跳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[][],b[][],c[][];
int main()
{
int n,x,i,j,k;
while(scanf("%d",&n)==)
{
for(i=;i<=n;i++)
for(j=;j<=n;j++)
{
scanf("%d",&x);
a[i][j]=x%mod;
}
for(i=;i<=n;i++)
for(j=;j<=n;j++)
{
scanf("%d",&x);
b[j][i]=x%mod;
}
for(i=;i<=n;i++)
for(j=;j<=n;j++)
{
c[i][j]=;
for(k=;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 ;
}

Matrix multiplication hdu4920的更多相关文章

  1. hdu4920 Matrix multiplication 模3矩阵乘法

    hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  2. HDU-4920 Matrix multiplication

    矩阵相乘,采用一行的去访问,比采用一列访问时间更短,根据数组是一行去储存的.神奇小代码. Matrix multiplication Time Limit: 4000/2000 MS (Java/Ot ...

  3. 【数学】Matrix Multiplication

                                 Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  4. hdu 4920 Matrix multiplication bitset优化常数

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

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

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  6. acdeream Matrix Multiplication

    D - Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/O ...

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

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  8. 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17783   Accepted: ...

  9. hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

    Matrix multiplication                                                                           Time ...

随机推荐

  1. Lucene学习笔记: 四,Lucene索引过程分析

    对于Lucene的索引过程,除了将词(Term)写入倒排表并最终写入Lucene的索引文件外,还包括分词(Analyzer)和合并段(merge segments)的过程,本次不包括这两部分,将在以后 ...

  2. 开源项目SuperSocket的学习笔记

    近几日想在一个项目中引进一个Socket Server,用来接收客户端发送的命令消息并根据具体的业务逻辑对消息进行处理,然后转发给其它在线的客户端.因为以前在博客园关注过江大渔开源的SuperSock ...

  3. Chapter 2 Build Caffe

    Caffe for windows 的build药按照一定的顺序进行. ============================================================ 先以b ...

  4. 有关OOM KILLER的一些理解

    Linux下有一种OOM KILLER 的机制,它会在系统内存耗尽的情况下,启用自己算法有选择性的kill 掉一些进程. 一.为什么会有OOM killer 当我们使用应用时,需要申请内存,即进行ma ...

  5. 附加题-stack的理解

    这次的附加题推荐的博客是http://www.ruanyifeng.com/blog/2013/11/stack.html阮一峰的,感觉讲的深入浅出,比较适合对计算机刚刚接触的人: 下面谈谈感想: 这 ...

  6. ocp 1Z0-051 1-22题解析

    1. View the Exhibit andexamine the structure of the SALES, CUSTOMERS, PRODUCTS, and TIMES tables. Th ...

  7. Java IO (2) - OutputStream

    Java IO (2) - OutputStream 前言 JavaIO一共包括两种,一种是stream,一种是reader/writer,每种又包括in/out,所以一共是四种包.Java 流在处理 ...

  8. CAAnimation解读

    序言 CAAnimation是一个抽象类,遵循了CAMediaTiming协议和CAAction协议!我们不要直接使用CAAnimation类,而是使用其子类: CATransition:提供渐变效果 ...

  9. C#下内存管理--垃圾收集

    章节安排 内存管理简介 垃圾回收机制 性能问题 C#下非托管资源的处理 要强调的几点 References 内存管理简介 对于任何一种编程语言,内存管理都是不得不提很重要的一块内容,但可惜的是目前为止 ...

  10. Win7关机出现关闭程序提示框

    运行输入Gpedit.msc回车打开组策略,在左侧选计算机配置/管理模板/系统/关机选项,在右侧双击“关闭会阻止或取消关机的应用程序的自动终止功能”,在打开的提示框中选“已启用”,按确定即可.