PM3(矩阵相乘 行和的简便运算)
2015-12-09 17:15
441 查看
http://poj.org/problem?id=3213
PM3
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 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 “
the element of C at row r, column c should be corrected to v.
Sample Input
Sample Output
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];
PM3
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 3146 | Accepted: 1108 |
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 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 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; }
相关文章推荐
- PMCAFF微课堂「已结束」 | 产品汪如何做好“时间管理”让效率提高100倍
- 优秀的产品经理是如何炼成的
- 福利|PMCAFF问答专家群-社区大咖聚集地
- 产品经理的战场:需求评审会
- MPMoviePlayerController概述
- 转自36Kr --不靠谱产品经理养成
- 产品经理需要会写代码吗?
- 在ubuntu上软件安装的几种格式dev,rpm,bin,tar.gz
- rpm 安装mysql
- PMCAFF产品众测 | 对话随手攒CEO聊聊这款产品的设计、推广和改进(活动已结束)
- npm常用命令
- PMCAFF产品经理第一课 | 杭州站 现场集锦
- npm无反应的问题
- 自动化部署必备技能—定制化RPM包
- CCBPM流程结束的大总结
- 一步步创建自己的npm插件
- RPM程序包管理
- 北京 | PMCAFF年度老友会本周开始,老伙伴们聚起来!
- NPM 在MacOSX中的使用技巧
- RPM方式安装MySQL5.6