【补题】 POJ 3318 随机化

题目链接:http://poj.org/problem?id=3318

题意:给你矩阵ABC,要求验证A * B是否等于C。

思路:直接算的话复杂度n^3,妥妥TLE。

随机化有两种方法。

一是不断随机一个列矩阵,左乘验证。

二是不断随机C里的某个点验证。

补充一下随机函数知识点:

要取得[a,b)的随机整数,使用(rand() % (b-a))+ a (结果值含a不含b)。
要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a (结果值含a和b)。
要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1 (结果值不含a含b)。

(总的来说,通用公式:a + rand() % n ;其中的a是起始值,n是整数的范围)

要取得a到b之间的随机整数,另一种表示:a + (int)b * rand() / (RAND_MAX + 1)。

要取得0~1之间的浮点数,可以使用rand() / double(RAND_MAX)。

其实更简单的是不随机,直接找一个普通的列矩阵左乘就行了。

而且这题数据真的超级水。

有个小tips:

这题如果用随机化的话,C++能过,G++过不了。(涉及精度的POJ上的题也这样)

如果用下面这代码的话,G++能过。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<vector>
 7 #include<set>
 8 #include<string>
 9 #include<sstream>
10 #include<cctype>
11 #include<map>
12 #include<stack>
13 #include<queue>
14 using namespace std;
15 #define INF 0x3f3f3f3f
16 typedef long long ll;
17 int gcd(int a, int b){return b==0?a:gcd(b,a%b);}
18
19 const int maxn = 510;
20 int a[maxn][maxn];
21 int b[maxn][maxn];
22 int c[maxn][maxn];
23 int x[maxn];
24 int xa[maxn];
25 int xab[maxn];
26 int xc[maxn];
27 int n;
28
29 void input(int d[maxn][maxn])
30 {
31     for(int i = 1; i <= n; i++)
32         for(int j = 1; j <= n; j++)
33             scanf("%d", &d[i][j]);
34 }
35
36 bool judge()
37 {
38     for(int j = 1; j <= n; j++)
39     {
40         for(int i = 1; i <= n; i++)
41         {
42             xa[j] += x[i] * a[i][j];
43             xc[j] += x[i] * c[i][j];
44         }
45     }
46     for(int j = 1; j <= n; j++)
47         for(int i = 1; i <= n; i++)
48             xab[j] += xa[i] * b[i][j];
49     for(int i = 1; i <= n; i++)
50         if(xab[i] != xc[i])
51             return false;
52     return true;
53 }
54
55 int main()
56 {
57 //    freopen("input.txt", "r", stdin);
58 //    freopen("output.txt", "w", stdout);
59     scanf("%d", &n);
60     input(a);
61     input(b);
62     input(c);
63     memset(xa, 0, sizeof(xa));
64     memset(xab, 0, sizeof(xab));
65     memset(xc, 0, sizeof(xc));
66     for(int i = 1; i <= n; i++)
67         x[i] = i;
68     if(judge())    cout << "YES" << endl;
69     else    cout << "NO" << endl;
70     return 0;
71 }

时间: 2024-07-30 10:11:00

【补题】 POJ 3318 随机化的相关文章

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

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 2503

1 #include <cstdio> 2 #include <cstring> 3 4 char en[11],fr[11]; 5 int st; 6 struct Tire{ 7 int next[26]; 8 char eng[11]; 9 }node[200005]; 10 void insert(char *s,int cur) 11 { 12 if(*s){ 13 if(!node[cur].next[*s-'a']) 14 node[cur].next[*s-'a']

4.30-5.1cf补题

//yy:拒绝转载!!! 悄悄告诉你,做题累了,去打两把斗地主就能恢复了喔~~~ //yy:可是我不会斗地主吖("'▽'") ~~~那就听两遍小苹果嘛~~~ 五一假期除了花时间建模,就抽空把最近没做的CF题补了点..毕竟明天开始又要继续上好多课呐...Yes, I can!(? •_•)?……(I can Huá shuǐ~~) codeforces 803 A. Maximal Binary Matrix   [简单构造] 题意:n行和n列填充零矩阵. 您要将k个1放在其中,使得得到

[2015hdu多校联赛补题]hdu5371 Hotaru&#39;s problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-sequence,现在给你一个数字串,问你这个串中最长的N-sequence子串长度 解:可以想到A-A是一个回文串,-AA也是一个回文串,那么首先Manacher跑一遍求出所有回文子串 可以想到任意两个互相覆盖的回文子串都可以表示成N-sequence 然后有三种搞法: 1.时间复杂度O(N*logN

补题 留空

这两道题虽然不是很难,但是目前还不会,在这留个地,省赛之后多刷点这种类型的题,再补上. 1. http://acm.hdu.edu.cn/showproblem.php?pid=3306 矩阵快速幂的题,相加平方和. 2.http://acm.hdu.edu.cn/showproblem.php?pid=3308 线段树 补题 留空,码迷,mamicode.com

[2015hdu多校联赛补题]hdu5348 MZL&#39;s endless loop

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1,如果无解输出-1 解:考虑奇数度的点一定会成对出现(因为所有度数和肯定是偶数个->因为一条边产生两度~),那么我们可以将奇数度的点两两一连消除掉(两奇数度点的出度入读差的绝对值都为1, 路径上的点的差绝对值为0) 然后偶数度的点可以成环,那么可以搜出所有的环 1 /* 2 * Problem: 3

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