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. [bzoj1552\bzoj2506][Cqoi2014]robotic sort 排序机械臂_非旋转Treap

    robotic sort 排序机械臂 bzoj-1552 bzoj-2506 Cqoi-2014 题目大意:给定一个序列,让你从1到n,每次将[1,p[i]]这段区间反转,p[i]表示整个物品权值第i ...

  2. POJ 3076

    DLX算法,刚接触,是关于精确覆盖的,白书上有算法介绍. 代码模板 #include <iostream> #include <cstdio> #include <cst ...

  3. 【网络协议】ICMP协议、Ping、Traceroute

        ICMP协议 ICMP常常被觉得是IP层的一个组成部分,它是网络层的一个协议.它传递差错报文以及其它须要注意的信息.ICMP报文通常被IP层或更高层(TCP.UDP等)使用,它是在IP数据报内 ...

  4. ant整合junit自己主动化測试

    一. 使用Junit进行測试 1. Java业务代码: public class HelloWorld { // 測试返回"world" public String hello() ...

  5. 基于spark1.4的Spark-Sql

    Author: kwu 基于spark1.4的Spark-Sql,spark1.4.1在7月15刚公布.提供较好sql支持 1.怎样启动Spark-Sql 启动脚本例如以下 #!/usr/bin/en ...

  6. int、bigint、smallint 和 tinyint范围

    int.bigint.smallint 和 tinyint范围使用整数数据的精确数字数据类型.bigint从 -2^63 (-9223372036854775808) 到 2^63-1 (922337 ...

  7. Element UI Form 每行显示多列,即多个 el-form-item

    Element UI Form组件使用问题. 每个 el-form-item 都会独占一行. 对于输入项很多的管理app, 能否在每个form中, 每行显示 2 个或者多个 el-form-item ...

  8. ijkplayer详解AAA

    https://www.jianshu.com/p/c5d972ab0309 https://www.android-arsenal.com/details/1/530 https://stackov ...

  9. [JavaEE] DWR框架简介

    DWR框架简介 DWR框架是一个可以允许你去创建AJAX WEB站点的JAVA开源库.它可以让你在浏览器的JavaScript代码中调用Web服务器的Java代码,就像Java代码在浏览器中一样.DW ...

  10. eclipse中文汉字看不清或过小的问题解决方法!!

    把字体修改为 中欧字体就可以看清汉字