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

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

Sample Output

YES

这个题目太甩了, 完全用随机的方法在有限次尝试以后,判断是否正确, 美其名曰 “随机化算法”。 实际就是取随机数,每次只计算矩阵C中一个位置上的值,如果通过A、B 计算出来的结果与C相同,进入下一次循环,不同就跳出,同时输出NO。 有限次循环后,如果都正确,输出YES。
但是这里面随机数的种子选取是根据当前时间来计算的,也就意味着结果是否正确跟测试用例、尝试次数和当前时间都有关系。同样的代码不同时间提交,可能结果不同。 还有个地方要注意,scanf真的比cin快好多,如果用cin根本没办法尝试太多次,得到的结果肯定错误。
关于scanf和cin的速度测试这有一篇博文写的比较详细 http://blog.sina.com.cn/s/blog_93294724010163rl.html
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime> using namespace std; int n; int matrixA[][];
int matrixB[][];
int matrixC[][]; int main()
{
scanf("%d", &n); for(int j=; j<=n; ++j)
{
for(int k=; k<=n; ++k)
{
scanf("%d", &matrixA[j][k]);
}
}
for(int j=; j<=n; ++j)
{
for(int k=; k<=n; ++k)
{
scanf("%d", &matrixB[j][k]);
}
}
for(int j=; j<=n; ++j)
{
for(int k=; k<=n; ++k)
{
scanf("%d", &matrixC[j][k]);
}
}
bool flag = true; srand((unsigned)time(NULL));
for(int i=; i<; i++)
{
int r = rand()%n+;
int c = rand()%n+;
int sum = ;
for(int j=; j<=n; ++j)
{
sum += matrixA[r][j]*matrixB[j][c];
} if(sum != matrixC[r][c])
{
flag = false;
break;
}
} if(flag)
cout << "YES" << endl;
else
cout << "NO" << endl; }
												

POJ3318--Matrix Multiplication 随机化算法的更多相关文章

  1. PKU 3318 Matrix Multiplication(随机化算法||状态压缩)

    题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...

  2. poj 3318 Matrix Multiplication 随机化算法

    方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...

  3. poj3318 Matrix Multiplication

    poj3318 Matrix Multiplication 题意:给定$n*n(n<=500)$的矩阵$A,B,C$,如果$A*B==C$,输出“YES”,否则为“NO”:多组数据,$O(n^{ ...

  4. POJ 3318 Matrix Multiplication(随机算法)

    题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...

  5. 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17783   Accepted: ...

  6. POJ 矩阵相乘 (随机化算法-舍伍德(Sherwood))

    周三的算法课,主要讲了随机化算法,介绍了拉斯维加斯算法,简单的理解了为什么要用随机化算法,随机化算法有什么好处. 在处理8皇后问题的时候,穷举法是最费时的,回朔比穷举好点,而当数据量比较大的时候,如1 ...

  7. 【数学】Matrix Multiplication

                                 Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  8. hdu4920 Matrix multiplication 模3矩阵乘法

    hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  9. hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

    Matrix multiplication                                                                           Time ...

随机推荐

  1. java.lang.OutOfMemoryError: unable to create new native thread如何解决

    工作中碰到过这个问题好几次了,觉得有必要总结一下,所以有了这篇文章,这篇文章分为三个部分:认识问题.分析问题.解决问题. 一.认识问题: 首先我们通过下面这个 测试程序 来认识这个问题:运行的环境 ( ...

  2. 《剑指Offer》之替换空格

    1.题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 2.代码实 ...

  3. A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python)

    A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON  ...

  4. Learn python the hard way. python test program 2016.04.27

    # this will not be printed in python ! print "I could have code like this." # and the comm ...

  5. Compiler ,Interpreter, Linker

    https://en.wikipedia.org/wiki/Interpreter_(computing) https://en.wikipedia.org/wiki/Compiler https:/ ...

  6. 亲测 logminer挖掘

    LogMiner两种使用类型,一种是使用源数据库的数据字典分析DML操作,别一种是摘取LogMiner数据字典到字典文件分析DDL操作.检查下suppplemental logging:SQL> ...

  7. 制作和unity调用动态链接库dll文件

    首先用vc建立一个dll工程 然后在里面建立一个testunity.h文件.内容如下 1 extern "C" int _declspec(dllexport)testunity( ...

  8. Page cache和Buffer cache[转1]

    http://www.cnblogs.com/mydomain/archive/2013/02/24/2924707.html Page cache实际上是针对文件系统的,是文件的缓存,在文件层面上的 ...

  9. C++ typedef详解

    1.typedef的用途1)定义一种类型的别名注意typedef并不是简单的宏替换,如下例所示: int main() { char *pa,pb;//声明了一个指向字符变量的指针pa,和一个字符变量 ...

  10. PHP使用libevent实现高性能httpServer

    今天发现php也有一个libevent的扩展,安装后尝试下了一个webserver(httpserver), 发现性能还不错,逻辑很简单,每秒响应速度1800~4000次/s 代码如下 <?ph ...