数学(矩阵乘法,随机化算法):POJ 3318 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
#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的更多相关文章
- 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: 18928 Accepted: ...
- [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 ...
- POJ 3318 Matrix Multiplication(矩阵乘法)
题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...
- POJ 3318 Matrix Multiplication(随机算法)
题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...
- 线性代数(矩阵乘法):POJ 3233 Matrix Power Series
Matrix Power Series Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- 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 矩阵相乘 (随机化算法-舍伍德(Sherwood))
周三的算法课,主要讲了随机化算法,介绍了拉斯维加斯算法,简单的理解了为什么要用随机化算法,随机化算法有什么好处. 在处理8皇后问题的时候,穷举法是最费时的,回朔比穷举好点,而当数据量比较大的时候,如1 ...
随机推荐
- css 权威指南笔记( 五)结构和层叠
特殊性 重要性 !important; 继承 向上传播例外,应用到body元素的背景样式可以传递到html元素,相应对的可以定义其画布. 大多数框模型属性(包括外边距.内边距.背景.边框)都不能继承 ...
- 关于弹出层(iframe)时刷新页面的js
[javascript] view plaincopyprint? iframe弹出子页面刷新父页面iframe parent.location.reload(); [javascript] view ...
- Python之路【第十一篇】:CSS --暂无内容-待更新
Python之路[第十一篇]:CSS --暂无内容-待更新
- native跟volatile
native是告知编译器 该方法是其他语言实现的 比如C 呵呵 private native void CoutSea();没有方法实现部分的 volatile是Java语言的关键字,用在变量的声明中 ...
- JavaScript核心
JavaScript核心 arguments对象 Array对象 Boolean对象 Date对象 Error对象 Function对象 Global对象 Math对象 Number对象 Object ...
- jquery实现很简单的DIV拖动
今天用jquery实现了一个很简单的拖动...实现思路很简单 如下: 在thickbox 弹出层内实现拖拽DIV,那么得进行一下相对宽高的运算:必须加上相对于可见窗口宽高和弹出层宽高之间的差: ...
- log4Net配置详解
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- C#中Dictionary、ArrayList、Hashtable和Array的区别
IDictionary接口是所有字典类集合的基本接口,该接口与ICollection,IEnumerable接口是所有非泛型类集合的最基本的接口 IEnumerable接口用于公开枚举数,该枚举数支持 ...
- Tensor神经网络进行知识库推理
本文是我关于论文<Reasoning With Neural Tensor Networks for Knowledge Base Completion>的学习笔记. 一.算法简介 网络的 ...
- Linux 关于解压
1.*.tar 用 tar –xvf 解压 2.*.gz 用 gzip -d或者gunzip 解压 3.*.tar.gz和*.tgz 用 tar –xzf 解压 4.*.bz2 用 bzip2 -d或 ...