PM3
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 NP and M (0 < NPM ≤ 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


题目意思是,给出A,B,C三个矩阵,C为给出的A*B结果。可是可能会有错。而错最多是一个。

用立方算法必跪无疑。

用的是奇技淫巧啊。

对于注意到C的第i行行和,等于sum(A[i]*B[i]行行和)
逐行去比較,出错的必定在i行。
之后再逐个去比較。推断出出错列,就OK拉。

受教了。
#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(矩阵乘法)的更多相关文章

  1. *HDU2254 矩阵乘法

    奥运 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...

  2. *HDU 1757 矩阵乘法

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. CH Round #30 摆花[矩阵乘法]

    摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...

  4. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  5. bzoj 2738 矩阵乘法

    其实这题跟矩阵乘法没有任何卵关系,直接整体二分,用二维树状数组维护(刚刚学会>_<),复杂度好像有点爆炸(好像有十几亿不知道是不是算错了),但我们不能怂啊23333. #include&l ...

  6. 【BZOJ-2476】战场的数目 矩阵乘法 + 递推

    2476: 战场的数目 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 58  Solved: 38[Submit][Status][Discuss] D ...

  7. 【BZOJ-1898】Swamp 沼泽鳄鱼 矩阵乘法

    1898: [Zjoi2005]Swamp 沼泽鳄鱼 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1012  Solved: 566[Submit][S ...

  8. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  9. 矩阵乘法的MapReduce实现

    对于任意矩阵M和N,若矩阵M的列数等于矩阵N的行数,则记M和N的乘积为P=M*N,其中mik 记做矩阵M的第i行和第k列,nkj记做矩阵N的第k行和第j列,则矩阵P中,第i行第j列的元素可表示为公式( ...

随机推荐

  1. JsonCpp Documentation

    JsonCpp - JSON data format manipulation library JsonCpp Documentation 0.6.0-rc2 Introduction JSON (J ...

  2. hdu 折线切割平面 (java)

    问题: 仅仅要找到规律问题就攻克了,在做题时应该细致去发现数与数之间的联系. 折线切割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  3. Knockout应用开发指南 第三章:绑定语法(2)

    原文:Knockout应用开发指南 第三章:绑定语法(2) 7   click 绑定 目的 click绑定在DOM元素上添加事件句柄以便元素被点击的时候执行定义的JavaScript 函数.大部分是用 ...

  4. HashMap-死锁导致cpu占用100%分析(转)

    最近项目里面的一段千年代码出了问题,这个问题以前也出现过,不过不是那么明显,这次迁移机器由以前的4台机子变成2台以后问题被放大,最终不得不解决,特此分析一下. 先放出问题的代码 ? 1 2 3 4 5 ...

  5. SVN的命令行操作

    最近在进行svn二次开发,使用的是svnkit.看了很多svnkit的api,渐渐发现都是和SVN的命令行操作对应的.顺便研究一下svn的命名行操作. 1.将文件checkout到本地目录: 基本命令 ...

  6. Java EE (10) - 资源服务器的整合

    加密(Encryption)和数字签名(Digital Signature)通常被用于保护通讯--加密用来防止数据传输过程中的窃听--数字签名用来防止数据传输过程中的篡改 JDBC: 整合关系型数据库 ...

  7. java获取指定地址图片高度宽度简单代码

    package com.test; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.In ...

  8. WPF Delegate委托整理

    那啥,是从这里整理出来的,感谢Rising_Sun,整理的过于简单,看不明白的戳这里 using System; using System.Collections.Generic; using Sys ...

  9. elasticsearch2.2

    elasticsearch2.2 集群搭建各种坑     目前生产环境的es版本是1.0版本,需要升级到最新的2.2版本,于是在测试环境进行部署集群测试,在测试过程中遇到的坑相当多,下面详细介绍下. ...

  10. HDOJ 5147 Sequence II 树阵

    树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    ...