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 (随机化+矩阵)的更多相关文章

  1. poj 3318 Matrix Multiplication 随机化算法

    方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...

  2. 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17783   Accepted: ...

  3. Poj 3318 Matrix Multiplication( 矩阵压缩)

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18928   Accepted: ...

  4. PKU 3318 Matrix Multiplication(随机化算法||状态压缩)

    题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...

  5. POJ 3318 Matrix Multiplication(矩阵乘法)

    题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...

  6. POJ 3318 - Matrix Multiplication 第一次用随机化解决问题...

    随机化还是很厉害的...印象最深的是以前手写快排~~一般加个随机化会使耗时不受输入数据的..时间更加稳定 这个题是人品题了...开始交了好多遍都过不了..多交几次终于过了... Program: #i ...

  7. poj 3318 Matrix Multiplication

    http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C #include <cstdio> #include <cstring> #incl ...

  8. POJ 3318 Matrix Multiplication(随机算法)

    题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...

  9. hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

    Matrix multiplication                                                                           Time ...

随机推荐

  1. Centos6.4建立本地yum源

    https://jingyan.baidu.com/article/e9fb46e1aed8207521f7662c.html

  2. [bzoj2588][Spoj10628]Count on a tree_主席树

    Count on a tree bzoj-2588 Spoj-10628 题目大意:给定一棵n个点的树,m次查询.查询路径上k小值. 注释:$1\le n,m\le 10^5$. 想法:好像更博顺序有 ...

  3. Linux文件查找命令find(转)

    Linux find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件.并且将查找到的子目录 ...

  4. HDU 5355 Cake

    HDU 5355 Cake 更新后的代码: 今天又一次做这道题的时候想了非常多种思路 最后最终想出了自觉得完美的思路,结果却超时 真的是感觉自己没救了 最后加了记忆化搜索,AC了 好了先说下思路吧.不 ...

  5. python Flask JQuery使用说明

    0.前言     近期因为某种原因再次学习Flask框架.借助博客整理相关内容.Flask框架和Apache+PHP存在少许不同,Flask框架中JS和CSS文件存放于一个相对固定的位置. 普通情况下 ...

  6. mysql配置文件夹错误:在安装mysql 5.6.19 时运行cmake命令是出现CMake Error: The source directory does not appear to contai

    在安装mysql 5.5.xx 时运行cmake命令是出现CMake Error: The source directory does not appear to contain CMakeLists ...

  7. 报错configure:error: no acceptable C compiler found in $PATH。。

    报错configure:error: no acceptable C compiler found in $PATH.. 查看日志: 出错原因:新安装的linux系统,没有gcc库 解决方案:使用yu ...

  8. [ASPX] 模版引擎XTemplate与代码生成器XCoder(源码)

    模版引擎XTemplate是一个仿T4设计的引擎,功能上基本与T4一致(模版语法上完全兼容T4,模版头指令部分兼容). 自己设计模版引擎,就是为了代码生成器.网站模版.邮件模版等多种场合,也就是要能拿 ...

  9. mysql 强制修改密码

    mysql忘记密码时强制修改步骤如下: 1.用命令编辑配置文件/etc/my.cnf 2.添加一条语句使其变为不用密码就能进入的状态 skip-grant-tables 3.保存并退出,然后再命令行输 ...

  10. python之路——迭代器和生成器

    阅读目录 楔子 python中的for循环 可迭代协议 迭代器协议 为什么要有for循环 初识生成器 生成器函数 列表推导式和生成器表达式 本章小结 生成器相关的面试题 返回顶部 楔子 假如我现在有一 ...