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. python 函数基础

    定义: def intersect(seq1,seq2): res = [] for x in seq1: if x in seq2: res.append(x) yield res 运行效果 > ...

  2. Javascript中的函数、this以及原型

    关于函数 在Javascript中函数实际上就是一个对象,具有引用类型的特征,所以你可以将函数直接传递给变量,这个变量将表示指向函数“对象"的指针,例如: function test(mes ...

  3. 笨小猴 2008年NOIP全国联赛提高组

    题目描述 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设m ...

  4. HDU 1029 Ignatius and the Princess IV --- 水题

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  5. Javascript高性能动画与页面渲染

    转自:http://www.infoq.com/cn/articles/javascript-high-performance-animation-and-page-rendering No setT ...

  6. Codeforces Round #339 Div.2 C - Peter and Snow Blower

    Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. A ...

  7. C++@重载函数

    关于重载详细分析参考: http://www.cnblogs.com/skynet/archive/2010/09/05/1818636.html 内部机制涉及重载函数如何解决命名冲突,调用匹配的问题 ...

  8. 【转】NSString属性什么时候用copy,什么时候用strong?

    原文网址:http://www.cocoachina.com/ios/20150512/11805.html 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境) ...

  9. 【转】完美解除Windows7的驱动程序强制签名限制

    原文网址:http://nick.txtcc.com/index.php/nocategory/290 Windows 7很J,很多驱动程序都无法安装,因为Windows 7不像Vista,必须要求所 ...

  10. C# TextBox 只能输入数字

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { TextBox txt = sender as TextBox ...