POJ 3318 Matrix Multiplication(隨機化演算法)

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   strong   io   資料   

給你三個矩陣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 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 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

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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.