各种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. Learn OpenGL (十一):光照贴图
  2. Python遍历字典删除元素
  3. CentOS常见配置
  4. Python中元组的介绍以及常见操作
  5. AI学习---卷积神经网络
  6. 【机器视觉】Qt联合Halcon编程之显示多图片
  7. linux mysql8配置文件_Linux下 MySQL8安装教程
  8. 小波分析实验: 实验1 连续小波变换
  9. 设置git协议clone代理
  10. 咱中国女人太贪钱? 其实真不怨她们
  11. upnp协议和dlna源码理解与修改
  12. Android 通过WebService进行网络编程,使用工具类轻松实现
  13. UNIX环境高级编程 第11章 线程
  14. Git 可视化管理工具 - Sourcetree 使用指南
  15. 一天一个产品分析之美拍_米米米米粒口红_新浪博客
  16. iphone,ipad尺寸汇总
  17. win7去掉桌面快捷方式小箭头
  18. 一包辣条如何逆袭,从屌丝品牌成为有逼格的产品?
  19. 好好说话之Chunk Extend/Overlapping
  20. 快手算法岗日常实习面试经验

热门文章

  1. [Redux/Mobx] redux和flux的区别是什么?
  2. [react] react的mixins有什么作用?适用于什么场景?
  3. [react] React的render中可以写{if else}这样的判断吗?
  4. Taro+react开发(77):taro项目目录介绍
  5. React开发(128):ant design学习指南之input中addonBefore
  6. [html] 当img标签中的src图片加载失败时,怎么让它变得更美观呢?
  7. [css] 请描述margin边界叠加是什么及解决方案
  8. [css] 怎么让英文单词的首字母大写?
  9. 前端学习(2766):生命周期函数
  10. 前端学习(2507):初始化多个实例化对象