20190828模拟赛T2

$Problem’s$ $Website$

2019.8.28模拟赛T2

$Solution$

$dp$求解,我们设$f[i][j][k]$为前$i$个数,最后连续$j$个黑球,连续$k$个白球的方案数,这样求出来答案要$\times 2$,因为颜色可以互换

具体步骤:

1.当$n < 7$时,答案为$2^n$,因为怎样涂颜色都可以。

2.当$n \ge 7$时,我们求出一个$ans$数组,$ans[i]$为i个数时一种情况的方案数 (即$f[i][j][k]$ $j$ 为黑球个数,$k$为白球个数),因为可以互换,所以答案为$ans[n] \times 2$。

P.s. 如何处理$f$和$ans$数组?

最外层一次循环枚举$i$从$1$到$6$,每次将$f$数组清零,初始化$f[i + 1][1][0] = 1$,显然~。然后有以下式子:

$\sum\limits_{j = i + 1}^{2e5 + 10}{\sum\limits_{k = 1}^{6}{f[j][1][0] += f[j - 1][0][k], f[j][0][1] += f[j - 1][k][0];}}$

$\sum\limits_{j = i + 1}^{2e5 + 10}{\sum\limits_{k = 1}^{5}{f[j][k + 1][0] += f[j - 1][k][0], f[j][0][k + 1] += f[j - 1][0][k];}}$

$\sum\limits_{j = i + 1}^{2e5 + 10}{\sum\limits_{k = 1}^{6}{ans[j] += f[j][k][0];}}$

$\sum\limits_{j = i + 1}^{2e5 + 10}{\sum\limits_{k = 1}^{6 - i}{ans[j] += f[j][0][k];}}$

最后别忘了$mod$!

$Code$

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
//Coded by dy.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
#define gc getchar()
#define pc(x) putchar(x)
inline int sc() {
int xx = 0, ff = 1; char cch = gc;
while(!isdigit(cch)) {
if(cch == '-') ff = -1; cch = gc;
}
while(isdigit(cch)) {
xx = (xx << 1) + (xx << 3) + (cch ^ '0'); cch = gc;
}
return xx * ff;
}
inline void out(int x) {
if(x < 0)
pc('-'), x = -x;
if(x >= 10)
out(x / 10);
pc(x % 10 + '0');
}
#define re register
const int Maxn = 2e5 + 10;
const int mod = 1e9 + 7;
int t, n;
int ans[Maxn];
int f[Maxn][10][10];
inline void control() {
for(re int i = 1; i <= 6; ++i) {
memset(f, 0, sizeof(f));
f[i + 1][1][0] = 1;
for(re int j = i + 1; j <= Maxn; ++j) {
for(re int k = 1; k <= 6; ++k)
f[j][1][0] = (f[j][1][0] + f[j - 1][0][k]) % mod;
for(re int k = 1; k <= 5; ++k)
f[j][k + 1][0] = (f[j][k + 1][0] + f[j - 1][k][0]) % mod;
for(re int k = 1; k <= 6; ++k)
f[j][0][1] = (f[j][0][1] + f[j - 1][k][0]) % mod;
for(re int k = 1; k <= 5; ++k)
f[j][0][k + 1] = (f[j][0][k + 1] + f[j - 1][0][k]) % mod;
for(re int k = 1; k < 7; ++k)
ans[j] = (ans[j] + f[j][k][0]) % mod;
for(re int k = 1; i + k < 7; ++k)
ans[j] = (ans[j] + f[j][0][k]) % mod;
}
}
}
int main() {
control();
t = sc();
while(t--) {
n = sc();
if(n < 7)
out(1 << n), pc('\n');
else
out((ans[n] << 1) % mod), pc('\n');
}
return 0;
}

$rp++$