[poj 3318] 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
Hint
Multiple inputs will be tested. So O(n^3) algorithm will get TLE.
注意:只需要判断是否一样
如果A*B=C 那么 A*(B*R)=C*R
用一个随机数组(R)当做矩阵然后再乘即可变为O(n^2)
code:
//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define ll long long
#define f(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
inline ll rd() {
ll x=0,fla=1; char c=' ';
while(c>'9'|| c<'0') {if(c=='-') fla=-fla; c=getchar();}
while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
return x*fla;
}
const int MAX=1010;
const int INF=0x3f3f3f3f;
int n;
int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],ans1[MAX],ans2[MAX],rnd[MAX];
void mul(int a[MAX],int b[MAX][MAX],int c[MAX]) {
int reg[MAX];
f(i,1,n) {
reg[i]=0;
f(j,1,n) reg[i]+=a[j]*b[j][i];
}
f(i,1,n) c[i]=reg[i];
}
bool jud() {
f(i,1,n) if(ans1[i]!=ans2[i]) return 0;
return 1;
}
int main() {
f(i,1,MAX) rnd[i]=rand();
while(scanf("%d",&n)==1) {
f(i,1,n) f(j,1,n) a[i][j]=rd();
f(i,1,n) f(j,1,n) b[i][j]=rd();
f(i,1,n) f(j,1,n) c[i][j]=rd();
mul(rnd,a,ans1);mul(ans1,b,ans1);mul(rnd,c,ans2);
if(jud()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
[poj 3318] Matrix Multiplication (随机化+矩阵)的更多相关文章
- poj 3318 Matrix Multiplication 随机化算法
方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...
- 数学(矩阵乘法,随机化算法):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: ...
- PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...
- POJ 3318 Matrix Multiplication(矩阵乘法)
题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...
- POJ 3318 - Matrix Multiplication 第一次用随机化解决问题...
随机化还是很厉害的...印象最深的是以前手写快排~~一般加个随机化会使耗时不受输入数据的..时间更加稳定 这个题是人品题了...开始交了好多遍都过不了..多交几次终于过了... Program: #i ...
- 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 ...
- hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场
Matrix multiplication Time ...
随机推荐
- 【ACM】poj_1000_A+B_201307271012
A+B ProblemTime Limit: 1000MS Memory Limit: 10000K Total Submissions: 296408 Accepted: 162241 Desc ...
- Spring Boot 配置Oracle数据库
1.添加oralce 依赖包,仓库没有则通过maven装载到本地仓库: 2.application.properties 中添加配置,特别是第一个配置项要严重注意! spring.jpa.databa ...
- HDU 3987 && DINIC
很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样: 求一次网络流最小割后,把满流的边置1,不满流的置INF.再求一次最大流即可. 为什么呢? 是否会存在一些边当前不满流 ...
- Tomcat扩展——监控
(转过来,源地址:http://www.jmatrix.org/notes/1067.html) 近期心血来潮.想能否够通过添加一个tomcat的扩展,来持续收集tomcatserver本身的性能信息 ...
- mysql查询count
Every derived table must have its own alias 每个派生出来的表都必须有一个自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时候子查询出来的的 ...
- 在ubuntu中安装与配置zsh与oh-my-zsh
先补充点东西 1.ubuntu中默认安装了那些shell jiang@Linux:~$ cat /etc/shells # /etc/shells: valid login shells/bin/sh ...
- linux下图片转换工具[【转】
本文转载自:https://linux.cn/article-8014-1.html 计算机术语中,批处理指的是用一个非交互式的程序来执行一序列的任务的方法.这篇教程里,我们会使用 Linux 命令行 ...
- P1127 词链
P1127 词链 题目描述 如果单词X的末字母与单词Y的首字母相同,则X与Y可以相连成X.Y.(注意:X.Y之间是英文的句号“.”).例如,单词dog与单词gopher,则dog与gopher可以相连 ...
- 部署微信定位精灵APK到Genymotion
- 转载:SoapUI之接口数据传递
SoapUI之接口数据传递(TestCase.TestSuite传递) SoapUI之接口数据传递(step传递) SoapUI+Groovy做接口自动化测试 SoapUI中使用Conditional ...