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

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(n3) algorithm will get TLE.
  用个一维随机矩阵去乘再判断是否相等,类似与哈希的思想。
 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int maxn=;
struct Array{
int a[maxn],L;
int *operator[](int x){
return &a[(x-)*L];
}
};
struct Matrix{
int R,C;
Array mat;
Matrix(){
memset(mat.a,,sizeof(mat.a));
R=C=;
}
void Init(int r,int c){
R=r;mat.L=C=c;
}
int *operator[](int x){
return mat[x];
}
friend Matrix operator*(Matrix a,Matrix b){
Matrix c;c.Init(a.R,b.C);
for(int i=;i<=a.R;i++)
for(int j=;j<=b.C;j++)
for(int k=;k<=a.C;k++)
c[i][j]+=a[i][k]*b[k][j];
return c;
}
friend bool operator ==(Matrix a,Matrix b){
if(a.R!=b.R||a.C!=b.C)return false;
for(int i=;i<=a.R;i++)
for(int j=;j<=b.C;j++)
if(a[i][j]!=b[i][j])
return false;
return true;
}
}A,B,C,D;
int main(){
int n,x;
scanf("%d",&n);srand(n);
A.Init(n,n);
B.Init(n,n);
C.Init(n,n);
D.Init(n,);
for(int i=;i<=n;i++)
D[i][]=rand();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&x);
A[i][j]=x;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&x);
B[i][j]=x;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&x);
C[i][j]=x;
}
}
A=A*(B*D);
C=C*D;
if(A==C)
printf("YES\n");
else
printf("NO\n");
return ;
}

数学(矩阵乘法,随机化算法):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: 18928   Accepted: ...

  3. [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 ...

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

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

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

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

  6. 线性代数(矩阵乘法):POJ 3233 Matrix Power Series

    Matrix Power Series   Description Given a n × n matrix A and a positive integer k, find the sum S = ...

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

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

  8. poj 3318 Matrix Multiplication

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

  9. POJ 矩阵相乘 (随机化算法-舍伍德(Sherwood))

    周三的算法课,主要讲了随机化算法,介绍了拉斯维加斯算法,简单的理解了为什么要用随机化算法,随机化算法有什么好处. 在处理8皇后问题的时候,穷举法是最费时的,回朔比穷举好点,而当数据量比较大的时候,如1 ...

随机推荐

  1. Linux squid 安装配置

    linux 代理软件 squid 查看是否安装squid   以上信息表明,本机是已经安装了此软件了 如果没有显示说明没有安装,则可以使用yum工具来安装   安装完软件后我们接着开始配置squid代 ...

  2. hdu 5073 Galaxy

    题意是给定n个点,让求找到一个点p使得sigma( (a[i] - p) ^ 2 ) 最小,其中a[i]表示第i个点的位置.其中有k个点不用算. 思路:发现这道题其实就是求n-k个点方差. 那么推一下 ...

  3. Android Camera开发:使用TextureView和SurfaceTexture预览Camera 基础拍照demo

    Google自Android4.0出了TextureView,为什么推出呢?就是为了弥补Surfaceview的不足,另外一方面也是为了平衡GlSurfaceView,当然这是本人揣度的.关于Text ...

  4. Android ListView 嵌套 ImageView,如何响应ImageView的点击和长按事件

    http://www.tuicool.com/articles/EZv2Uv 1.先说下嵌套在ListView中的ImageView如何响应点击事件 方法:在imageView中设置onClick属性 ...

  5. 工欲善其事必先利其器之Xcode高效插件和舒适配色

    功能强大的Xcode再配上高效的插件,必会让你的开发事半功倍.直接进入正题. Xcode插件安装方式: 1.github下载插件然后用xcode打开运行一遍,然后重启xcode. 2.安装插件管理Al ...

  6. 颜色rgb

    1.几种基本颜色的rgb 黑色:R.G.B(0.0.0) 白色:R.G.B(255.255.255) 红色:R.G.B(255.0.0) 绿色:R.G.B(0.255.0) 蓝色:R.G.B(0.0. ...

  7. Lua-C交互函数

    lua_gettable(lua_State * , tableIndex) //获取表的在key位置的值 过程:tableIndex为表在栈的位置,例:-2为第二个位置 , 此时会弹(出)栈作为参数 ...

  8. stat 的名字接口

    File::stat - stat 的名字接口 名字为:dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksiz ...

  9. sql server 数据分页显示。

    select [ID] ,[StockApplyCode] ,[RcCode] ,[LabCenterCode] ,[LabGroupCode] ,[LabGroupName] ,[Barcode] ...

  10. 响应式设计的5个CSS实用技巧

    正如我在教程响应式Web设计三步走当中所讲的,响应式的Web设计其实并不难,但是要让元素在布局切换时能够平滑过渡就比较考验技巧了.现在我分享在编码时常用的五个CSS技巧并举例说明.这些技巧都是使用简单 ...