PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
题目大意:原题链接
给定三个n*n的矩阵A,B,C,验证A*B=C是否成立.
所有解法中因为只测试一组数据,因此没有使用memset清零
Hint中给的傻乎乎的TLE版本:
#include<cstdio>
#include<cstring>
int n,A[][];
int B[][],C[][];
void Input(int m[][])
{
for(int i=;i<=n;i++){
for(int j=;j<=n;j++)
scanf("%d",&m[i][j]);
}
}
int main()
{
scanf("%d",&n);
bool sign=true;
Input(A),Input(B),Input(C);
for(int i=;i<=n&&sign;i++){
for(int j=;j<=n&&sign;j++){
int sum=;
for(int k=;k<=n&&sign;k++)
sum+=A[i][k]*B[k][j];
if(sum!=C[i][j]) sign=false;
}
}
if(sign) printf("YES");
else printf("NO");
}
AC版本解法一:神奇的输入优化

之前就看到过几次一个神奇的输入模板,不知道这段代码是否就是那个(提交多次,觉得估计在提交人数多时容易超时)
#include<cstdio>
using namespace std;
int n,A[][];
int B[][],C[][]; int Read()
{
int d=;
char ch,t=;
while((ch=getchar())==' '||ch=='\n') ;
if(ch=='-') t=;
else d=ch-'';
while((ch=getchar())>=''&&ch<='')
d=d*+ch-'';
if(t) return -d;
else return d;
} int main()
{
scanf("%d",&n);
bool sign=true;
for(int i=;i<n;i++){
for(int j=;j<n;j++)
A[i][j]=Read();
}
for(int i=;i<n;i++){
for(int j=;j<n;j++)
B[i][j]=Read();
}
for(int i=;i<n;i++){
for(int j=;j<n;j++)
C[i][j]=Read();
}
for(int i=;i<n&&sign;i++){
for(int j=;j<n&&sign;j++){
int sum=;
for(int k=;k<n;k++)
sum+=A[i][k]*B[k][j];
if(sum!=C[i][j]){
sign=false;
break;
}
}
}
if(sign) printf("YES");
else printf("NO");
}
AC版本解法二:稳稳的随机化算法

#include<ctime>
#include<cstdio>
#include<cstdlib>
#define TLE 2000
int n,A[][];
int B[][],C[][]; void Input(int m[][])
{
for(int i=;i<=n;i++){
for(int j=;j<=n;j++)
scanf("%d",&m[i][j]);
}
}
bool Judge()
{
int r,c;//随机行数,列数
int time=*TLE;//注意取的次数需要适当
while(time--){
r=rand()%n+,c=rand()%n+;
int sum=;
for(int k=;k<=n;k++)
sum+=A[r][k]*B[k][c];
if(sum!=C[r][c]) return ;
}
return ;
} int main()
{
scanf("%d",&n);
srand(time(NULL));
Input(A),Input(B),Input(C);
if(Judge()) printf("YES");
else printf("NO");
}
AC版本解法三:流弊的状态压缩
思路;
/*
Problem B: Matrix Multiplication
The author gives an approximate algorithm rather than a precise one.
Randomize a n ×1 matrix X, test if the equation A × B × X = C × X holds true.
If it is not true we can safely say "NO" to this problem.
If it is true, the possibility that A × B ≠ X is extremely little.
*/
上面是北大网站上的提示,用行向量把C矩阵和A*B矩阵压缩了,看这一句:
Randomize a n ×1 matrix X,用一个随机的列向量。
具体怎实现呢?
用压缩矩阵再比较的方法:
我习惯左乘一个行向量,所以设一个行向量X,X是1*n的矩阵,若A*B等于C则必有X*A*B等于X*C,虽然多乘了一个向量,但是时间复杂度却降低到了O(n^2)了。
这样解的实质是把一个方阵压缩成了一个行向量,向量的每一个元素都是原矩阵该列的的和,也就是说用和来比较。这样大大节省了时间。但是带来一个问题:
如这两个矩阵:
x x x x x x x x x x x x x x x x x x x x
x x 1 x x x x 1 x x x x 0 x x x x 2 x x
x x x x x x x x x x x x x x x x x x x x
x x 1 x x x x 1 x x x x 2 x x x x 0 x x
…… ……
x x x x x x x x x x x x x x x x x x x x
用压缩再比较的方法不能得到正确结果。压缩再比较的关键在于怎么样在和中体现原来每个元素的个性。关键就是X行向量怎么设定。题目在比赛结束后提示是一个随机的向量X,就是把X的每个元素设为随机数。我觉得可以把X设为一个递增的向量:{1、2、…、n},这样更能体现每个元素的个性,而随机有可能在关键点上出现错误。

#include<cstdio>
int n,A[][];
int B[][],C[][];
int E[],e[],t[];
void Input(int m[][])
{
for(int i=;i<=n;i++){
for(int j=;j<=n;j++)
scanf("%d",&m[i][j]);
}
}
bool Judge(int n)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
t[i]+=j*A[j][i];
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
e[i]+=t[j]*B[j][i];
for(int i=;i<=n;i++)
if(e[i]!=E[i]) return false;
return true;
}
int main()
{
scanf("%d",&n);
Input(A),Input(B),Input(C);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
E[i]+=j*C[j][i];//关键部分:j*C[j][i]就是给C矩阵左乘了一个递增的行向量X:{1,2,3,...,n}
if(Judge(n)) printf("YES");
else printf("NO");
}
PKU 3318 Matrix Multiplication(随机化算法||状态压缩)的更多相关文章
- poj 3318 Matrix Multiplication 随机化算法
方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...
- POJ 3318 Matrix Multiplication(随机算法)
题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...
- PKU 3318 Matrix Multiplication(神奇的输入)
#include<cstdio> using namespace std; ][]; ][],C[][]; int Read() { ; ; while((ch=getchar())==' ...
- 数学(矩阵乘法,随机化算法):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: ...
- POJ3318--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 第一次用随机化解决问题...
随机化还是很厉害的...印象最深的是以前手写快排~~一般加个随机化会使耗时不受输入数据的..时间更加稳定 这个题是人品题了...开始交了好多遍都过不了..多交几次终于过了... Program: #i ...
- [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 ...
随机推荐
- 转载: crypto:start() 错误。
错误信息: Eshell V5.10.3 (abort with ^G)1> crypto:start().** exception error: undefined function cry ...
- mysql插入多行数据
表结构如图:
- 【转】关于OnPaint的工作机制
转载出处:http://blog.csdn.net/foreverhuylee/article/details/21889025 用了两年的VC++,其实对OnPaint的工作原理一直都是一知半解.这 ...
- WPF XAML 特殊字符(小于号、大于号、引号、&符号)
XAML 受限于 XML 规则.例如, XML 特别关注一些特殊字符,如 & < > 如果试图使用这些字符设置一个元素内容,将会遇到许多麻烦,因为 XAML 解析器认为您正在做其 ...
- ItcastOA_设计BaseDao_设计DAO接口和实现类_写DAO实现类中的方法内容
3. 基础功能 3.1. 设计BaseDao接口与BaseDaoImpl类 每个实体都应有一个对应的Dao,他封装了对这个实体的数据库操作.例 实体Dao接口实现类 ================= ...
- mybatis 控制台打印出来的sql 执行结果为空 但是将sql放到mysql执行有数据
mybatis中的sql如下 select airln_Cd airlnCd,city_coordinate_j cityCoordinateJ,city_coordinate_w cityCoord ...
- [libwww-perl]——POST方法的使用
libwww-perl是我在学习varnish的时候遇到的一个工具. 具体libwww-perl是干什么的,可以参考官网https://github.com/libwww-perl/libwww-pe ...
- 深搜———ZOJ 1004:anagrams by stack
细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深 ...
- oracle 存储过程返回结果集
好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集. 配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过 ...
- 【BZOJ4254】Aerial Tramway 树形DP
[BZOJ4254]Aerial Tramway 题意:给你一座山上n点的坐标,让你在山里建m条缆车,要求缆车两端的高度必须相等,且中间经过的点的高度都小于缆车的高度.并且不能存在一个点位于至少k条缆 ...