Poj 3318 matrix multiplication (randomization algorithm)

Source: Internet
Author: User

Here are three matrices A, B, and C. Let you determine whether a * B is equal to C.

A random group of data, and then determine whether it is equal after multiplying A and B with multiplication C.

I think this algorithm is not rigorous ,,,

Matrix Multiplication
Time limit:2000 ms   Memory limit:65536 K
Total submissions:16255   Accepted:3515

Description

You are given threeN×NMatricesA,BAndC. Does the equationA×B=CHold true?

Input

The first line of input contains a positive integerN(NLess than or equal to 500) followed by the three matricesA,BAndCRespectively. Each matrix's description is a block of n × n integers.

It guarantees that the elementsAAndBAre less than 100 in absolute value and elementsCAre less than 10,000,000 in absolute value.

Output

Output "yes" if the equation holds true, otherwise "no ".

Sample Input

21 02 35 10 85 110 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 longusing 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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.