http://poj.org/problem?id=3213

PM3
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 3146   Accepted: 1108

Description

USTC has recently developed the Parallel Matrix Multiplication Machine – PM3, which is used for very large matrix multiplication.

Given two matrices A and B, where A is an N × P matrix and B is a P × M matrix, PM3 can compute matrix C = AB in O(P(N + P + M)) time. However the developers of PM3 soon discovered a small problem: there is a small chance that PM3 makes a mistake, and whenever a mistake occurs, the resultant matrix Cwill contain exactly one incorrect element.

The developers come up with a natural remedy. After PM3 gives the matrix C, they check and correct it. They think it is a simple task, because there will be at most one incorrect element.

So you are to write a program to check and correct the result computed by PM3.

Input

The first line of the input three integers NP and M (0 < NPM ≤ 1,000), which indicate the dimensions of A and B. Then follow N lines with P integers each, giving the elements of A in row-major order. After that the elements of B and C are given in the same manner.

Elements of A and B are bounded by 1,000 in absolute values which those of C are bounded by 2,000,000,000.

Output

If C contains no incorrect element, print “Yes”. Otherwise print “No” followed by two more lines, with two integers r and c on the first one, and another integer von the second one, which indicates the element of C at row r, column c should be corrected to v.

Sample Input

2 3 2
1 2 -1
3 -1 0
-1 0
0 2
1 3
-2 -1
-3 -2

Sample Output

No
1 2
1

Hint

The test set contains large-size input. Iostream objects in C++ or Scanner in Java might lead to efficiency problems.

题意:判断A、B两矩阵相乘的结果C矩阵中是否有唯一错的那个数字,如果没有,输出yes,,否则输出no,并输出错误数字位置和正确答案。

题解:由于有且只有一个位置有错,则可以简化为求出相乘后的矩阵的行和与C比较,若不相同,则找出了有错的行,然后在逐个比较找出错误的位置。技巧就在求行和的时候,矩阵的对应相乘后的行和等于A的某一行的数字分别乘以B的某一行之和,最后求和。          输入矩阵也可以只用一个循环:%s,a[i];

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<string>#define maxn 1010
using namespace std;int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
int bcol[maxn],ccol[maxn];
int main()
{int m,n,p;int fg=0,f1=0,f2=0,ans;scanf("%d %d %d",&n,&p,&m);for(int i=0;i<n;i++)for(int j=0; j<p; j++)scanf("%d",&a[i][j]);for(int i=0;i<p;i++)for(int j=0; j<m; j++)scanf("%d",&b[i][j]);for(int i=0;i<n;i++)for(int j=0; j<m; j++)scanf("%d",&c[i][j]);for(int i=0;i<p;i++){bcol[i]=0;for(int j=0;j<m;j++)bcol[i]+=b[i][j];}for(int i=0;i<n;i++){ccol[i]=0;for(int j=0;j<m;j++)ccol[i]+=c[i][j];}for(int i=0;i<n;i++){int temp=0;for(int j=0;j<p;j++)temp+=a[i][j]*bcol[j];if(temp!=ccol[i]){fg=1;f1=i;break;}}if(!fg)puts("Yes");else{for(int i=0;i<m;i++){int cnt=0;for(int j=0;j<p;j++){cnt+=a[f1][j]*b[j][i];}if(c[f1][i]!=cnt){f2=i;ans=cnt;break;}}puts("No");printf("%d %d\n",f1+1,f2+1);printf("%d\n",ans);}return 0;
}

PM3(矩阵相乘 行和的简便运算)相关推荐

  1. 用Python实现矩阵相乘

    题目1:计算两个普通矩阵A和B的乘积 #Description 计算两个矩阵A和B的乘积. #Input 第一行三个正整数m.p和n,0<=m,n,p<=10,表示矩阵A是m行p列,矩阵B ...

  2. html计算两个数相乘,多个矩阵相乘运算顺序

    三个矩阵相乘从左向右算还是从右算起 三个矩阵相乘从左向右算和从右算起都可以 据结合律(AB)C=A(BC),先算前两个与先算后两个都可以,只要矩阵的前后次序保持不变即可. 矩阵的数乘满足以下运算律: ...

  3. 矩阵以及转置矩阵python_Python实现的矩阵转置与矩阵相乘运算示例

    本文实例讲述了Python实现的矩阵转置与矩阵相乘运算.分享给大家供大家参考,具体如下: 矩阵转置 方法一 :使用常规的思路 def transpose(M): # 初始化转置后的矩阵 result ...

  4. 06 ,矩阵的运算:加法运算,数乘,矩阵乘向量,矩阵相乘

    1 ,矩阵计算 : 加法运算 前提 : 必须同型矩阵之间才可以进行加法运算 运算 : 两个 m * n 矩阵相加 总结 : 对应元相加 2 ,矩阵计算 : 数乘 计算规则 : 3 ,矩阵计算 : 矩阵 ...

  5. 动手写一个Caffe层:矩阵相乘Matmul

    动手写一个Caffe层:矩阵相乘Matmul 背景 实现 前向传播实现 后向传播实现 backward推导 小结 背景 最近在研究chainer网络的caffe实现,顺便也体验一下caffe.对于ca ...

  6. 并行计算——OpenMP加速矩阵相乘

    OpenMP是一套基于共享内存方式的多线程并发编程库.第一次接触它大概在半年前,也就是研究cuda编程的那段时间.OpenMP产生的线程运行于CPU上,这和cuda不同.由于GPU的cuda核心非常多 ...

  7. python numpy常用操作、Numpy 多维数组、矩阵相乘、矩阵乘以向量

    python numpy常用操作 Numpy基本操作 # 导入numpy import numpy as np # 生成numpy数组 x = np.array([1.0, 2.0, 3.0]) pr ...

  8. c++矩阵转置_python3 单行代码实现矩阵相乘

    Python中有许多模块用来进行科学与数学的运算. 例如,numpy就是其中的一个,而且numpy中就有大量.好使的矩阵乘法的函数. 即便如此,我们还是可以探究一下如何用Python的自带函数,在一行 ...

  9. C C++实现两矩阵相乘--模拟法

    目录 前言 数学中两矩阵怎么相乘? C/C++语言实现 运行结果 前言 11月左右大三找日常实习的时候,面试乱杀,但是笔试碰到了这个矩阵相乘的编程题有几次,可能脑瓜子晕,突然被绕来绕去写不出来,很无语 ...

最新文章

  1. valve 的设计_向Valve Portal开发人员学习游戏设计原则
  2. JZOJ 5476. 【NOIP2017提高组正式赛】奶酪
  3. javax.management.InstanceNotFoundException: org.springframework.boot:type=Admin,name=SpringApplicati
  4. 压测|关于PHP7和5.6的压测对比
  5. CF125E MST company (凸优化+MST)
  6. fileinputstream读取文件_压缩 20M 文件从 30 秒到 1 秒的优化过程
  7. datetimepicker一个不错的日历android特效
  8. oracle 10g在redhat4.6上的安装
  9. 英雄联盟大区测试稳定软件,英雄联盟官方公布新界面,部分大区展开测试,你最希望改什么?...
  10. 3D图形学(4):纹理贴图
  11. EVO工具在EUROC数据集TUM数据集,评测ORB-SLAM3和VINS-MONO
  12. 中文编程发展与兴起的重要意义
  13. 安装clustalw-2.1
  14. 自定义控件之下拉刷新列表
  15. web课程设计网页规划与设计 :旅游景点网站设计——西安(20页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 HTML静态网页作业
  16. 美国队长的工资 python代码-Python | 用Python画个美队盾牌送给你
  17. 扒一扒苹果的那些事儿—春节见闻随想
  18. CRC32 tools in Ubuntu /fedora
  19. UML图及UML工具使用技巧
  20. BNET通信区块链项目CEO刘建军受邀参加2018“区块链共识大会”

热门文章

  1. iPhoto的删除动画(转)
  2. ExecuteScalar()方法的使用
  3. 【Octave】柱面投影简析
  4. android4.1 l36h,1080P四核旗舰手机 索尼L36h抢先评测
  5. 爱因斯坦的经典名言精选
  6. QQ for Mac OS X : QQ表情管理插件
  7. android改微信号码,微信已支持改微信号:仅限安卓最新版,附修改办法
  8. hp服务器查看硬盘命令,HP hpssacli 常用命令
  9. springboot导出pdf到前端浏览器预览下载
  10. c语言fscanf读入字符,C语言fprintf()和fscanf()函数