Poj3318--matrix Multiplication randomization algorithm

Source: Internet
Author: User

Description

You are given three n x n matrices A, B and C. Does the equation A x 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 nxn integers.

It guarantees that the elements of A and B is less than of absolute value and elements of C is 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

This topic is too dump, completely with a random method after a limited number of attempts to determine whether the correct, the United States its name " randomization algorithm ." The actual is to take the random number, each time only the value of a position in the matrix C, if the result of a and B is the same as C, into the next cycle, the difference will jump out, and output No. After the finite cycles, if all is correct, the output is yes.
But the seed selection for random numbers in this case is calculated based on the current time , which means that the results are correctly related to the test cases, the number of attempts, and the current time. the same code commits at different times and may result in different results . There is a place to note,scanf really much faster than CIN , if using CIN simply can not try too many times, the results are definitely wrong.
Speed test on scanf and Cin There's a blog post that's written in more detail http://blog.sina.com.cn/s/blog_93294724010163rl.html

#include <iostream>#include<cstdio>#include<cstdlib>#include<ctime>using namespacestd;intN;intmatrixa[501][501];intmatrixb[501][501];intmatrixc[501][501];intMain () {scanf ("%d", &N);  for(intj=1; j<=n; ++j) { for(intk=1; k<=n; ++k) {scanf ("%d", &Matrixa[j][k]); }        }         for(intj=1; j<=n; ++j) { for(intk=1; k<=n; ++k) {scanf ("%d", &Matrixb[j][k]); }        }         for(intj=1; j<=n; ++j) { for(intk=1; k<=n; ++k) {scanf ("%d", &Matrixc[j][k]); }        }    BOOLFlag =true;      Srand ((unsigned) time (NULL));  for(intI=0; i<80000; i++)    {        intr = rand ()%n+1; intc = rand ()%n+1; intsum =0;  for(intj=1; j<=n; ++j) {Sum+ = matrixa[r][j]*Matrixb[j][c]; }        if(Sum! =Matrixc[r][c]) {Flag=false;  Break; }    }    if(flag) cout<<"YES"<<Endl; Elsecout<<"NO"<<Endl;}



Poj3318--matrix Multiplication randomization algorithm

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.