POJ 3213 PM3 矩阵乘法优化
来源:互联网 发布:java中的垃圾回收机制 编辑:程序博客网 时间:2024/06/07 19:00
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 C will 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 N, P and M (0 < N, P, M ≤ 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 v on the second one, which indicates the element of C at rowr, column c should be corrected to v.
Sample Input
2 3 21 2 -13 -1 0-1 00 21 3-2 -1-3 -2
Sample Output
No。1 21
Hint
The test set contains large-size input. Iostream objects in C++ or Scanner in Java might lead to efficiency problems.
题意:给矩阵A,B,C。C == A*B,但是C中可能有一个数据是错的。没有错误输出Yes,有就输出No并输出几行几列和正确答案。
解法,暴力矩阵乘法可过,直接写就行
另一种,写矩阵乘法的时候,会发现A的每个数都会乘B中某一行的每个数字。这样,可以处理B的每一行的和,求的A*B的每一行的和与C的每一行的和进行比较,如果相等就没有错误数据,不想等就有错误数据,对出现错误的这一行与B进行矩阵乘法找出那个数据就行了。感觉还是很巧妙的。
暴力的写法已注释
/*暴力求解#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;const int N = 1000+10;int A[N][N],B[N][N],C[N][N];int main(){ int n,p,m; scanf("%d%d%d",&n,&p,&m); for(int i = 1;i <= n;i++) for(int j = 1;j <= p;j++) scanf("%d",&A[i][j]); for(int i = 1;i <= p;i++) for(int j = 1;j <= m;j++) scanf("%d",&B[i][j]); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) scanf("%d",&C[i][j]); int tmp; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ tmp = 0; for(int k = 1;k <= p;k++) tmp += A[i][k]*B[k][j]; if(tmp != C[i][j]){ printf("No\n%d %d\n%d\n",i,j,tmp); return 0; } } } printf("Yes\n"); return 0;}*/#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>using namespace std;const int N = 1000+10;int A[N][N],B[N][N],C[N][N];int b_c[N],c_c[N];int main(void){ int n,p,m; memset(b_c,0,sizeof b_c); memset(c_c,0,sizeof c_c); scanf("%d%d%d",&n,&p,&m); for(int i = 1;i <= n;i++) for(int j = 1;j <= p;j++) scanf("%d",&A[i][j]); for(int i = 1;i <= p;i++) for(int j = 1;j <= m;j++) scanf("%d",&B[i][j]),b_c[i] += B[i][j]; for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) scanf("%d",&C[i][j]),c_c[i] += C[i][j]; bool flag = false; int col; ///记录哪一行出错 for(int i = 1;i <= n;i++){ ///查找哪一行出错 int tmp = 0; for(int j = 1;j <= p;j++){ tmp += A[i][j]*b_c[j]; } if(tmp != c_c[i]) {col = i;flag = true;break;} } if(!flag) puts("Yes"); else{ puts("No"); for(int i = 1;i <= m;i++){ ///进行矩阵乘法查找就行 int tmp = 0; for(int j = 1;j <= p;j++){ tmp += A[col][j]*B[j][i]; } if(tmp != C[col][i]){ printf("%d %d\n%d\n",col,i,tmp); //return 0; } } } return 0;}
- POJ 3213 PM3 矩阵乘法优化
- POJ 3213 PM3 矩阵乘法
- POJ 3213 PM3 可能会
- POJ 3213 矩阵乘法(优化)
- poj 3734 Blocks 矩阵乘法优化dp
- POJ 3233 - 矩阵乘法及其性质和优化
- POJ 3744 分段矩阵乘法优化求概率
- POJ 3735 矩阵乘法的小优化GET。
- Poj 3734 Blocks(DP,矩阵乘法优化)
- 矩阵乘法的优化
- CUDA 矩阵乘法优化
- 矩阵乘法的优化
- CUDA: 矩阵乘法优化
- 矩阵乘法的优化
- 矩阵乘法优化
- CUDA: 矩阵乘法优化
- 矩阵乘法cache优化
- 矩阵乘法的优化
- Java使用Collections.sort对一个列表进行自定义排序
- 华为机试题--6.质数因子
- Android 注册页面 设置显示或者隐藏密码 和 点击获取验证码倒计时60s
- Web 基础学习之 用 JS 实现网页版本 计算器
- java多线程系列03
- POJ 3213 PM3 矩阵乘法优化
- 糗百的HTML更改后的python爬虫,2016,7,5 总计
- 作为基础开发人员必须掌握的linux常用命令
- 2016年该如何去做SEO?
- IntelliJ IDEA 添加项目后编译显示包不存在的解决方案
- android studio svn配置
- ESN向MEID转换常见问题解答
- 设计模式总结
- C++语言实现螺旋队列