这个题,比较巧妙
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<string>
using namespace std;
//freopen("C://i.txt","r",stdin);
#define N 1001
int a[N][N];
int b[N][N];
int c[N][N];
int n,m,p;
int bb[N];
int cc[N];
int main()
{
freopen("C://i.txt","r",stdin);
int i,j,k;
cin>>n>>m>>p;
for (i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&a[i][j]);
for (i=1;i<=m;i++)
{
bb[i]=0;
for (j=1;j<=p;j++)
scanf("%d",&b[i][j]),bb[i]+=b[i][j];
}
for (i=1;i<=n;i++)
{
cc[i]=0;
for (j=1;j<=p;j++)
scanf("%d",&c[i][j]),cc[i]+=c[i][j];
}
int f=true;
for (i=1;i<=n;i++)
{
int sum=0;
for (j=1;j<=m;j++)
{
sum+=a[i][j]*bb[j];
}
// cout<<cc[i]<<' '<<sum<<endl;
if (sum!=cc[i])
{
f=false;
for (k=1;k<=p;k++)
{
sum=0;
for (j=1;j<=m;j++)
{
sum+=a[i][j]*b[j][k];
}
if (sum!=c[i][k])
{
cout<<"No"<<endl;
cout<<i<<' '<<k<<endl;
cout<<sum<<endl;
break;
}
}
break;
}
}
if (f)
cout<<"Yes"<<endl;
}
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 1957 | Accepted: 557 |
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