#include <bits/stdc++.h>
using namespace std;
const int N = 1003;
const long long M = 1000000007;
int n, k;
char a[N];
char ll[N], rr[N];
long long dp[N][N][2][2];
long long dpq[N][N][2][2];
char t[N];
long long solv()
{
memset(dp, 0, sizeof dp);
memset(dpq, 0, sizeof dpq);
dp[0][0][0][1] = 1;
dpq[0][0][0][1] = 1;
dp[0][0][1][1] = 1;
dpq[0][0][1][1] = 1;
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j <= k; ++j)
{
for (int z = 0; z < 2; ++z)
{
if ((z == 0 && a[i] == 'L') || (z == 1 && a[i] == 'R'))
{
// 1
if (t[i + 1] == '0')
{
dp[i + 1][j][z][1] = (dp[i][j][z][1] * 2 + dp[i + 1][j][z][1]) % M;
dpq[i + 1][j][z][1] = (dpq[i][j][z][1] + dpq[i + 1][j][z][1]) % M;
}
else
{
dp[i + 1][j][z][0] = (dp[i][j][z][1] * 2 + dp[i + 1][j][z][0]) % M;
dpq[i + 1][j][z][0] = (dpq[i][j][z][1] + dpq[i + 1][j][z][0]) % M;
dp[i + 1][j + 1][z ^ 1][1] = (dp[i][j][z][1] * 2 + dpq[i][j][z][1] + dp[i + 1][j + 1][z ^ 1][1]) % M;
dpq[i + 1][j + 1][z ^ 1][1] = (dpq[i][j][z][1] + dpq[i + 1][j + 1][z ^ 1][1]) % M;
}
// 0
dp[i + 1][j][z][0] = (dp[i][j][z][0] * 2 + dp[i + 1][j][z][0]) % M;
dpq[i + 1][j][z][0] = (dpq[i][j][z][0] + dpq[i + 1][j][z][0]) % M;
dp[i + 1][j + 1][z ^ 1][0] = (dp[i][j][z][0] * 2 + dpq[i][j][z][0] + dp[i + 1][j + 1][z ^ 1][0]) % M;
dpq[i + 1][j + 1][z ^ 1][0] = (dpq[i][j][z][0] + dpq[i + 1][j + 1][z ^ 1][0]) % M;
}
else
{
// 1
if (t[i + 1] == '0')
{
dp[i + 1][j + 1][z ^ 1][1] = (dp[i][j][z][1] * 2 + dp[i + 1][j + 1][z ^ 1][1]) % M;
dpq[i + 1][j + 1][z ^ 1][1] = (dpq[i][j][z][1] + dpq[i + 1][j + 1][z ^ 1][1]) % M;
}
else
{
dp[i + 1][j + 1][z ^ 1][0] = (dp[i][j][z][1] * 2 + dp[i + 1][j + 1][z ^ 1][0]) % M;
dpq[i + 1][j + 1][z ^ 1][0] = (dpq[i][j][z][1] + dpq[i + 1][j + 1][z ^ 1][0]) % M;
dp[i + 1][j][z][1] = (dp[i][j][z][1] * 2 + dpq[i][j][z][1] + dp[i + 1][j][z][1]) % M;
dpq[i + 1][j][z][1] = (dpq[i][j][z][1] + dpq[i + 1][j][z][1]) % M;
}
// 0
dp[i + 1][j + 1][z ^ 1][0] = (dp[i][j][z][0] * 2 + dp[i + 1][j + 1][z ^ 1][0]) % M;
dpq[i + 1][j + 1][z ^ 1][0] = (dpq[i][j][z][0] + dpq[i + 1][j + 1][z ^ 1][0]) % M;
dp[i + 1][j][z][0] = (dp[i][j][z][0] * 2 + dpq[i][j][z][0] + dp[i + 1][j][z][0]) % M;
dpq[i + 1][j][z][0] = (dpq[i][j][z][0] + dpq[i + 1][j][z][0]) % M;
}
}
}
}
long long ans = 0;
for (int z = 0; z < 2; ++z)
{
for (int g = 0; g < 2; ++g)
{
ans = (ans + dp[n - 1][k][z][g]) % M;
}
}
return ans;
}
long long stg()
{
long long ans = 1;
for (int i = 1; i < n; ++i)
{
if (t[i] == '0')
ans = (ans * 2) % M;
else
ans = (ans * 2 + 1) % M;
}
int kk = k;
bool z = false;
for (int i = 1; i < n; ++i)
{
if ((z == false && a[i - 1] == 'L') || (z == true && a[i - 1] == 'R'))
{
if (t[i] == '0')
continue;
else
{
z ^= true;
--kk;
}
}
else
{
if (t[i] == '1')
continue;
else
{
z ^= true;
--kk;
}
}
}
if (kk == 0)
{
return ans;
}
kk = k;
z = true;
for (int i = 1; i < n; ++i)
{
if ((z == false && a[i - 1] == 'L') || (z == true && a[i - 1] == 'R'))
{
if (t[i] == '0')
continue;
else
{
z ^= true;
--kk;
}
}
else
{
if (t[i] == '1')
continue;
else
{
z ^= true;
--kk;
}
}
}
if (kk == 0)
return ans;
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> n >> k;
cin >> a;
cin >> ll >> rr;
long long ans = 0;
for (int i = 0; i < n; ++i)
t[i] = rr[i];
ans = solv();
for (int i = 0; i < n; ++i)
t[i] = ll[i];
ans = (ans - solv() + M) % M;
ans = (ans + stg()) % M;
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
63352 KB |
Output is correct |
2 |
Correct |
66 ms |
63480 KB |
Output is correct |
3 |
Correct |
67 ms |
63372 KB |
Output is correct |
4 |
Correct |
66 ms |
63352 KB |
Output is correct |
5 |
Correct |
64 ms |
63296 KB |
Output is correct |
6 |
Correct |
66 ms |
63480 KB |
Output is correct |
7 |
Correct |
65 ms |
63356 KB |
Output is correct |
8 |
Correct |
66 ms |
63324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
63320 KB |
Output is correct |
2 |
Correct |
66 ms |
63352 KB |
Output is correct |
3 |
Correct |
66 ms |
63284 KB |
Output is correct |
4 |
Correct |
65 ms |
63352 KB |
Output is correct |
5 |
Correct |
65 ms |
63356 KB |
Output is correct |
6 |
Correct |
67 ms |
63352 KB |
Output is correct |
7 |
Correct |
65 ms |
63324 KB |
Output is correct |
8 |
Correct |
65 ms |
63352 KB |
Output is correct |
9 |
Correct |
67 ms |
63352 KB |
Output is correct |
10 |
Correct |
65 ms |
63352 KB |
Output is correct |
11 |
Correct |
74 ms |
63308 KB |
Output is correct |
12 |
Correct |
65 ms |
63280 KB |
Output is correct |
13 |
Correct |
66 ms |
63352 KB |
Output is correct |
14 |
Correct |
99 ms |
63468 KB |
Output is correct |
15 |
Correct |
65 ms |
63352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
63316 KB |
Output is correct |
2 |
Correct |
90 ms |
63352 KB |
Output is correct |
3 |
Correct |
101 ms |
63528 KB |
Output is correct |
4 |
Correct |
123 ms |
63352 KB |
Output is correct |
5 |
Correct |
89 ms |
63352 KB |
Output is correct |
6 |
Correct |
126 ms |
63352 KB |
Output is correct |
7 |
Correct |
80 ms |
63352 KB |
Output is correct |
8 |
Correct |
95 ms |
63352 KB |
Output is correct |
9 |
Correct |
67 ms |
63352 KB |
Output is correct |
10 |
Correct |
87 ms |
63352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
63352 KB |
Output is correct |
2 |
Correct |
66 ms |
63480 KB |
Output is correct |
3 |
Correct |
67 ms |
63372 KB |
Output is correct |
4 |
Correct |
66 ms |
63352 KB |
Output is correct |
5 |
Correct |
64 ms |
63296 KB |
Output is correct |
6 |
Correct |
66 ms |
63480 KB |
Output is correct |
7 |
Correct |
65 ms |
63356 KB |
Output is correct |
8 |
Correct |
66 ms |
63324 KB |
Output is correct |
9 |
Correct |
67 ms |
63320 KB |
Output is correct |
10 |
Correct |
66 ms |
63352 KB |
Output is correct |
11 |
Correct |
66 ms |
63284 KB |
Output is correct |
12 |
Correct |
65 ms |
63352 KB |
Output is correct |
13 |
Correct |
65 ms |
63356 KB |
Output is correct |
14 |
Correct |
67 ms |
63352 KB |
Output is correct |
15 |
Correct |
65 ms |
63324 KB |
Output is correct |
16 |
Correct |
65 ms |
63352 KB |
Output is correct |
17 |
Correct |
67 ms |
63352 KB |
Output is correct |
18 |
Correct |
65 ms |
63352 KB |
Output is correct |
19 |
Correct |
74 ms |
63308 KB |
Output is correct |
20 |
Correct |
65 ms |
63280 KB |
Output is correct |
21 |
Correct |
66 ms |
63352 KB |
Output is correct |
22 |
Correct |
99 ms |
63468 KB |
Output is correct |
23 |
Correct |
65 ms |
63352 KB |
Output is correct |
24 |
Correct |
102 ms |
63316 KB |
Output is correct |
25 |
Correct |
90 ms |
63352 KB |
Output is correct |
26 |
Correct |
101 ms |
63528 KB |
Output is correct |
27 |
Correct |
123 ms |
63352 KB |
Output is correct |
28 |
Correct |
89 ms |
63352 KB |
Output is correct |
29 |
Correct |
126 ms |
63352 KB |
Output is correct |
30 |
Correct |
80 ms |
63352 KB |
Output is correct |
31 |
Correct |
95 ms |
63352 KB |
Output is correct |
32 |
Correct |
67 ms |
63352 KB |
Output is correct |
33 |
Correct |
87 ms |
63352 KB |
Output is correct |
34 |
Correct |
114 ms |
63420 KB |
Output is correct |
35 |
Correct |
81 ms |
63352 KB |
Output is correct |
36 |
Correct |
90 ms |
63352 KB |
Output is correct |
37 |
Correct |
145 ms |
63352 KB |
Output is correct |
38 |
Correct |
83 ms |
63460 KB |
Output is correct |
39 |
Correct |
131 ms |
63324 KB |
Output is correct |
40 |
Correct |
82 ms |
63352 KB |
Output is correct |
41 |
Correct |
97 ms |
63352 KB |
Output is correct |
42 |
Correct |
107 ms |
63276 KB |
Output is correct |
43 |
Correct |
105 ms |
63352 KB |
Output is correct |
44 |
Correct |
114 ms |
63480 KB |
Output is correct |
45 |
Correct |
80 ms |
63480 KB |
Output is correct |
46 |
Correct |
103 ms |
63352 KB |
Output is correct |
47 |
Correct |
123 ms |
63352 KB |
Output is correct |
48 |
Correct |
88 ms |
63352 KB |
Output is correct |
49 |
Correct |
87 ms |
63352 KB |
Output is correct |
50 |
Correct |
112 ms |
63480 KB |
Output is correct |
51 |
Correct |
87 ms |
63324 KB |
Output is correct |
52 |
Correct |
88 ms |
63352 KB |
Output is correct |
53 |
Correct |
122 ms |
63352 KB |
Output is correct |
54 |
Correct |
103 ms |
63352 KB |
Output is correct |
55 |
Correct |
111 ms |
63484 KB |
Output is correct |
56 |
Correct |
129 ms |
63484 KB |
Output is correct |
57 |
Correct |
74 ms |
63416 KB |
Output is correct |
58 |
Correct |
105 ms |
63384 KB |
Output is correct |