POJ3213(矩阵乘法)
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 3036 | Accepted: 1059 |
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 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.
Source
#include <iostream>
#include <cstdio>
using namespace std;
#define N 1001
int a[N][N],b[N][N],c[N][N];
int c_col[N],b_col[N];
int main()
{
int n,m,p,i,j,k; while(scanf("%d%d%d",&n,&m,&p)!=EOF){
for( i=0;i<n;i++){
for( j=0;j<m;j++){
scanf("%d",&a[i][j]); }
}
for( i=0;i<m;i++){
for( j=0;j<p;j++){
scanf("%d",&b[i][j]);
}
}
for( i=0;i<n;i++){
for( j=0;j<p;j++){
scanf("%d",&c[i][j]);
}
}
for(i=0;i<m;i++){
b_col[i]=0;
for(j=0;j<p;j++){
b_col[i]+=b[i][j];
}
}
for(i=0;i<n;i++){
c_col[i]=0;
for(j=0;j<p;j++){
c_col[i]+=c[i][j];
}
}
for(i=0;i<n;i++){
int tmp=0;
for(j=0;j<m;j++){
tmp+=a[i][j]*b_col[j];
}
if(tmp!=c_col[i]){
break;
}
}
if(i==n){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
//i line is wrong
for(j=0;j<p;j++){
int res=0;
for(k=0;k<m;k++){
res+=a[i][k]*b[k][j];
}
if(res!=c[i][j]){
cout<<i+1<<" "<<j+1<<endl;
cout<<res<<endl;
break;
}
} } }
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
POJ3213(矩阵乘法)的更多相关文章
- *HDU2254 矩阵乘法
奥运 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...
- *HDU 1757 矩阵乘法
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- CH Round #30 摆花[矩阵乘法]
摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...
- POJ3070 Fibonacci[矩阵乘法]
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- bzoj 2738 矩阵乘法
其实这题跟矩阵乘法没有任何卵关系,直接整体二分,用二维树状数组维护(刚刚学会>_<),复杂度好像有点爆炸(好像有十几亿不知道是不是算错了),但我们不能怂啊23333. #include&l ...
- 【BZOJ-2476】战场的数目 矩阵乘法 + 递推
2476: 战场的数目 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 58 Solved: 38[Submit][Status][Discuss] D ...
- 【BZOJ-1898】Swamp 沼泽鳄鱼 矩阵乘法
1898: [Zjoi2005]Swamp 沼泽鳄鱼 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1012 Solved: 566[Submit][S ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- 矩阵乘法的MapReduce实现
对于任意矩阵M和N,若矩阵M的列数等于矩阵N的行数,则记M和N的乘积为P=M*N,其中mik 记做矩阵M的第i行和第k列,nkj记做矩阵N的第k行和第j列,则矩阵P中,第i行第j列的元素可表示为公式( ...
随机推荐
- ubuntu下ssh使用 与 SCP 使用
1 ssh远程登录服务器 ssh username@remote_ip #将username换成自己的用户名,将remote_ip换成远程服务器的ip地址 2 将文件/文件夹从远程服务器拷至本地(sc ...
- 怎样在屏幕上显示多个alv
本文解说怎样在屏幕上显示多个alv. 实现这种需求关键是下面几点(举例:在屏幕上显示4个alv): 1.须要定义4个alv control 2.由于有4个alv control,于是就须要定义4个容器 ...
- 蚁群算法 matlab程序(已执行)
下面是解放军信息project大学一个老师编的matlab程序,请尊重原作者劳动,引用时请注明出处. 我经过改动添加了凝视,已经执行过,无误, function [R_best,L_best,L_av ...
- JavaWeb学习总结(一)JavaWeb开发入门
静态网页和动态网页 静态网页:在服务器上没有经过服务器解释执行的网页. 动态网页:在服务器上经过服务器解释执行的网页. 无论是静态网页还是动态网页,客户端看到的网页都是由HTML所构成的,所以Java ...
- C#之再议数组和集合
1.数组 1.1简单数组 1.2 多维数组 1.3锯齿数组 1.4Array数组 1.5作为参数的数组 1.6枚举 1.7结构 以上部分可参考 http://www.cnblogs.com/ztb12 ...
- Android编程 获取网络连接状态 及调用网络配置界面
获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...
- 21天教你学会C++
- windows phone 墓碑化(9)
原文:windows phone 墓碑化(9) 几个术语的理解: 名称 说明 ; App ap = (App)Application.Current; // 构造函数 ...
- java移位运算符具体解释
java移位运算符不外乎就这三种:<<(左移).>>(带符号右移)和>>>(无符号右移). 1. 左移运算符 左移运算符<<使指定值的全部位都左移 ...
- CTR校准
普遍预测CTR不准,需要校准.例如.boosted trees and SVM预測结果趋于保守.即预測的概率偏向于中值:而对于NaiveBayes预測的概率,小概率趋于更小.大概率趋于更大.经常使用的 ...