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. Cocos2d-x 2地图步行实现:SPFA算法

    本文乃Siliphen原创,转载请注明出处:http://blog.csdn.net/stevenkylelee 上一节<Cocos2d-x 地图行走的实现1:图论与Dijkstra算法> ...

  2. Android Ant打包笔记

    本文文档的下载地址(Word版):http://download.csdn.net/detail/yangwei19680827/7250711 Android Ant 打包 网上找了ant打包的资料 ...

  3. Windows phone 8 学习笔记(8) 定位地图导航

    原文:Windows phone 8 学习笔记(8) 定位地图导航 Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模 ...

  4. Java命令参数说明

    Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令 JAVA_HOME"bin"java –option 来启动,-option为虚 ...

  5. hdu1869六度分离,spfa实现求最短路

    就是给一个图.假设随意两点之间的距离都不超过7则输出Yes,否则 输出No. 因为之前没写过spfa,无聊的试了一下. 大概说下我对spfa实现的理解. 因为它是bellmanford的优化. 所以之 ...

  6. Android build-tools升级到23.0.0_rc1无法解决编译后的问题

    背景 作为项目要改变android studio,它采取了.他们主动向我,结果下载了最新的build-tools 23.0.0_rc1,然后,当我再次使用eclipse不了了.git reset了n次 ...

  7. 线段树(单点更新and成段更新)

    线段树需要的空间. 区间为1-->n 假设是一棵完全二叉树,且树高为i. 完全二叉树性质:第i层最多有2^(i-1)个结点. 那么 2^(i-1) = n;     i = log2(n)  + ...

  8. C语言 cgi(2)

    1Columbia Universitycs3157 – Advanced ProgrammingSummer 2014, Lab #3, 40 pointsJune 10, 2014This lab ...

  9. ubuntu12.04 安装和配置jdk1.7

    第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...

  10. Duanxx的C++学习: const指针具体解释

    Const指的是一个编译时的常量. keywordconst使得代码能够确定一个变量能否够被改动. 使用了const后,能够防止对变量或者指针的改动:更重要的是,const的引用能够防止对所引用的对象 ...