POJ 3150 & BZOJ 2510 带有轮换的矩阵乘法

怎么样去解决带有轮换的乘法?

POJ 3150 & BZOJ 2510 带有轮换的矩阵乘法

题目描述

POJ 3150

细胞自动机。给你四个参数 $n, m, d, k$ ,表示对于一个 $A = (A_1, A_2, …, A_n)$,生成 $A’ = (A’_1, A’_2, …, A’_n)$ :

$$A’i = \sum {j} ^ {|i-j| \leq d} (A_j)$$

例如,对于 $n = 5, m = 3, d = 1, k = 1$ ,有下图:

BZOJ 2510

每次操作等概率取出一个球(即取出每个球的概率均为$\frac {1} {m}$),若这个球标号为 $k$($k < N$),则将它重新标号为$k + 1$;若这个球标号为 $N$,则将其重标号为 $1$。求每个标号的球的期望个数。

题解

这两道题有一个共性:都有轮换性

POJ 3150

假设 $n = 7, d = 2$,构造矩阵

根据题意,我们这样构造矩阵:$M_{ij} = {1 | |i-j| \leq d}$

最后的答案是 $A^{(k)} = A \ M^k$

通过观察,发现每一行具有轮换的特性。

直接计算出矩阵,然后做矩阵乘法一定会超时的。

BZOJ 2510

我们发现,对于每个球,有 $\frac 1 m$ 的可能性会转移成下一个球,而剩下 $1 - \frac 1 m$ 的可能性不变。

对于 $m = 4​$ 构造矩阵 (这里故意不把 $m​$ 带入矩阵)

根据题意,我们这样构造矩阵:

答案是 $A ^ {(k)} = A \ M^k$

针对轮换矩阵的解法

由于矩阵每一行具有轮换性,我们计算出第 $1$ 行的所有元素以后,即可代表这个矩阵。

当我们做 $M^k$ 的时候,传统的方法计算 $M^k$ 是利用快速幂,复杂度 $O(n^3 \ \log k)$

我们现在利用第 $1$ 行来代替这个矩阵进行计算:

现在假设 ,则

我们发现,轮换矩阵的乘法得到的矩阵仍然具有轮换性

通过计算我们发现,

这样子的复杂度为 $O(n^2 \log k)$ (因为我们每次只计算第一行,剩下行就是复制的)

AC Code

POJ 3150

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#define NOSTDCPP
//#define Cpp11
//#define Linux_System
#ifndef NOSTDCPP
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <cstdio>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#endif
# ifdef Linux_System
# define getchar getchar_unlocked
# define putchar putchar_unlocked
# endif
# define RESET(_) memset(_, 0, sizeof(_))
# define RESET_(_, val) memset(_, val, sizeof(_))
# define fi first
# define se second
# define pb push_back
# define midf(x, y) ((x + y) >> 1)
# define DXA(_) ((_ << 1))
# define DXB(_) ((_ << 1) | 1)
# define begin __Chtholly__
# define x1 __Mercury__
# define y1 __bbtl04__
# define index __ikooo__
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;
typedef long double ld;
const int MOD = 1e9 + 7;
const int maxn = 300009;
const int maxm = 300009;
const ll inf = 1e18;
const double pi = acos(-1.0);
const double eps = 1e-6;
ll myrand(ll mod){return ((ll)rand() << 32 ^ (ll)rand() << 16 ^ rand()) % mod;}
template <class T>
inline bool scan_d(T & ret)
{
char c;
int sgn;
if(c = getchar(), c == EOF)return false;
while(c != '-' && (c < '0' || c > '9'))c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(), c >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
ret *= sgn;
return true;
}
#ifdef Cpp11
template <class T, class ... Args>
inline bool scan_d(T & ret, Args & ... args)
{
scan_d(ret);
scan_d(args...);
}
#define cin.tie(0); cin.tie(nullptr);
#define cout.tie(0); cout.tie(nullptr);
#endif
inline bool scan_ch(char &ch)
{
if(ch = getchar(), ch == EOF)return false;
while(ch == ' ' || ch == '\n')ch = getchar();
return true;
}
template <class T>
inline void out_number(T x)
{
if(x < 0)
{
putchar('-');
out_number(- x);
return ;
}
if(x > 9)out_number(x / 10);
putchar(x % 10 + '0');
}
int nn, d, k;
ll m;
struct matrix
{
ll a[509][509];
int n;
void clear(){RESET(a), n = 0;}
};
matrix tmp, tmp2, f, ee, q1, q2;
void one(int n)
{
tmp.clear();
tmp.n = n;
for(int i = 1; i <= n; ++ i) tmp.a[i][i] = 1;
//return tmp;
}
void mul(const matrix &ma, const matrix &b)
{
tmp.clear();
tmp.n = ma.n;
for(int i = 1; i <= ma.n; ++ i)
for(int j = 1; j <= ma.n; ++ j)
tmp.a[1][j] = (tmp.a[1][j] + ma.a[1][i] * b.a[i][j]) % m;
for(int i = 2; i <= ma.n; ++ i)
for(int j = 1; j <= ma.n; ++ j)
tmp.a[i][j] = tmp.a[i - 1][j == 1 ? ma.n : j - 1];
//return tmp;
}
void pow(matrix &q, int b)
{
q1.n = q.n;
one(q.n);
q2 = tmp;
for(int i = 1; i <= tmp.n; ++ i)
for(int j = 1; j <= tmp.n; ++ j)
q1.a[i][j] = q.a[i][j];
while(b)
{
if(b & 1)
{
mul(q1, q2);
q2 = tmp;
}
b >>= 1;
mul(q1, q1);
q1 = tmp;
}
// return q2;
}
ll beg[1000], now[1000];
//map <ll, int> mp;
//bool begin;
int main()
{
//freopen("cell.in", "r", stdin);
//freopen("cell.out", "w", stdout);
while(~ scanf("%d %I64d %d %d", &nn, &m, &d, &k))
{
for(int i = 1; i <= nn; ++ i) scanf("%I64d", beg + i);
tmp.clear();
tmp2.clear();
f.clear();
ee.clear();
q1.clear();
q2.clear();
RESET(now);
/*
memcpy(sv + 1, beg + 1, n * sizeof(int));
begin = false;
for(int q = 1; q <= k; ++ q)
{
RESET(now);
for(int i = 1; i <= n; ++ i)
{
for(int j = 0; j <= d; ++ j)
now[i] = (now[i] + beg[(i + j - 1) % n + 1]) % m;
for(int j = 1; j <= d; ++ j)
now[i] = (now[i] + beg[(i - j - 1 + n) % n + 1]) % m;
}
memcpy(beg + 1, now + 1, n * sizeof(int));
hsh = 0;
for(int i = 1; i <= n; ++ i)
{
hsh *= n;
hsh += now[i];
}
if(mp.find(hsh) == mp.end()) mp[hsh] = 1;
else
{
k %= q;
begin = true;
break;
}
}
if(begin)
{
memcpy(beg + 1, sv + 1, n * sizeof(int));
for(int q = 1; q <= k + 1; ++ q)
{
RESET(now);
for(int i = 1; i <= n; ++ i)
{
for(int j = 0; j <= d; ++ j)
now[i] = (now[i] + beg[(i + j - 1) % n + 1]) % m;
for(int j = 1; j <= d; ++ j)
now[i] = (now[i] + beg[(i - j - 1 + n) % n + 1]) % m;
}
memcpy(beg + 1, now + 1, n * sizeof(int));
}
}
*/
f.n = nn;
for(int i = 1, p; i <= nn; ++ i)
for(int j = 1; j <= nn; ++ j)
{
p = abs(i - j);
if(min(p, nn - p) <= d) f.a[i][j] = 1;
else f.a[i][j] = 0;
}
pow(f, k);
ee = q2;
for(int i = 1; i <= nn; ++ i)
for(int j = 1; j <= nn; ++ j)
{
now[i] = (now[i] + beg[j] * ee.a[j][i]) % m;
}
for(int i = 1; i <= nn; ++ i) printf("%I64d ", now[i]);
puts("");
}
return 0;
}
/*
5 3 1 1
1 2 2 1 2
5 3 1 10
1 2 2 1 2
5 123 33 1000
29 28 38 20 19
20 20 293 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1
*/

BZOJ 2510

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//#define NOSTDCPP
//#define Cpp11
//#define Linux_System
#ifndef NOSTDCPP
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <cstdio>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#endif
# ifdef Linux_System
# define getchar getchar_unlocked
# define putchar putchar_unlocked
# endif
# define RESET(_) memset(_, 0, sizeof(_))
# define RESET_(_, val) memset(_, val, sizeof(_))
# define fi first
# define se second
# define pb push_back
# define midf(x, y) ((x + y) >> 1)
# define DXA(_) ((_ << 1))
# define DXB(_) ((_ << 1) | 1)
# define begin __Chtholly__
# define x1 __Mercury__
# define y1 __bbtl04__
# define index __ikooo__
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;
typedef long double ld;
const int MOD = 1e9 + 7;
const int maxn = 300009;
const int maxm = 300009;
const ll inf = 1e18;
const double pi = acos(-1.0);
const double eps = 1e-6;
ll myrand(ll mod){return ((ll)rand() << 32 ^ (ll)rand() << 16 ^ rand()) % mod;}
template <class T>
inline bool scan_d(T & ret)
{
char c;
int sgn;
if(c = getchar(), c == EOF)return false;
while(c != '-' && (c < '0' || c > '9'))c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(), c >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
ret *= sgn;
return true;
}
#ifdef Cpp11
template <class T, class ... Args>
inline bool scan_d(T & ret, Args & ... args)
{
scan_d(ret);
scan_d(args...);
}
#define cin.tie(0); cin.tie(nullptr);
#define cout.tie(0); cout.tie(nullptr);
#endif
inline bool scan_ch(char &ch)
{
if(ch = getchar(), ch == EOF)return false;
while(ch == ' ' || ch == '\n')ch = getchar();
return true;
}
template <class T>
inline void out_number(T x)
{
if(x < 0)
{
putchar('-');
out_number(- x);
return ;
}
if(x > 9)out_number(x / 10);
putchar(x % 10 + '0');
}
int nn, d, k;
ll m;
struct matrix
{
double a[1009][1009];
int n;
void clear(){RESET(a), n = 0;}
};
matrix tmp, tmp2, f, ee, q1, q2;
void one(int n)
{
tmp.clear();
tmp.n = n;
for(int i = 1; i <= n; ++ i) tmp.a[i][i] = 1;
//return tmp;
}
void mul(const matrix &ma, const matrix &b)
{
tmp.clear();
tmp.n = ma.n;
for(int i = 1; i <= ma.n; ++ i)
for(int j = 1; j <= ma.n; ++ j)
tmp.a[1][j] = (tmp.a[1][j] + ma.a[1][i] * b.a[i][j]);
for(int i = 2; i <= ma.n; ++ i)
for(int j = 1; j <= ma.n; ++ j)
tmp.a[i][j] = tmp.a[i - 1][j == 1 ? ma.n : j - 1];
//return tmp;
}
void pow(matrix &q, int b)
{
q1.n = q.n;
one(q.n);
q2 = tmp;
for(int i = 1; i <= tmp.n; ++ i)
for(int j = 1; j <= tmp.n; ++ j)
q1.a[i][j] = q.a[i][j];
while(b)
{
if(b & 1)
{
mul(q1, q2);
q2 = tmp;
}
b >>= 1;
mul(q1, q1);
q1 = tmp;
}
// return q2;
}
double beg[1000], now[1000];
int main()
{
scanf("%d %d %d", &nn, &m, &k);
for(int i = 1; i <= nn; ++ i) scanf("%lf", beg + i);
f.clear();
f.n = nn;
for(int i = 1; i <= nn; ++ i)
{
f.a[i][i] = -1.0 / (double)m + 1.0;
f.a[i][i == nn ? 1 : i + 1] = 1.0 / (double)m;
}
pow(f, k);
ee = q2;
for(int i = 1; i <= nn; ++ i)
for(int j = 1; j <= nn; ++ j)
{
now[i] = (now[i] + beg[j] * ee.a[j][i]);
}
for(int i = 1; i <= nn; ++ i) printf("%.3f\n", now[i]);
return 0;
}
/*
2 3 2
3 0
*/
文章目录
  1. 1. POJ 3150 & BZOJ 2510 带有轮换的矩阵乘法
    1. 1.1. 题目描述
      1. 1.1.1. POJ 3150
      2. 1.1.2. BZOJ 2510
    2. 1.2. 题解
      1. 1.2.1. POJ 3150
      2. 1.2.2. BZOJ 2510
      3. 1.2.3. 针对轮换矩阵的解法
    3. 1.3. AC Code
      1. 1.3.1. POJ 3150
      2. 1.3.2. BZOJ 2510
{{ live2d() }}