POJ3318--Matrix Multiplication 随机化算法
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 随机化算法的更多相关文章
- PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...
- poj 3318 Matrix Multiplication 随机化算法
方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...
- poj3318 Matrix Multiplication
poj3318 Matrix Multiplication 题意:给定$n*n(n<=500)$的矩阵$A,B,C$,如果$A*B==C$,输出“YES”,否则为“NO”:多组数据,$O(n^{ ...
- POJ 3318 Matrix Multiplication(随机算法)
题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...
- 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17783 Accepted: ...
- POJ 矩阵相乘 (随机化算法-舍伍德(Sherwood))
周三的算法课,主要讲了随机化算法,介绍了拉斯维加斯算法,简单的理解了为什么要用随机化算法,随机化算法有什么好处. 在处理8皇后问题的时候,穷举法是最费时的,回朔比穷举好点,而当数据量比较大的时候,如1 ...
- 【数学】Matrix Multiplication
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total S ...
- hdu4920 Matrix multiplication 模3矩阵乘法
hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场
Matrix multiplication Time ...
随机推荐
- Matlab位运算操作
本文为转载他人文章: bitand 按位与操作 a = 7; b = bitand(10,a); disp(dec2bin(a,8)); %ans = 00000111 disp(dec2bin(b, ...
- List-ApI及详解
1.API : add(Object o) remove(Object o) clear() indexOf(Object o) get(int i) size() iterator() isEmpt ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 作业:用HTML制作简历
代码为: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- Git入门指南十一:Git branch 分支与合并分支
十五. Git branch 分支 查看当前有哪些branch bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch * master ...
- ExtJS 的一些技巧与问题
1.修改列表(grid)里store的加载url grid.getStore().proxy.conn.url = "xxx.jsp"; grid.getStore().reloa ...
- checking
https://en.wikipedia.org/wiki/Java_performance https://en.wikipedia.org/wiki/Java_virtual_machine ht ...
- php中定义网站根目录的常用方法
define('BASE_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/../')));
- jquery ajax POST 例子详解
function test(){ $.ajax({ //提交数据的类型 POST GET type:"POST", //提交的网址 url:"testLogin.aspx ...
- excel让每个单元格的宽度随着字体自动变动的两种方式(有更好方法的大神,请忽略,求评论下)
1.打开在EXCEL的工作表,点击工作表左上方的方框,选中整个工作表,将鼠标一致A列的右边线处,也就是A列和B列中间的分界线处,光标会变成十字,鼠标左键双击,列宽会调整到最恰当处.截图如下