各种TEL,233啊。没想到是处理掉0的情况就能够过啊。一直以为会有极端数据。没想到居然是这种啊、、在网上看到了一个AC的奇妙的代码,经典的矩阵乘法,仅仅只是把最内层的枚举,移到外面就过了啊、、、有点不理解啊,复杂度不是一样的吗、、

Matrix multiplication

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 640    Accepted Submission(s): 250

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
Author
Xiaoxu Guo (ftiasch)
Source
2014 Multi-University Training Contest 5
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?

0:x) using namespace std; const int maxn = 810; int a[maxn][maxn]; int b[maxn][maxn]; int c[maxn][maxn]; int aa[maxn][maxn]; int bb[maxn][maxn]; int main() { int n; while(cin >>n) { memset(c, 0, sizeof(c)); memset(aa, 0, sizeof(aa)); memset(bb, 0, sizeof(bb)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d",&a[i][j]); a[i][j] %= 3; } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d",&b[i][j]); b[i][j] %= 3; } } for(int i = 1; i <= n; i++) { int x = -1; for(int j = n; j >= 0; j--) { aa[i][j] = x; if(a[i][j]) x = j; } } for(int i = 1; i <= n; i++) { int x = -1; for(int j = n; j >= 0; j--) { bb[i][j] = x; if(b[i][j]) x = j; } } for (int i = 1; i <= n; i++) { for(int j = aa[i][0]; j != -1; j = aa[i][j]) { for(int k = bb[j][0]; k != -1; k = bb[j][k]) c[i][k] += a[i][j]*b[j][k]; } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n-1; j++) printf("%d ",c[i][j]%3); printf("%d\n",c[i][n]%3); } } return 0; }

这是看到有人交的AC的代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 805;
int a[N][N], b[N][N], ans[N][N];
int main()
{int n, i, j, k;while(~scanf("%d",&n)){for(i = 1; i <= n; i++)for(j = 1; j <= n; j++){scanf("%d",&a[i][j]);a[i][j] %= 3;}for(i = 1; i <= n; i++)for(j = 1; j <= n; j++){scanf("%d",&b[i][j]);b[i][j] %= 3;}memset(ans, 0, sizeof(ans));for(k = 1; k <= n; k++) //经典算法中这层循环在最内层。放最内层会超时,可是放在最外层或者中间都不会超时,不知道为什么for(i = 1; i <= n; i++)for(j = 1; j <= n; j++){ans[i][j] += a[i][k] * b[k][j];//ans[i][j] %= 3;   //假设在这里对3取余,就超时了}for(i = 1; i <= n; i++){for(j = 1; j < n; j++)printf("%d ", ans[i][j] % 3);printf("%d\n", ans[i][n] % 3);}}return 0;
}

转载于:https://www.cnblogs.com/yxwkf/p/5418535.html

HDU 4920 Matrix multiplication(矩阵相乘)相关推荐

  1. HDU4920 Matrix multiplication 矩阵

    不要问窝 为什么过了> < 窝也不造为什么就过了 说是%3变成稀疏矩阵 可是随便YY个案例都会超时.. . 看来数据是随机的诶 #include <stdio.h> #incl ...

  2. 向量、矩阵乘法的几何意义(二) 矩阵乘法(Matrix Multiplication)

    一.             旋转( rotation ) 1.   矩阵与向量相乘 由向量内积(两个向量相乘)出发,考虑矩阵与向量相乘的情况.以二维平面空间为例,设X=(x1, x2, -, xn) ...

  3. C++matrix chain multiplication矩阵链乘法算法的实现(附完整源码)

    C++lmatrix chain multiplication矩阵链乘法算法的实现 C++matrix chain multiplication矩阵链乘法算法的实现的完整源码(定义,实现,main函数 ...

  4. 编码分布式矩阵乘法(Coded Distributed Matrix Multiplication, CDMM)问题简单介绍

    许多现代分布式计算框架都会遇到大规模分布式矩阵乘法问题,即计算两个大规模矩阵和的乘积,如MapReduce.Spark.由于分布式计算系统会出现的无法预测的时延,主节点(master node)必须等 ...

  5. Hdu 4920矩阵乘法(内存访问的讲究)

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

  6. CUDA Samples: matrix multiplication(C = A * B)

    以下CUDA sample是分别用C++和CUDA实现的两矩阵相乘运算code即C= A*B,CUDA中包含了两种核函数的实现方法,第一种方法来自于CUDA Samples\v8.0\0_Simple ...

  7. tensorflow tf.matmul() (多维)矩阵相乘(多维矩阵乘法)

    @tf_export("matmul") def matmul(a,b,transpose_a=False,transpose_b=False,adjoint_a=False,ad ...

  8. 2020牛客国庆集训派对day2 MATRIX MULTIPLICATION CALCULATOR

    MATRIX MULTIPLICATION CALCULATOR 题意: 求两矩阵相乘 题解: 应该都学过把...矩阵相乘 矩阵相乘的前提是两个矩阵的列等于另一个矩阵的行 也就是cij=∑aik*bk ...

  9. (原創) 用OOP实作矩阵相乘 (C/C++)

    这是我修C++的第四次作业第一题,要我们从档案读进两个矩阵,最后相乘显示结果. 此程序主要展示了用OOP的方式计算矩阵,且用了STL的vector,而非传统的array. Matrix.h  1#if ...

最新文章

  1. 开发日记-20190911 关键词 C代码实现shell ftw命令
  2. 祝贺!屠呦呦再获国际大奖!一文读懂:神药青蒿素那些我们不知道的事
  3. myisam为什么比innodb查询快_InnoDB 和 MyISAM的数据分布是什么样的?
  4. Python List 列表list()方法分享
  5. 【数据结构与算法】判断单链表是否有环的算法
  6. UE4 Roadmap
  7. 中文的括号和英文的括号区别_家庭教育的困惑 (数学,中/英文)
  8. 《Unix/Linux网络日志分析与流量监控》2014年11月底出版
  9. impala查询数据与hive的查询数据比对(数据的校验)
  10. python试卷生成_小学初中高中试卷自动生成
  11. 2-9 装箱问题 (20 分)
  12. Linux 二进制分析
  13. 电脑开机主板报警1长2短是怎么回事?
  14. 华为认证云计算HICA
  15. 【PS CS6】替换证件照背景色
  16. pt函数html,pt是什么元素?
  17. android程序开发笔记
  18. 对java中public、static的理解
  19. web自动化验证码处理
  20. 学习openvz虚拟机

热门文章

  1. $.ajax注册表单
  2. [ECMAScript] 说说你对class的理解
  3. Taro+react开发(48)taro中switchTab
  4. 前端学习(3242):react总结生命周期
  5. 工作314:uni-提交成功加入表单验证
  6. 前端学习(2589):前端权限的设计思路
  7. 前端学习(2131):作用域插槽的使用
  8. 前端学习(1541):本地运行ng文档
  9. 前端学习(659):小结
  10. 项目管理(2):管理过程二