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列的元素可表示为公式( ...
随机推荐
- Get与Post的差别
Http定义了与server交互的不同方法,最主要的方法有4种,各自是GET,POST.PUT,DELETE. URL全称是资源描写叙述符.我们能够这样觉得:一个URL地址,它用于描写叙述一个网络上的 ...
- 每天一个JavaScript实例-html5拖拽
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Algorithm Part I:Priority Queues
1.binary heap实现 BinaryHeap.h #ifndef BINARYHEAP_H #define BINARYHEAP_H class BinaryHeap { public: Bi ...
- ZOJ 3822 Domination(概率dp 牡丹江现场赛)
题目链接:problemId=5376">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 Edward ...
- android创建自定义对话框
创建如下自定义对话框: JAVA代码 LayoutInflater li = LayoutInflater.from(TagActivity. this); //NOTE final View Te ...
- oracle转Mysql中,varchar2(10)和number应该转换为什么类型? (转)
一. varchar2(10)和number应该转换为什么类型? oracle转成mysql时:varchar2(10)可以转成varchar(10)number则要看oracle中存储的具体是什么类 ...
- linux sed命令详解(转)
简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...
- android笔记6——intent的使用
今天挑出一节专门来说一下使用intent和intentfilter进行通信. 场景:一个Activity启动还有一个Activity. 前面已经讲了Fragment的切换,Fragment顾名思义是基 ...
- JDK基本介绍
JDK这是Java Development Kit 缩写,中国被称为Java开发套件.由SUN该公司提供.这是Java应用程序开发提供了编译和执行环境,所有的Java写程序都依赖于它. JDK能够将J ...
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(三)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...