数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication
2016-07-29 11:53
507 查看
Matrix Multiplication
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
Sample Output
Hint
Multiple inputs will be tested. So O(n3) algorithm will get TLE.
用个一维随机矩阵去乘再判断是否相等,类似与哈希的思想。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17783 | Accepted: 3845 |
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 <iostream> #include <cstring> #include <cstdlib> #include <cstdio> using namespace std; const int maxn=250010; struct Array{ int a[maxn],L; int *operator[](int x){ return &a[(x-1)*L]; } }; struct Matrix{ int R,C; Array mat; Matrix(){ memset(mat.a,0,sizeof(mat.a)); R=C=0; } void Init(int r,int c){ R=r;mat.L=C=c; } int *operator[](int x){ return mat[x]; } friend Matrix operator*(Matrix a,Matrix b){ Matrix c;c.Init(a.R,b.C); for(int i=1;i<=a.R;i++) for(int j=1;j<=b.C;j++) for(int k=1;k<=a.C;k++) c[i][j]+=a[i][k]*b[k][j]; return c; } friend bool operator ==(Matrix a,Matrix b){ if(a.R!=b.R||a.C!=b.C)return false; for(int i=1;i<=a.R;i++) for(int j=1;j<=b.C;j++) if(a[i][j]!=b[i][j]) return false; return true; } }A,B,C,D; int main(){ int n,x; scanf("%d",&n);srand(n); A.Init(n,n); B.Init(n,n); C.Init(n,n); D.Init(n,1); for(int i=1;i<=n;i++) D[i][1]=rand(); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&x); A[i][j]=x; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&x); B[i][j]=x; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&x); C[i][j]=x; } } A=A*(B*D); C=C*D; if(A==C) printf("YES\n"); else printf("NO\n"); return 0; }
相关文章推荐
- POJ 3318 思维 + 矩阵乘法 || 随机化算法
- POJ 3318 矩阵乘法 随机算法
- [随机化 矩阵乘法] BZOJ 2396 神奇的矩阵 & 51Nod 1140 矩阵相乘结果的判断 & POJ 3318 Matrix Multiplication
- POJ 3318 Matrix Multiplication(矩阵乘法)
- poj 3070(矩阵的乘法)
- 玲珑OJ 1109 Niro plays with snow [递推+预处理矩阵乘法]【数学】
- [WIKIOI 1250]Fibonacci数列(数论+数学+矩阵乘法+二分快速幂)
- Poj 3233 Matrix Power Series(矩阵乘法)
- POJ 3735 Training little cats(矩阵乘法)
- 【矩阵乘法入门】给数学不好的人的矩阵乘法模板教程
- POJ-3420 Quad Tiling 状态压缩+矩阵乘法
- BZOJ 2326 HNOI2011 数学作业 矩阵乘法
- poj 3070 Fibonacci + 矩阵乘法(矩阵快速幂)
- bzoj2326 数学作业 矩阵乘法
- POJ-3070 Fibonacci 矩阵乘法
- 【日常学习】【数学】【矩阵乘法】【大数乘方取模】codevs1982 加密算法题解
- ML之数学基础:矩阵乘法
- 矩阵乘法专题4——bzoj 2326 [HNOI2011] 数学作业 题解
- POJ 3233 - 矩阵乘法及其性质和优化
- poj 3233 矩阵乘法(分块矩阵)