数学(矩阵乘法,随机化算法):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 ...
随机推荐
- php 两个数组是否相同,并且输出全面的数据,相同的加一个字段标示
方法一: $date是数组,数组中有字段id,name; $data1是数组,数组中有字段sort_id,name; 所以要通过$date[$i]['id']==$data1[$j]['sort_id ...
- 9.27 noip模拟试题
工资 (money/money.in/money.out) 时限1000ms 内存256MB 聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi.有m个结算工钱的时间,聪哥可 ...
- SQL存储过程基于字段名传入的字符串拼接.
--定义存储过程. Create PROCEDURE Usp_Static ), ), --分组字段. ), --统计字段. ), --表头字段. ) --聚会函数. AS ) --存储游标执行的列. ...
- ASP生成新会员编号
Function MakeUserCode OpenDB() Randomize dim getid_rs,getid set getid_rs=rsobj do while true getid=^ ...
- 06MySQL数据库入门
1.数据库的概念 数据库是保存数据的仓库,可以方便的把数据放进去,并且把数据根据各种需求取出来. 数据库管理系统(Database Management System,DBMS)是对数据库进行管理(增 ...
- SGU 175.Encoding
Solution: 简单题. 答案初始化为1. 从给定的n,q往下推出新的n和q,如果q是在右半边,答案加上 n-n/2. 一直到推到n==1. code: #include <iostream ...
- 做了一个类似天猫鼠标经过icon的动画,记录一下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Ubuntu14.04安装AMD显卡驱动双屏显示器完全解决方案
网上有很多方法,但是针对AMD显卡的方案不多,所以笔者今天想写一篇关于AMD显卡的教程. 首先,进入这个网址下载一些东东:http://support.amd.com/zh-cn/download/d ...
- Unity3D动态加载外部资源
最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质上我理解没有什么区别.Resources ...
- PHP常用代码:
1.$array=explode(separator,$string); //字符串->数组 2.$string=implode(glue,$array);//数组->字符串 3.file ...