Matrix Multiplication
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18928   Accepted: 4074

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.

Source

 
/*
题意:有3个n*n的矩阵A,B,C,问AB是否等于C。
思路:题目描述很简单,就是用矩阵乘法,但是很明显矩阵乘法的时间复杂度为O(n^3),很明显超时。那怎么改进呢?就是用压缩矩阵的方法:
设矩阵R是1*n的矩阵,根据矩阵的性质,若R*A*B=R*C,那么A*B=C。由此可以看出来,虽然多成了一个矩阵,但是时间复杂度成了O(n^2)。那么问题是这个R的行列式该怎么设定,有人用的随机算法,但是随机算法可能在关键点上出现错误,可以将R设定成一个递增的数列{1,2,3……}。
*/
#include<iostream>
#include<vector>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,x;
int a[][],b[][],c[][];
bool work()
{
int R[],ra[],rab[],rc[];
for(int i=;i<=n;i++) {R[i]=i; ra[i]=; rab[i]=; rc[i]=;} for(int j=;j<=n;j++)
for(int i=;i<=n;i++)
ra[j]+=R[i]*a[i][j]; for(int j=;j<=n;j++)
for(int i=;i<=n;i++)
rab[j]+=ra[i]*b[i][j]; for(int j=;j<=n;j++)
for(int i=;i<=n;i++)
rc[j]+=R[i]*c[i][j]; for(int i=;i<=n;i++)
if (rab[i]!=rc[i]) return ;
return ;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&b[i][j]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&c[i][j]);
if (work()) printf("YES\n");
else printf("NO\n");
return ;
}

Poj 3318 Matrix Multiplication( 矩阵压缩)的更多相关文章

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

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

  2. 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17783   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

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

  5. poj 3318 Matrix Multiplication 随机化算法

    方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...

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

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

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

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

  8. PKU 3318 Matrix Multiplication(随机化算法||状态压缩)

    题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...

  9. HDU 4920 Matrix multiplication(矩阵相乘)

    各种TEL,233啊.没想到是处理掉0的情况就能够过啊.一直以为会有极端数据.没想到居然是这种啊..在网上看到了一个AC的奇妙的代码,经典的矩阵乘法,仅仅只是把最内层的枚举,移到外面就过了啊...有点 ...

随机推荐

  1. Zookeeper环境安装

    源码包下载: http://archive.apache.org/dist/zookeeper/zookeeper-3.4.10 集群环境: master 192.168.1.99 slave1 19 ...

  2. zookeeper 监听事件 PathChildrenCacheListener

    zookeeper 监听事件 PathChildrenCacheListener PathChildrenCacheListener一次父节点注册,监听每次子节点操作,不监听自身和查询. 1.测试类: ...

  3. 使用selenium前学习HTML(2)——标签

    <!-- HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. HTML 段落是通过 <p> 标签进行定义的. HTML 链接是 ...

  4. 基于session和token的身份认证方案

    一.基于session的身份认证方案 1.方案图示 2.比较通用的鉴权流程实现如下: 在整个流程中有两个拦截器. 第一个拦截器AuthInteceptor是为了每一次的请求的时候都先去session中 ...

  5. 浅谈boost.variant的几种访问方式

    前言 variant类型在C++14并没有加入,在cppreference网站上可以看到该类型将会在C++17加入,若想在不支持C++17的编译器上使用variant类型,我们可以通过boost的va ...

  6. BIOS/MBR UEFI/GPT关系与区别-资料整理

    ---恢复内容开始--- 关于 BIOS/MBR UEFI/GPT他们之间的关系一直比较疑惑, 首先一点前提 BIOS UEFI 是一类,是控制硬件,引导启动的:MBR GPT是硬盘的分区定义.. 后 ...

  7. 利用python获取nginx服务的ip以及流量统计信息

    #!/usr/bin/python #coding=utf8 log_file = "/usr/local/nginx/logs/access.log" with open(log ...

  8. nginx之proxy、cache、upstream模块学习

    nginx之proxy反向代理模块: location ^~ /proxy_path/ { root "/www/html"; 这里没必要配置 index index.html; ...

  9. APP开放接口API安全性——Token令牌Sign签名的设计与实现

    在APP开放接口API的设计中,避免不了的就是安全性问题. 一.https协议 对于一些敏感的API接口,需要使用https协议.https是在http超文本传输协议加入SSL层,它在网络间通信是加密 ...

  10. 【前端】jQuery选择器$()的实现原理

    今天三七互娱技术面试的时候面试官问了我这个问题,当时一脸懵逼,于是好好总结一下. 当我们使用jquery选择器的时候,$(s).回默认去执行jquery内部封装好的一个init的构造函数每次申明一个j ...