POJ 3318:Matrix Multiplication(随机算法)
http://poj.org/problem?id=3318
题意:问A和B两个矩阵相乘能否等于C。
思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的。
这里的随机算法指的是随机枚举矩阵C的一个位置,然后通过A*B计算是否能够得到矩阵C相应位置的数,如果不等,就直接退出了,如果跑过一定的数量后能够相等,那么就可以判断这个矩阵C等于A*B的。第一次见这样的题目。。。有点新奇。
暴力算法:
#include <cstdio>
using namespace std;
int a[][], b[][], c[][]; int read() {
int num = , f = ;
char c;
while((c = getchar()) == ' ' || c == '\n') ;
if(c == '-') f = ;
else num = c - '';
while((c = getchar()) >= '' && c <= '') num = num * + c - '';
if(f) return -num;
else return num;
} void solve(int n) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
int num = ;
for(int k = ; k <= n; k++)
num += a[i][k] * b[k][j];
if(num != c[i][j]) { puts("NO"); return ; }
}
}
puts("YES");
} int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = ; i <= n; i++) for(int j = ; j <= n; j++) a[i][j] = read();
for(int i = ; i <= n; i++) for(int j = ; j <= n; j++) b[i][j] = read();
for(int i = ; i <= n; i++) for(int j = ; j <= n; j++) c[i][j] = read();
solve(n);
}
return ;
}
随机算法(试了好多次才对):
#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;
int a[][], b[][], c[][]; int main() {
srand((unsigned)time(NULL));
int n;
while(~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]);
bool f = ;
for(int t = ; t < ; t++) {
int row = rand() % n + ;
int col = rand() % n + ;
int num = ;
for(int i = ; i <= n; i++)
num = num + a[row][i] * b[i][col];
if(num != c[row][col]) { f = ; break; }
}
if(!f) puts("YES");
else puts("NO");
}
return ;
}
POJ 3318:Matrix Multiplication(随机算法)的更多相关文章
- POJ 3318 Matrix Multiplication(随机算法)
题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...
- 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: 17783 Accepted: ...
- Poj 3318 Matrix Multiplication( 矩阵压缩)
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18928 Accepted: ...
- PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...
- poj 3318 Matrix Multiplication
http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C #include <cstdio> #include <cstring> #incl ...
- [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 第一次用随机化解决问题...
随机化还是很厉害的...印象最深的是以前手写快排~~一般加个随机化会使耗时不受输入数据的..时间更加稳定 这个题是人品题了...开始交了好多遍都过不了..多交几次终于过了... Program: #i ...
- PKU 3318 Matrix Multiplication(神奇的输入)
#include<cstdio> using namespace std; ][]; ][],C[][]; int Read() { ; ; while((ch=getchar())==' ...
随机推荐
- style原则问题
就近原则 1.“行内”覆盖“嵌入”,“嵌入”覆盖“外部”Style.xml <Window.Resources> <Grid.Resources> ….中间层 <Butt ...
- x:Static
用途:访问代码中的变量等 后台定义一个变量 public partial class GetStaticFromBackgroundCode : Window { public static stri ...
- InnoSetup提升系统管理员权限(通过破解方式修改?)
PrivilegesRequired=admin 1 2 3 4 5 找到```INNO```安装目录下的```SetupLdr.e32```文件(其实就是一个exe程序),将程序中的```Man ...
- CentOS 7使用yum快速搭建LAMP环境
1.安装Apache [root@localhost ~]# yum -y install httpd # 开机自启动 [root@localhost ~]# chkconfig httpd on # ...
- Android零基础入门第1节:Android的前世今生
原文:Android零基础入门第1节:Android的前世今生 现在网上有很多各色Android资料了,但相对来说还是比较零散,Android覆盖的范围极广,最近刚好有机会全部拉通整理一遍,也保存起来 ...
- Python中实现switch分支结构
Python不像C/C++,Java等有switch-case的语法.不过其这个功能,比如用Dictionary以及lambda匿名函数特性来替代实现. 实现方法分为两步: 首先,定义一个字典: 其次 ...
- UWP入门(四)--设置控件样式
原文:UWP入门(四)--设置控件样式 官方定义:可以使用 XAML 框架通过多种方式自定义应用的外观. 通过样式可以设置控件属性,并重复使用这些设置,以便保持多个控件具有一致的外观. 可分享至不同e ...
- spring.net的简单使用(二)资源配置
主要对资源配置做进一步的解析. 对资源位置的配置是在spring节点的context下,resource节点配置. spring.net的资源是可以设置在三种不同的位置的, 1.配置文件中 <r ...
- MySQL操作详解
创建并使用数据库 查看服务器上的数据库:SHOW DATABASES; 创建数据库:CREATE DATABASE <数据库名>; 指明使用何数据库:USE <数据库名> 创建 ...
- Uncaught (in promise)
Uncaught (in promise) 使用es6的promise时候,有时候会出现如下错误: 这是因为,使用定义promise方法的时候,reject了,但是,在使用的地方没有用catch进行接 ...