poj 3213 叉姐的魔法训练

PM3
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 3400 Accepted: 1193

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.



题意:

给出两个矩阵,和他们相乘的答案矩阵;

这个计算过程中至多只有一个错误,问这个错误在哪并且求出正解,如果没有错误就输出yes

题解:

利用举证计算公式,发现规律,先计算行,判断错误在哪一行出现

然后在这一行进行枚举找到最终错误


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 1010
int a[MAXN][MAXN];
int b[MAXN][MAXN];
int c[MAXN][MAXN];
int sum1[MAXN],sum2[MAXN];

int main()
{
    int n,p,m;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&n,&p,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=p;j++)
            scanf("%d",&a[i][j]);

        for(int i=1;i<=p;i++){
            sum1[i]=0;
            for(int j=1;j<=m;j++)
            scanf("%d",&b[i][j]),sum1[i]+=b[i][j];
        }
        for(int i=1;i<=n;i++){
            sum2[i]=0;
            for(int j=1;j<=m;j++)
            scanf("%d",&c[i][j]),sum2[i]+=c[i][j];
        }

        int temp,row=0;
        for(int i=1;i<=n;i++){
            temp=0;
            for(int j=1;j<=p;j++)
                temp+=a[i][j]*sum1[j];
            if(temp!=sum2[i])
                row=i;
        }
        if(!row){
            puts("Yes");
            continue;
        }

        int col=0;
        for(int i=1;i<=m;i++){
            temp=0;
            for(int j=1;j<=p;j++)
                temp+=a[row][j]*b[j][i];
            if(temp!=c[row][i]){
                col=i;
                goto kkk;
            }
        }
        kkk:puts("No");
        printf("%d %d\n",row,col);
        printf("%d\n",temp);
    }
    return 0;
}


根据提供的引用内容,我们可以得知这是一道POJ(Peking University Online Judge)的题目,题号为3213。这道题目的主要任务是计算给定的一组数据中,每个数出现的次数,并输出出现次数最多的数的出现次数。同时,由于测试数据较大,使用C++中的iostream对象或Java中的Scanner对象可能会导致效率问题。 下面是Java语言的解题思路和代码实现: 1.首先,我们需要使用Java中的HashMap来存储每个数出现的次数。HashMap是一种基于哈希表实现的Map接口,可以用来存储键值对,其中键是唯一的,值可以重复。 2.接下来,我们需要读入数据,并将每个数出现的次数存储到HashMap中。由于输入数据的格式比较特殊,我们需要使用Java中的Scanner类来读取数据。具体来说,我们可以使用Scanner类的nextInt()方法来读取整数,并使用while循环来读取所有的数据。 3.最后,我们需要遍历HashMap,找到出现次数最多的数的出现次数,并输出结果。 下面是Java语言的代码实现: ```java import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int n = in.nextInt(); int m = in.nextInt(); for (int i = 0; i < n; i++) { int x = in.nextInt(); if (x != -1) { if (map.containsKey(x)) { map.put(x, map.get(x) + 1); } else { map.put(x, 1); } } } int max = 0; for (int key : map.keySet()) { max = Math.max(max, map.get(key)); } System.out.println(max * m); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值