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 ...
随机推荐
- (转)Python中的random模块
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- connect() failed (111: Connection refused) while connecting to upstream, cli
php-fpm没有运行 执行如下命令查看是否启动了php-fpm,如果没有则启动你的php-fpm即可 netstat -ant | grep 9000 没有运行为空,有运行显示 tcp 0 0 12 ...
- Hibernate_day01--解决配置文件没有提示问题_演示常见错误
解决配置文件没有提示问题 1 可以上网 2 把约束文件引入到eclipse中 (1)在配置文件中复制一句话 重启eclipse开发工具 演示常见错误 1 在映射配置文件中,把name属性值写错了,和实 ...
- ArcGIS 相同要素类的多Shp文件或多要素合并
- c++11——多线程
c++11中增加了线程以及线程相关的类,很方便的支持了并发编程. 1. 线程 线程创建 使用std::thread创建线程,提供线程函数或者函数对象即可,并且可以指定线程函数的参数. #inc ...
- 简单深搜:POJ1546——Sum it up
结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 ...
- js 中导出excel 较长数字串会变成科学计数法(转载)
在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串转换成 科学计数法.现在网上找到解决方案之一: (在数字串 ...
- 160606、springmvc中使用Spring Mobile
springmobile特点: 1.客户端设备识别:识别结果只有3种类型:NORMAL(非手机设备).MOBILE(手机设备).TABLET(平板电脑). 2.网站偏好设置:Spring 通过设备识别 ...
- IT资料常用网址汇总
菜鸟联盟: http://www.runoob.com/ w3c school :https://www.w3cschool.cn/ http://www.w3school.com.cn/w3c/in ...
- SignalR循序渐进(二)泛型Hub
接上一篇,文章末尾抛出了2个问题: 能不能让客户端声明一个强类型的方法列表呢?这样首先不容易写错. 同样的,能不能让服务端声明一个强类型的方法列表给客户端调用呢? 如果要让客户端的方法以强类型出现在服 ...