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列的元素可表示为公式( ...
随机推荐
- Egret是一套完整的HTML5游戏开发解决方案
Egret是一套完整的HTML5游戏开发解决方案.Egret中包含多个工具以及项目.Egret Engine是一个基于TypeScript语言开发的HTML5游戏引擎,该项目在BSD许可证下发布.使用 ...
- SPOJ 375(树链剖分)
题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 题意:一棵包含N 个结点的树,每条边都有一个权值, ...
- Qt调用word 例子
Qt调用word 例子 Getting Microsoft Word Object to SaveAs #include <QtGui> #include <QAxObject> ...
- js面向对象编程:命名空间
在其它语言中为了避免类和方法重名问题,都有一个类似命名空间的概念,在js中实现类似的功能吗? 能够实现,主要是借助于js中对象的概念来实现,比如: 1 在命名空间中定义方法属性 var GiantC ...
- bat文件无法双击运行
问题: win7系统下新建txt文件,编辑脚本内容后,保存为test.bat.每次双击它,只会默认以txt格式打开它,而不是运行它. 解决: 1. 双击打开“我的电脑”,然后在“工具”下选择“文件夹选 ...
- js中 正則表達式
正則表達式使用具体解释 简单介绍 简单的说,正則表達式是一种能够用于模式匹配和替换的强有力的工具.其作用例如以下: 測试字符串的某个模式.比如,能够对一个输入字符串进行測试,看在该字符串是否存在一个电 ...
- ubuntu 系统设置bugzilla制
随着时间的推移.在大脑中形成的记忆总会慢慢的淡去.人的记忆力就是这样.所以最好的办法就是形成博客去记录下来,一方面给自己以后回想用.一方面也算是自己的一个积累.所以一旦选择了一个行业,最好不要轻 易转 ...
- hdu 2191 悼念512四川汶川大地震遇难者——如今宝,感恩生活
悼念512四川汶川大地震遇难者--如今宝,感恩生活 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- HTML5 CSS3 诱人的实例 : 网页载入进度条的实现,下载进度条等
今天给大家带来一个比較炫的进度条,进度条在一耗时操作上给用户一个比較好的体验,不会让用户认为在盲目等待,对于没有进度条的长时间等待,用户会任务死机了,毫不犹豫的关掉应用:一般用于下载任务,删除大量任务 ...
- android 去除标题
//去除标题,必须在setContentView之前设置 requestWindowFeature(Window.FEATURE_NO_TITLE);