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(随机算法)的更多相关文章

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

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

  2. poj 3318 Matrix Multiplication 随机化算法

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

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

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17783   Accepted: ...

  4. Poj 3318 Matrix Multiplication( 矩阵压缩)

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

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

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

  6. poj 3318 Matrix Multiplication

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

  7. [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 ...

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

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

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

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

  10. PKU 3318 Matrix Multiplication(神奇的输入)

    #include<cstdio> using namespace std; ][]; ][],C[][]; int Read() { ; ; while((ch=getchar())==' ...

随机推荐

  1. OpenSSL RSA加解密 (.Net公钥加密/ Linux端私钥解密)

    要求在.Net端生成公钥私钥对. 然后在.Net端使用RSA公钥加密:在Linux端使用RSA私钥解密. 最初的尝试是:.Net端使用RSACryptoServiceProvider; linux端使 ...

  2. jquery开关灯

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  3. jquery ready和window onload区别

    window onload是指标签加载完成,并且标签资源加载完成: jquery ready是指标签加载完成,标签资源可能未加载完成 $(document).ready(function(){});= ...

  4. jvm常用参数设置 专题

    在jdk8中 -Xms2g不合法,能通过的:-Xms2G #!/bin/bash JAVA_OPTS="-Xms4G -Xmx4G -XX:+HeapDumpOnOutOfMemoryErr ...

  5. facebook javascript api 使用

    官方api文档:http://developers.facebook.com/docs 先简单的介绍下创建一个app(https://developers.facebook.com/apps),

  6. mfc开发an unsupported operation was attempted错误解决

    mfc开发删除了一个控件后,没有删除该控件对应的id和代码导致 觉得mfc真xx 在资源编辑可视化界面手动删除一个控件后,resource.h里该控件的ID竟然还存在 因为该id还存在,调用该控件的代 ...

  7. Windows 10 版本信息

    原文 https://technet.microsoft.com/zh-cn/windows/release-info Windows 10 版本信息 Microsoft 已更新其服务模型. 半年频道 ...

  8. 笔记:认识.NET平台

    认识.NET平台先了解一堆技术术语和缩写 http://www.cnblogs.com/dbycl/p/6419456.html 天生不跨平台的.NET Framework 2.大家都来开发新语言 3 ...

  9. linux输出信息调试信息重定向

    最近在做一个android系统移植的项目,所使用的开发板com1是调试串口,就是说会有uboot和kernel的调试信息打印在com1上(ttySAC0).因为后期要使用ttySAC0作为上层应用通信 ...

  10. 该内存不能read 或written数值 叙述(居然还有具体的讲究)

    该内存不能read 或written数值 叙述 0 0x0000 作业完成. 1 0x0001 不正确的函数. 2 0x0002 系统找不到指定的档案. 3 0x0003 系统找不到指定的路径. 4 ...