Poj 3318 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
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( 矩阵压缩)的更多相关文章
- POJ 3318 Matrix Multiplication(矩阵乘法)
题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...
- 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17783 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
http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C #include <cstdio> #include <cstring> #incl ...
- poj 3318 Matrix Multiplication 随机化算法
方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...
- POJ 3318 Matrix Multiplication(随机算法)
题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...
- POJ 3318 - Matrix Multiplication 第一次用随机化解决问题...
随机化还是很厉害的...印象最深的是以前手写快排~~一般加个随机化会使耗时不受输入数据的..时间更加稳定 这个题是人品题了...开始交了好多遍都过不了..多交几次终于过了... Program: #i ...
- PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...
- HDU 4920 Matrix multiplication(矩阵相乘)
各种TEL,233啊.没想到是处理掉0的情况就能够过啊.一直以为会有极端数据.没想到居然是这种啊..在网上看到了一个AC的奇妙的代码,经典的矩阵乘法,仅仅只是把最内层的枚举,移到外面就过了啊...有点 ...
随机推荐
- configparser模块来生成和修改配置文件
1. 安装configparser模块 pip3 install configparser ##python2.7模块名为ConfigParser 2. 创建配置文件 import configpar ...
- SpringSource Tool Suite (STS)无法启动问题
修改STS.ini,指定一个JRE路径: -vmD:\Program\Java\jdk1.7.0_79\bin\javaw.exe-startupplugins/org.eclipse.equinox ...
- jquery阻止事件冒泡的方法
$("table tbody").click(function(e) { e.preventDefault(); //阻止自身的事件,并不能阻止冒泡 e.stopPropagati ...
- Heartbeats
很少有人否定,这是一首天籁.凄美动听的声音,触动的,是真正的灵魂深处.所谓“仁者见仁智者见智”,但有些东西是共通的,比如,我们的内心会被同一样东西触动.在这首动听的歌曲中,体现出那男女之间平凡又伟大的 ...
- Docker---大型项目容器化改造
虚拟化和容器化是项目云化不可避免的两个问题.虚拟化由于是纯平台操作,一个运行于linux操作系统的项目几乎不需要做任何改造就可以支持虚拟化.而项目如果要支持容器化则需要做许多细致的改造工 ...
- Java-GC垃圾收集器
1. Serial young generation “单线程”收集器,是指进行垃圾收集时,必须暂停其他所有工作线程,直到收集结束.是历史最悠久的收集器. 2. Serial Old tenured ...
- 屏幕变黑白-winhotkey
下载了个windows hot key 的工具想看快捷键冲突 结果安装好之后屏幕变黑白了,变成辅助模式了.而且鼠标移动到哪都加蓝色框框 如果这个时候你带上耳机就能听到在朗读,这应该也是一种辅助模式 ...
- IDEA 安装mybatis 插件 可以通过mapper定位到xml
在使用IDEA的时候 ,通过mapper类定位到 xml文件是让人很头疼的问题! 无意中发现了这个插件!可以通过类方法直接定位到xml中!比较强大!哈哈! 这玩意好像是付费的! 不过不破解也能用! 哈 ...
- 【转载】解决window.showModalDialog 模态窗口中location 打开新窗口问题
来源: <http://bibipear.blog.sohu.com/143449988.html> 在我们的项目中,通常会用到showModalDialog 打开一个模态的子窗口,但是在 ...
- linux下安装sphinx
1.下载sphinx源码包 上面截图的这个网址 复制链接地址 在putty终端使用:wget http://sphinxsearch.com/files/sphinx-2.3.1-beta.t ...