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. 2016年CCF第七次测试 俄罗斯方块

    //2016年CCF第七次测试 俄罗斯方块 // 这道小模拟题还是不错 // 思路:处理出输入矩阵中含1格子的行数和列数 // 再判是否有一个格子碰到底部,否则整体再往下移动一步,如果有一个格子不能移 ...

  2. web服务器分析与设计(二)

    面向对象分析与设计第二步:寻找对象,建立问题域模型 1,用例场景描述 接上一篇中的用例,编写用例场景 U1: 上网者:打开网站(www.xxx.com) 浏览器:连接网站 目标系统:接受连接 检查连接 ...

  3. lucene学习笔记:三,Lucene的索引文件格式

    Lucene的索引里面存了些什么,如何存放的,也即Lucene的索引文件格式,是读懂Lucene源代码的一把钥匙. 当我们真正进入到Lucene源代码之中的时候,我们会发现: Lucene的索引过程, ...

  4. java识别文件或字符串的编码格式

    1, 用juniversalchardet: http://code.google.com/p/juniversalchardet/ 官方示例: import org.mozilla.universa ...

  5. delphi提示错误行号之Assert(断言)

    一.用法:Assert(表达式)1.如果为假 Assert会产生一个EAssertionFailed异常,显示为 Assertion Failed (C:/src/unit1.pas, [size=+ ...

  6. hive UDF函数

    —虽然Hive提供了很多函数,但是有些还是难以满足我们的需求.因此Hive提供了自定义函数开发 —自定义函数包括三种UDF.UADF.UDTF —UDF(User-Defined-Function) ...

  7. [iOS UI进阶 - 6.0] CALayer

    A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用   2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...

  8. [iOS UI进阶 - 3.1] 触摸事件的传递

    A.事件的产生和传递 发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发 ...

  9. Umbraco中的Member登录时的Lock out功能

    请参看文章 https://our.umbraco.org/forum/using-umbraco-and-getting-started/76389-preventing-member-lock-o ...

  10. Google中rel="canonical"的相关解释和用法

    转载原地址 http://blog.sina.com.cn/s/blog_673b01740100jxlz.html 近听到很多SEO 对在页面的规范版本用规范 URL 标签( canonical U ...