POJ 3318:Matrix Multiplication(随机算法)

http://poj.org/problem?id=3318

题意:问A和B两个矩阵相乘能否等于C。

思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的。

这里的随机算法指的是随机枚举矩阵C的一个位置,然后通过A*B计算是否能够得到矩阵C相应位置的数,如果不等,就直接退出了,如果跑过一定的数量后能够相等,那么就可以判断这个矩阵C等于A*B的。第一次见这样的题目。。。有点新奇。

暴力算法:

 1 #include <cstdio>
 2 using namespace std;
 3 int a[505][505], b[505][505], c[505][505];
 4
 5 int read() {
 6     int num = 0, f = 0;
 7     char c;
 8     while((c = getchar()) == ‘ ‘ || c == ‘\n‘) ;
 9     if(c == ‘-‘) f = 1;
10     else num = c - ‘0‘;
11     while((c = getchar()) >= ‘0‘ && c <= ‘9‘) num = num * 10 + c - ‘0‘;
12     if(f) return -num;
13     else return num;
14 }
15
16 void solve(int n) {
17     for(int i = 1; i <= n; i++) {
18         for(int j = 1; j <= n; j++) {
19             int num = 0;
20             for(int k = 1; k <= n; k++)
21                 num += a[i][k] * b[k][j];
22             if(num != c[i][j]) { puts("NO"); return ; }
23         }
24     }
25     puts("YES");
26 }
27
28 int main() {
29     int n;
30     while(~scanf("%d", &n)) {
31         for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[i][j] = read();
32         for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) b[i][j] = read();
33         for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) c[i][j] = read();
34         solve(n);
35     }
36     return 0;
37 }

随机算法(试了好多次才对):

 1 #include <cstdio>
 2 #include <ctime>
 3 #include <cstdlib>
 4 using namespace std;
 5 int a[505][505], b[505][505], c[505][505];
 6
 7 int main() {
 8     srand((unsigned)time(NULL));
 9     int n;
10     while(~scanf("%d", &n)) {
11         for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
12         for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &b[i][j]);
13         for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &c[i][j]);
14         bool f = 0;
15         for(int t = 0; t < 60000; t++) {
16             int row = rand() % n + 1;
17             int col = rand() % n + 1;
18             int num = 0;
19             for(int i = 1; i <= n; i++)
20                 num = num + a[row][i] * b[i][col];
21             if(num != c[row][col]) { f = 1; break; }
22         }
23         if(!f) puts("YES");
24         else puts("NO");
25     }
26     return 0;
27 }
时间: 2024-12-18 10:14:22

POJ 3318:Matrix Multiplication(随机算法)的相关文章

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 e

[ACM] POJ 3318 Matrix Multiplication (随机化算法)

Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16118   Accepted: 3485 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 posit

POJ 3318 Matrix Multiplication(矩阵乘法)

题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. 1 //3318 2 #include <stdio.h> 3 #include <string.h> 4 #include <time.h> 5 #include <stdlib.h> 6 #include <iostream> 7 8 using nam

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];

POJ 题目3318 Matrix Multiplication(快速判断矩阵乘是否正确)

Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17132   Accepted: 3732 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 posit

随机算法 poj 2576 Tug of War

Tug of War Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8187   Accepted: 2204 Description A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must b

2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)

题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据是不可能会有的,而我们一开始一直在想极端数据能接受的方法......后来看了鹏哥的做法,就是把是0的地方都跳过就可以了,用矩阵保存前一个非0数的位置是多少.二师兄给我看了一个代码,人家根本没用别的优化,直接将最里层k的循环提到了最外层,然后就AC了,对此我表示无语. 1 #include <cstd

hdu4920 Matrix multiplication 模3矩阵乘法

hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 568    Accepted Submission(s): 225 Problem Description Given two matrices A and B of size n×n, find the product o

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640