POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C。让你判断A*B是否等于C。

随机一组数据,然后判断乘以A,B之后是否与乘C之后相等。

很扯淡的啊,感觉这种算法不严谨啊、、、

Matrix Multiplication

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16255   Accepted: 3515

Description

You are given three n × n matrices AB and C. Does the equation A × B = C hold true?

Input

The first line of input contains a positive integer n (n ≤ 500) followed by the the three matrices AB and respectively. Each matrix‘s description is a block of n × n integers.

It guarantees that the elements of A and B are less than 100 in absolute value and elements of C are less than 10,000,000 in absolute value.

Output

Output "YES" if the equation holds true, otherwise "NO".

Sample Input

2
1 0
2 3
5 1
0 8
5 1
10 26

Sample Output

YES

Hint

Multiple inputs will be tested. So O(n3) algorithm will get TLE.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <time.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
#define Mod 1000000007
#define LL long long

using namespace std;

const int maxn = 510;
int a[maxn][maxn];
int b[maxn][maxn];
int c[maxn][maxn];
int f[maxn];
int aa[maxn];
int bb[maxn];
int cc[maxn];

int main()
{
    int n;
    cin >>n;
    memset(aa, 0, sizeof(aa));
    memset(bb, 0, sizeof(bb));
    memset(cc, 0, sizeof(cc));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) scanf("%d",&a[i][j]);
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) scanf("%d",&b[i][j]);
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) scanf("%d",&c[i][j]);

    srand((unsigned int)time(0));
    for(int i = 0; i < n; i++)
        f[i] = rand()%100;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            bb[i] += b[i][j]*f[j];
            cc[i] += c[i][j]*f[j];
        }
    }
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) aa[i] += a[i][j]*bb[j];
    int flag = 0;
    for(int i = 0; i < n; i++)
    {
        if(aa[i] != cc[i])
        {
            flag = 1;
            break;
        }
    }
    if(flag)
        cout<<"NO"<<endl;
    else
        cout<<"YES"<<endl;
}

POJ 3318 Matrix Multiplication(随机化算法),布布扣,bubuko.com

时间: 2024-08-05 01:52:34

POJ 3318 Matrix Multiplication(随机化算法)的相关文章

[ACM] POJ 3318 Matrix Multiplication (随机化算法)

Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16118   Accepted: 3485 Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? Input The first line of input contains a posit

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];

POJ 3318 Matrix Multiplication(矩阵乘法)

题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. 1 //3318 2 #include <stdio.h> 3 #include <string.h> 4 #include <time.h> 5 #include <stdlib.h> 6 #include <iostream> 7 8 using nam

POJ 题目3318 Matrix Multiplication(快速判断矩阵乘是否正确)

Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17132   Accepted: 3732 Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? Input The first line of input contains a posit

POJ3318--Matrix Multiplication 随机化算法

Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? Input The first line of input contains a positive integer n (n ≤ 500) followed by the the three matrices A, B and C respectively. Each matrix's descript

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640 

2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)

题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据是不可能会有的,而我们一开始一直在想极端数据能接受的方法......后来看了鹏哥的做法,就是把是0的地方都跳过就可以了,用矩阵保存前一个非0数的位置是多少.二师兄给我看了一个代码,人家根本没用别的优化,直接将最里层k的循环提到了最外层,然后就AC了,对此我表示无语. 1 #include <cstd

hdu4920 Matrix multiplication 模3矩阵乘法

hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 568    Accepted Submission(s): 225 Problem Description Given two matrices A and B of size n×n, find the product o

POJ 2155 Matrix (二维线段树)

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. I