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 ...
随机推荐
- 滚动条滚动事件 js
<div class="xiangxix"> <ul> <li class=&qu ...
- shamir叠像术 分类: 图像处理 2015-07-08 16:50 17人阅读 评论(1) 收藏
K=imread('SHNU.bmp'); height=info.Height; width=info.Width; A=zeros(height,width); B=zeros(height,wi ...
- leetcode 109 Convert Sorted List to Binary Search Tree ----- java
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Codeforces Round #145 (Div. 2, ACM-ICPC Rules)
A. Lefthanders and Righthanders \(i\)与\(i+\frac n2\)匹配,根据左右手调整位置. B. Reading 排序,取前\(k\)个. C. Weather ...
- C++ Language
C++ Language Reference https://msdn.microsoft.com/en-us/library/3bstk3k5(v=vs.120).aspx
- Softmax回归(使用theano)
# coding:utf8 import numpy as np import cPickle import theano import os import theano.tensor as T cl ...
- C# String与string的区别
C#中同时存在String与string MSDN中对string的说明: string is an alias for String in the .NET Framework.string是Str ...
- rman的使用
下面是两种连接方式[oracle@oracle3A ~]$ rman target/ Recovery Manager: Release 11.2.0.1.0 - Production on Mon ...
- UML学习(类图和序列图等)
visio绘制UML图使用visio 提示此UML形状所在的绘图页不是UML模型图的一部分 请问这个问题怎么解决?新建->选择绘图类型->选择软件与数据库模板->选择UML模型图-& ...
- HBase(二): c#访问HBase之股票行情Demo
上一章完成了c#访问hbase的sdk封装,接下来以一个具体Demo对sdk进行测试验证.场景:每5秒抓取指定股票列表的实时价格波动行情,数据下载后,一方面实时刷新UI界面,另一方面将数据放入到在内存 ...