POJ 3318 Matrix Multiplication(隨機化演算法)
給你三個矩陣A,B,C。讓你判斷A*B是否等於C。
隨機一組資料,然後判斷乘以A,B之後是否與乘C之後相等。
很扯淡的啊,感覺這種演算法不嚴謹啊、、、
Matrix Multiplication
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16255 | Accepted: 3515 |
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.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <time.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define min( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define Mod 1000000007
#define LL long long
using namespace std;
const int maxn = 510;
int a[maxn][maxn];
int b[maxn][maxn];
int c[maxn][maxn];
int f[maxn];
int aa[maxn];
int bb[maxn];
int cc[maxn];
int main()
{
int n;
cin >>n;
memset(aa, 0, sizeof(aa));
memset(bb, 0, sizeof(bb));
memset(cc, 0, sizeof(cc));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) scanf("%d",&a[i][j]);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) scanf("%d",&b[i][j]);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) scanf("%d",&c[i][j]);
srand((unsigned int)time(0));
for(int i = 0; i < n; i++)
f[i] = rand()%100;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
bb[i] += b[i][j]*f[j];
cc[i] += c[i][j]*f[j];
}
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) aa[i] += a[i][j]*bb[j];
int flag = 0;
for(int i = 0; i < n; i++)
{
if(aa[i] != cc[i])
{
flag = 1;
break;
}
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
相關文章
- 「演算法」貪心與隨機化演算法隨機
- 隨機演算法隨機演算法
- 隨機化演算法(5) — 蒙特卡羅(Monte Carlo)演算法隨機演算法
- 隨機森林演算法隨機森林演算法
- 【隨機演算法】洗牌隨機演算法
- [6 kyu] Multiplication table
- CTF中的最佳化隨機演算法(爬山&退火)隨機演算法
- 隨機森林演算法梳理隨機森林演算法
- javaScript隨機排序演算法JavaScript隨機排序演算法
- POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)矩陣
- 演算法隨筆——DP最佳化演算法
- CUDA 的隨機數演算法 API隨機演算法API
- 隨機森林演算法深入淺出隨機森林演算法
- (DP) CF1861D Sorting By Multiplication
- 隨機化處理技術隨機
- MAC地址隨機化介紹Mac隨機
- 演算法練習--LeetCode--54. Spiral Matrix 100%演算法LeetCode
- Matrix
- 弱隨機化種子漏洞科普隨機
- 風場視覺化:隨機重置視覺化隨機
- 隨機森林演算法原理與Python實現隨機森林演算法Python
- 機器學習演算法系列(十八)-隨機森林演算法(Random Forest Algorithm)機器學習演算法隨機森林randomRESTGo
- 向量(高階的隨機化應用)隨機
- Bagging與隨機森林(RF)演算法原理總結隨機森林演算法
- Making the Grade POJ - 3666(離散化+dp)
- Matrix Computations
- Matrix Distance
- Matrix Operations
- 演算法隨筆——主席樹(可持久化線段樹)演算法持久化
- python生成隨機數、隨機字串Python隨機字串
- 實現陣列的隨機排序(含洗牌演算法)陣列隨機排序演算法
- 基於量子隨機遊走的影像加密演算法隨機加密演算法
- pytorch---之隨機種子初始化PyTorch隨機
- 瞭解 “MAC 地址隨機化” 刻不容緩Mac隨機
- 通俗易懂--決策樹演算法、隨機森林演算法講解(演算法+案例)演算法隨機森林
- POJ3268 Silver Cow Party【Dijkstra演算法+思維】演算法
- POJ 1330 LCA最近公共祖先 離線tarjan演算法演算法
- 用Python寫演算法 | 蓄水池演算法實現隨機抽樣Python演算法隨機
- 演算法隨筆——manacher演算法