Hdu 4920矩阵乘法(内存访问的讲究)
Matrix multiplication
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2143 Accepted Submission(s): 967Problem DescriptionGiven 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.
InputThe 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).
OutputFor each tests:Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
Sample Input10120 12 34 56 7Sample Output00 12 1
请看完这篇博文,看完就去AC吧。加了输入优化,效果并不明显。
/*************************************************************************
> File Name: 1010.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月05日 星期二 19时22分23秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int n;
int a[][], b[][], c[][]; int read() {
int res = ;
char c = ' ';
while (c < '' || c > '') c = getchar();
while (c >= '' && c <= '') res += c - '', c = getchar();
return res%;
} int main(void) {
while (~scanf("%d", &n)) {
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
a[i][j] = read();
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
b[i][j] = read();
memset(c, , sizeof(c));
for (int i = ; i < n; i++) {
for (int k = ; k < n; k++) {
for (int j = ; j < n; j++) {
c[i][j] += a[i][k] * b[k][j]; //注意这里的循环顺序
}
}
}
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
printf("%d%c", c[i][j]%, j == n- ? '\n' : ' ');
}
return ;
}
Hdu 4920矩阵乘法(内存访问的讲究)的更多相关文章
- *HDU 1757 矩阵乘法
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4920 矩阵乘积 优化
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- hdu 3483 矩阵乘法
这个题目上周对抗赛题目,搞了我好久 对数学这种不是很敏感 其实都不是自己想出来的,看其他的资料和博客的推导 还是有点难度的,反正我是推不出来 通过二项式定理的化简 有两个博客写得比较好 http:// ...
- HDU 4920 Matrix multiplication 题解(内存访问连续性/卡常)
题目链接 题目大意 多组输入,给你两个n×n的矩阵,要你求他们相乘%3的值 题目思路 这个题目主要是要了解内存访问连续化,要尽量每次访问连续的内存 所以第一种方法会超时,第二种则AC.一种卡常技巧 代 ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- HDU 5607 graph(DP+矩阵乘法)
[题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向 ...
- HDU 2604 Queuing (矩阵乘法)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 5863 cjj's string game (矩阵乘法优化递推)
题目大意:用k种字符构建两个长度为n的字符串(每种字符有无限多个),要求对应位置字符相同的连续子串最长长度为m,问方法数. 其中k,n,m是输入,n(1<=n<=1000000000), ...
- Hdu 2157 How many ways??(DP||矩阵乘法)
How many ways?? Time Limit:1000 MS Memory Limit: 32768 K Problem Description 春天到了, HDU校园里开满了花, 姹紫嫣红, ...
随机推荐
- Windows下运行Tomcat闪退问题
直接双击startup.bat后闪退,可能的原因是tomcat的配置没有完全正确 完整的tomcat的配置应该配置: 1.JAVA_HOME 2.CATALINA_HOME 3.Path:%CATAL ...
- vue-cli# 项目结构
引自:https://segmentfault.com/a/1190000007880723 http://blog.csdn.net/hongchh/article/details/55113751 ...
- C++面向对象高级编程(下)第一周-Geekband
勿在浮沙筑高台 革命尚未成功,同志仍需努力 <h1> Conversion Function</h1> class Fraction { public: Fraction(in ...
- 新手玩ubuntu(一)终端
有终端才能行天下事 点击为如下,就可以进行下面的开发了
- 在vue中使用pug
安装pug npm i pug pug-loader pug-cli pug-filters -D pug :安装pug pug-loader:pug的loader pug-cli:pug 编译工具 ...
- C++与Matlab混合编程之:矩阵数据结构
项目需要将matlab代码写成C++,准备用opencv.opencv中矩阵的存储与matlab有所不同,应注意以下问题: 1.matlab中矩阵是按照列优先存储的.对于n0*n1*...*nn维的矩 ...
- 【JZOJ5179】【NOI2017模拟6.29】哈哈
题意 给定一个长度为n的序列,你可以进行若干次操作: 选择一个区间,删掉,并获得Val[Len]的得分,Len为这个区间的长度: 其中这个区间满足: 1.相邻两个数差的绝对值为1 2.每个数都大于相邻 ...
- MySql存储过程批量删除多个数据库中同名表中的指定字段
1. 创建存储过程batchDeleteField:删除所有名称为"MyDB_"开头的数据库中的指定字段 -- ---------------------------- -- Pr ...
- 10 种最常见的 Javascript 错误(频率最高)
本文是小编给大家收藏的JavaScript 中频度最高的 10 种错误,我们会告诉你什么原因导致了这些错误,以及如何防止这些错误发生.写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学 ...
- ECMAScript 5 新增 Object 接口
对象 构造器 说明 Object getPrototypeOf 返回对象的原型 Object getOwnPropertyDescriptor 返回对象自有属性的属性描述符 Object getOwn ...