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. 线性模型(1):Perceptron Learning Algorithm (PLA)

    此笔记源于台湾大学林轩田老师<机器学习基石><机器学习技法> (一) PLA算法是基本的binary Classification算法. 一个基本的问题是,对于银行,假设我知道 ...

  2. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  3. [POJ] #1001# Exponentiation : 大数乘法

    一. 题目 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156373   Accepted: ...

  4. SRM 514 DIV1 500pt(DP)

    题目简述 给定一个H×W大小的矩阵,每个格子要么是1~9中的一个数,要么是".",要求你把“.”填成具体的数字(1~9),并且符合以下两个要求: 对于所有的整数r 和 c( 0 & ...

  5. 第二百三十八天 how can I 坚持

    最近睡觉,老是梦到死亡,多么可怕啊.感觉好虚幻. spring事务管理,框架搭建. 看着没多少事,最起来感觉好多啊. 梳理下最近爬过的山,时间久了会忘,反正上周没爬,下雪了. 10月18号-香山,11 ...

  6. 值栈与ognl

    ValueStack (值栈): 1.贯穿整个Action的生命周期(每个Action类的对象实例都拥有一个ValueStack对象).相当于一个数据的中转站.在其中保存当前Action对象和其他相关 ...

  7. JDBC学习笔记(1)——JDBC概述

    JDBC JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据.JDBC代表Java数据库连接. JDBC库中所包含的API任务通常与数据库使用: 连接到数 ...

  8. 在C++中定义常量的两种方法的比较

    常量是定以后,在程序运行中不能被改变的标识符.C++中定义常量可以用#define .const 这两种方法.例如:#define PRICE 10 //定义单价常量10const int PRICE ...

  9. Codeforces 626A Robot Sequence

    A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  10. WEB安全之威胁解析

    本文章转载自 http://www.xuebuyuan.com/60198.html 主要威胁: 暴力攻击(brute-force attack):这些攻击通过尝试所有可能的字符组合,以发现用户证书. ...