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())==' ...
随机推荐
- Binding控件某个属性
<Grid Margin="60"> <Grid.RowDefinitions> <RowDefinition Height="*" ...
- AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享
原文:AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享 ====================www.ayjs.net 杨洋 wpfui.com ...
- https://www.jianshu.com/p/4da29fa310d2
wampserver显示红色.橙色的解决方案 拿笔的小鑫 关注 2016.10.15 14:38* 字数 2643 阅读 5083评论 0喜欢 3赞赏 1 </br></br&g ...
- FileHelper
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Win ...
- C# 多进程安全
多个应用程序同时写入数据到一个文件中时可用 public void WriteData(string dataWh, string filePath) { EventWaitHandle waitHa ...
- Win10《芒果TV》商店内测版更新至v3.1.6:率先支持Xbox One平台 - 参与反馈,赢取VIP奖励
芒果TV For Win10商店内测版 v3.1.6 于2016年9月1日星期四下午正式登陆商店 主要是优化手机版视频下载相关设置.策略.风险提示,并升级兼容目标,率先支持Xbox One平台,覆盖更 ...
- Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker
原文:Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker 在实际开发中,经常会遇见一些时间选择器.日期选择器.数字选择器等需求,那么从本期开始来学习And ...
- ORA-19625: error identifying file XXXXX
在RMAN备份全库的时候,将归档日志一同进行备份,结果报如下错误,可以看到是无法获得对应归档日志的报错: RMAN: ========================================= ...
- Decision Tree
Decision Tree builds classification or regression models in the form of a tree structure. It break d ...
- Topshelf结合Quartz.NET实现服务端定时调度任务
这周接受到一个新的需求:一天内分时间段定时轮询一个第三方WebAPI,并保存第三方WebAPI结果. 需求分析:分时段.定时开启.定时结束.轮询.主要工作集中在前三个上,轮询其实就是个Http请求,比 ...