POJ3213(矩阵乘法)

简介:
PM 3
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;
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4728310.html,如需转载请自行联系原作者


相关文章
AcWing 753. 平方矩阵 I
AcWing 753. 平方矩阵 I
70 0
AcWing 753. 平方矩阵 I
AcWing 754. 平方矩阵 II
AcWing 754. 平方矩阵 II
95 0
AcWing 754. 平方矩阵 II
AcWing 755. 平方矩阵 III
AcWing 755. 平方矩阵 III
85 0
AcWing 755. 平方矩阵 III
|
索引
leetcode题解 - 转置矩阵
给你一个二位整数数组matrix,返回matrix的转置矩阵。 矩阵的转置是指将矩阵的主对角线反转,交换矩阵的行索引与列索引
176 0
leetcode题解 - 转置矩阵
|
机器学习/深度学习 人工智能 算法
【算法导论】矩阵乘法
离过年都不到十天了,还要等到这周五才能回家,想想也一年没回家了。从寒假开始到现在,已经有二十来天,这期间把2014年总结中的寒假计划也大多数完成了:The Element Of Style的阅读,三门数学课《随机过程》、《工程优化》、《数值分析》的算法实现。
1107 0