POJ 3318 - Matrix Multiplication 第一次用随机化解决问题...
随机化还是很厉害的...印象最深的是以前手写快排~~一般加个随机化会使耗时不受输入数据的..时间更加稳定
这个题是人品题了...开始交了好多遍都过不了..多交几次终于过了...
Program:
#include<iostream>
#include<stack>
#include<queue>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<time.h>
#include<cmath>
#define ll long long
#define oo 1000000007
#define MAXN 505
using namespace std;
int n,M[3][MAXN][MAXN];
bool judge()
{
int t,i,j,x,y,p,d;
for (t=1;t<=60000;t++)
{
y=rand()%n;
x=rand()%n;
d=0;
for (p=0;p<n;p++)
d+=M[0][y][p]*M[1][p][x];
if (d!=M[2][y][x]) return false;
}
return true;
}
int main()
{
int i,j,x;
scanf("%d",&n);
for (x=0;x<3;x++)
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&M[x][i][j]);
srand((unsigned)time(NULL));
if (judge()) printf("YES\n");
else printf("NO\n");
return 0;
}
POJ 3318 - Matrix Multiplication 第一次用随机化解决问题...的更多相关文章
- 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17783 Accepted: ...
- Poj 3318 Matrix Multiplication( 矩阵压缩)
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18928 Accepted: ...
- poj 3318 Matrix Multiplication 随机化算法
方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...
- [poj 3318] Matrix Multiplication (随机化+矩阵)
Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? In ...
- poj 3318 Matrix Multiplication
http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C #include <cstdio> #include <cstring> #incl ...
- POJ 3318 Matrix Multiplication(随机算法)
题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...
- POJ 3318 Matrix Multiplication(矩阵乘法)
题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...
- PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...
- PKU 3318 Matrix Multiplication(神奇的输入)
#include<cstdio> using namespace std; ][]; ][],C[][]; int Read() { ; ; while((ch=getchar())==' ...
随机推荐
- hdu 1035 Robot Motion(模拟)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- LINUX 网络编程 原始套接字
一 原始套接字 原始套接字(SOCK_RAW)是一种不同于SOCK_STREAM.SOCK_DGRAM的套接字,它实现于系统核心.然而,原始套接字能做什么呢?首先来说,普通的套接字无法处理ICMP.I ...
- 11gR2 RAC启用iptables导致节点宕机问题处理
通常,在安装数据库时,绝大多数都是要求把selinux及iptables关闭,然后再进行安装的.但是在运营商的系统中,很多安全的因素,需要将现网的数据库主机上的iptables开启的. 在开启ipta ...
- C#中按指定质量保存图片的实例代码 24位深度
/// <summary> /// 按指定的压缩质量及格式保存图片(微软的Image.Save方法保存到图片压缩质量为75) /// </summary ...
- 页面按F5重复提交数据解决方法
在Web开发中,必须面对的问题就是表单的重复提交问题(这里仅指F5刷新造成的重复提交),.NET中处理这个问题似乎没有什么好的方法. 在网上搜索得到的解决方法主要有两种,一种是直接让表单按钮失效,从而 ...
- uilable 换行标记
m_tipLabel.lineBreakMode = UILineBreakModeWordWrap; m_tipLabel.numberOfLines = 0; m_tipLabel.text = ...
- UIButton, KVC, KVO
按钮 自定义按钮:调整内部子控件的frame 方式1:实现titleRectForContentRect:和imageRectForContentRect:方法,分别返回titleLabel和imag ...
- linux安装php5.3
安装php的依赖包 [root@localhost admin]# unzip libxml2-2.7.8.tar.zip [root@localhost admin]#tar zvxf libxml ...
- C#性能优化实践 资料整理
缓存(Cache)是性能优化中最常用的优化手段.适用的情况是频繁的获取一些数据,而每次获取这些数据需要的时间比较长.这时,第一次获取的时候会用正常的方法,并且在获取之后把数据缓存下来.之后就使用缓存的 ...
- Get a handle on PHP Handlers
PHP Handlers? mod_php? FPM? How do we make sense of the inner workings of PHP outside of our lines o ...