#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e3 + 3, MOD = 1e9 + 7;
int n, k, ans;
string s, L, R;
char c;
int dp[N][N][2][2][2];
int sum[N][N][2][2][2];
int _sum(int a, int b) {
int c = a + b;
if (MOD <= c)
c -= MOD;
return c;
}
int _mul(int a, int b) {
int c = 1LL * a * b % MOD;
return c;
}
int _pow(int a, int b) {
int res = 1;
for (; b; b >>= 1) {
if (b & 1)
res = _mul(res, a);
a = _mul(a, a);
}
return res;
}
int calc(string a, string l, string r) {
a = '0' + a;
l = '0' + l;
r = '0' + r;
dp[0][0][0][1][1] = 1;
sum[0][0][0][1][1] = 0;
dp[0][0][1][1][1] = 1;
sum[0][0][1][1][1] = 0;
for (int i = 1; i < n; i++)
for (int j = 0; j <= min(i, k); j++)
for (int rev = 0; rev < 2; rev++)
for (int m1 = 0; m1 < 2; m1++)
for (int m2 = 0; m2 < 2; m2++) {
dp[i][j][rev][m1][m2] = 0;
sum[i][j][rev][m1][m2] = 0;
c = a[i];
if (rev)
c = '0' + (a[i] == '0');
if (m1 && l[i] != c)
continue;
if (m2 && r[i] != c)
continue;
for (int M1 = 1; M1 >= m1; M1--)
for (int M2 = 1; M2 >= m2; M2--) {
if (m1 != M1 && l[i] == c)
continue;
if (m2 != M2 && r[i] == c)
continue;
if (m1 != M1 && c == '0')
continue;
if (m2 != M2 && c == '1')
continue;
// if (i == 3 && j == 2 && rev == 0 && m1 == 0 && m2 == 0)
// cout << "! " << dp[i - 1][j][rev][M1][M2] << " " << rev << " " << M1 << " " << M2 << "\n";
dp[i][j][rev][m1][m2] = _sum(dp[i][j][rev][m1][m2], dp[i - 1][j][rev][M1][M2]);
sum[i][j][rev][m1][m2] = _sum(sum[i][j][rev][m1][m2], _mul(sum[i - 1][j][rev][M1][M2], 2));
if (c == '1')
sum[i][j][rev][m1][m2] = _sum(sum[i][j][rev][m1][m2], dp[i - 1][j][rev][M1][M2]);
}
if (!j)
continue;
for (int M1 = 1; M1 >= m1; M1--)
for (int M2 = 1; M2 >= m2; M2--) {
if (m1 != M1 && l[i] == c)
continue;
if (m2 != M2 && r[i] == c)
continue;
if (m1 != M1 && c == '0')
continue;
if (m2 != M2 && c == '1')
continue;
// if (i == 3 && j == 2 && rev == 0 && m1 == 0 && m2 == 0)
// cout << "!! " << m2 << " " << M2 << " " << c << "\n";
dp[i][j][rev][m1][m2] = _sum(dp[i][j][rev][m1][m2], dp[i - 1][j - 1][1 - rev][M1][M2]);
sum[i][j][rev][m1][m2] = _sum(sum[i][j][rev][m1][m2], _mul(sum[i - 1][j - 1][1 - rev][M1][M2], 2));
if (c == '1')
sum[i][j][rev][m1][m2] = _sum(sum[i][j][rev][m1][m2], dp[i - 1][j - 1][1 - rev][M1][M2]);
}
}
// cout << a << "\n" << l << "\n" << r << "\n";
int res = 0, cnt = 0;
for (int rev = 0; rev < 2; rev++)
for (int m1 = 0; m1 < 2; m1++)
for (int m2 = 0; m2 < 2; m2++) {
// cout << "@@ " << dp[n - 1][k][rev][m1][m2] << "\n";
res = _sum(res, sum[n - 1][k][rev][m1][m2]);
cnt = _sum(cnt, dp[n - 1][k][rev][m1][m2]);
}
res = _sum(res, _mul(cnt, _pow(2, n - 1)));
return res;
}
int main() {
cin >> n >> k;
cin >> s >> c >> L >> c >> R;
for (int i = 0; i < n - 1; i++)
s[i] = '0' + (s[i] == 'R');
cout << calc(s, L, R) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
8448 KB |
Output is correct |
2 |
Correct |
8 ms |
8064 KB |
Output is correct |
3 |
Correct |
8 ms |
7680 KB |
Output is correct |
4 |
Correct |
8 ms |
7296 KB |
Output is correct |
5 |
Correct |
8 ms |
6784 KB |
Output is correct |
6 |
Correct |
9 ms |
6400 KB |
Output is correct |
7 |
Correct |
7 ms |
6016 KB |
Output is correct |
8 |
Correct |
7 ms |
5632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
33912 KB |
Output is correct |
2 |
Correct |
60 ms |
28284 KB |
Output is correct |
3 |
Correct |
63 ms |
30328 KB |
Output is correct |
4 |
Correct |
90 ms |
39160 KB |
Output is correct |
5 |
Correct |
56 ms |
27384 KB |
Output is correct |
6 |
Correct |
87 ms |
39160 KB |
Output is correct |
7 |
Correct |
40 ms |
20988 KB |
Output is correct |
8 |
Correct |
63 ms |
30072 KB |
Output is correct |
9 |
Correct |
11 ms |
9600 KB |
Output is correct |
10 |
Correct |
58 ms |
28536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
8448 KB |
Output is correct |
2 |
Correct |
8 ms |
8064 KB |
Output is correct |
3 |
Correct |
8 ms |
7680 KB |
Output is correct |
4 |
Correct |
8 ms |
7296 KB |
Output is correct |
5 |
Correct |
8 ms |
6784 KB |
Output is correct |
6 |
Correct |
9 ms |
6400 KB |
Output is correct |
7 |
Correct |
7 ms |
6016 KB |
Output is correct |
8 |
Correct |
7 ms |
5632 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
72 ms |
33912 KB |
Output is correct |
25 |
Correct |
60 ms |
28284 KB |
Output is correct |
26 |
Correct |
63 ms |
30328 KB |
Output is correct |
27 |
Correct |
90 ms |
39160 KB |
Output is correct |
28 |
Correct |
56 ms |
27384 KB |
Output is correct |
29 |
Correct |
87 ms |
39160 KB |
Output is correct |
30 |
Correct |
40 ms |
20988 KB |
Output is correct |
31 |
Correct |
63 ms |
30072 KB |
Output is correct |
32 |
Correct |
11 ms |
9600 KB |
Output is correct |
33 |
Correct |
58 ms |
28536 KB |
Output is correct |
34 |
Correct |
82 ms |
36600 KB |
Output is correct |
35 |
Correct |
42 ms |
20600 KB |
Output is correct |
36 |
Correct |
57 ms |
27256 KB |
Output is correct |
37 |
Correct |
83 ms |
36088 KB |
Output is correct |
38 |
Correct |
25 ms |
14464 KB |
Output is correct |
39 |
Correct |
76 ms |
33528 KB |
Output is correct |
40 |
Correct |
22 ms |
13440 KB |
Output is correct |
41 |
Correct |
68 ms |
31608 KB |
Output is correct |
42 |
Correct |
79 ms |
35960 KB |
Output is correct |
43 |
Correct |
75 ms |
34424 KB |
Output is correct |
44 |
Correct |
73 ms |
33272 KB |
Output is correct |
45 |
Correct |
38 ms |
19832 KB |
Output is correct |
46 |
Correct |
69 ms |
31968 KB |
Output is correct |
47 |
Correct |
74 ms |
33272 KB |
Output is correct |
48 |
Correct |
55 ms |
25848 KB |
Output is correct |
49 |
Correct |
12 ms |
9216 KB |
Output is correct |
50 |
Correct |
81 ms |
36472 KB |
Output is correct |
51 |
Correct |
52 ms |
24952 KB |
Output is correct |
52 |
Correct |
55 ms |
26360 KB |
Output is correct |
53 |
Correct |
90 ms |
39160 KB |
Output is correct |
54 |
Correct |
42 ms |
21624 KB |
Output is correct |
55 |
Correct |
83 ms |
37240 KB |
Output is correct |
56 |
Correct |
90 ms |
38776 KB |
Output is correct |
57 |
Correct |
21 ms |
13312 KB |
Output is correct |
58 |
Correct |
79 ms |
35704 KB |
Output is correct |