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. configure: error: XML configuration could not be found

    运行: ./configure --prefix=/usr/local/php --enable-fastcgi --enable-fpm 之后出现 Running FastCGI Process M ...

  2. 利用Ajax调用controller方法并传递参数

    一.背景由于近期工作需要将人脸识别功能与选课系统结合,但是对前端知识了解的很少,只能边做边学了,因此在这边把遇到的一些坑说明一下,希望能帮助到像我一样的初学者 二.具体内容这里采用框架为MVC,如果想 ...

  3. Linux 服务具体解释

    acpid ACPI(全 称 Advanced Configuration and Power Interface)服务是电源管理接口. 建议全部的笔记本用户开启它. 一些server可能不须要 ac ...

  4. PyCharm 运行工程

  5. Android 怎样实现 焦点图的 无线循环滑动的状态?

    參考网址:http://my.oschina.net/xsk/blog/119167 总体的架构:ViewPgaer 中直接嵌套  IamgeView 方案一:  重写Viewpager 这样有局限性 ...

  6. 深入理解 C 指针阅读笔记 -- 第二章

    Chapter2.h #ifndef __CHAPTER_2_ #define __CHAPTER_2_ /*<深入理解C指针>学习笔记 -- 第二章*/ /* 内存泄露的两种形式 1.忘 ...

  7. 初识——Vim

    有些东西吧,总是碰见,低头不见抬头见,但又不知道是什么.用来干嘛的?总是搞的心里痒痒.所以一定要学习一下. 近期一段时间,总是碰到一个词儿:VIM,在这儿看到了,我不理他,隔一会儿丫的又跑我眼睛里,总 ...

  8. class com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text

    运行mapreduce遇到的错: Java.lang.ClassCastException: classcom.sun.jersey.core.impl.provider.entity.XMLJAXB ...

  9. The Euler function(hdoj --2824-欧拉函数)

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. Spring进行表单验证

    转自:https://www.tianmaying.com/tutorial/spring-form-validation 开发环境 IDE+Java环境(JDK 1.7或以上版本) Maven 3. ...