#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
const int mod = 1e9 + 7;
const int N = 1007;
int n, m;
string s, l, r;
ll p2[N];
// [idx][flips][last][restricted]
ll cnt[N][N][2][2], sum[N][N][2][2];
ll solve(string x) {
memset(cnt, 0, sizeof(cnt));
memset(sum, 0, sizeof(sum));
cnt[0][0][0][1] = cnt[0][0][1][1] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j <= m; j++) {
for (int k : {0, 1}) {
int p = s[i] == '1';
int q = p ^ 1;
if (x[i] == '0') {
(cnt[i][j + (k != q)][q][1] += cnt[i - 1][j][k][1]) %= mod;
(sum[i][j + (k != q)][q][1] += sum[i - 1][j][k][1]) %= mod;
}
else {
(cnt[i][j + (k != p)][p][1] += cnt[i - 1][j][k][1]) %= mod;
(sum[i][j + (k != p)][p][1] += sum[i - 1][j][k][1]) %= mod;
(sum[i][j + (k != p)][p][1] += cnt[i - 1][j][k][1] * p2[i] % mod) %= mod;
(cnt[i][j + (k != q)][q][0] += cnt[i - 1][j][k][1]) %= mod;
(sum[i][j + (k != q)][q][0] += sum[i - 1][j][k][1]) %= mod;
}
(cnt[i][j + (k != p)][p][0] += cnt[i - 1][j][k][0]) %= mod;
(sum[i][j + (k != p)][p][0] += sum[i - 1][j][k][0]) %= mod;
(sum[i][j + (k != p)][p][0] += cnt[i - 1][j][k][0] * p2[i] % mod) %= mod;
(cnt[i][j + (k != q)][q][0] += cnt[i - 1][j][k][0]) %= mod;
(sum[i][j + (k != q)][q][0] += sum[i - 1][j][k][0]) %= mod;
}
}
}
ll res = 0;
for (int j : {0, 1}) {
for (int k : {0, 1}) {
(res += sum[n - 1][m][j][k]) %= mod;
(res += cnt[n - 1][m][j][k] * p2[0] % mod) %= mod;
}
}
return res;
}
int main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n >> m >> s >> l >> r;
for (char &c : s) {
c = (c == 'L' ? '0' : '1');
}
s = '1' + s;
p2[n - 1] = 1;
for (int i = n - 2; i >= 0; i--)
p2[i] = p2[i + 1] * 2 % mod;
ll ans = (solve(r) - solve(l) + mod) % mod;
int cnt = 0;
int lst = l[1] == s[1];
for (int i = 2; i < n; i++) {
int here = l[i] == s[i];
if (here != lst)
cnt++;
lst = here;
}
if (cnt == m - 1 || cnt == m) {
for (int i = 0; i < n; i++) {
if (l[i] == '1')
(ans += p2[i]) %= mod;
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
63824 KB |
Output is correct |
2 |
Correct |
13 ms |
63824 KB |
Output is correct |
3 |
Correct |
13 ms |
63980 KB |
Output is correct |
4 |
Correct |
14 ms |
63824 KB |
Output is correct |
5 |
Correct |
13 ms |
63980 KB |
Output is correct |
6 |
Correct |
13 ms |
63824 KB |
Output is correct |
7 |
Correct |
13 ms |
63824 KB |
Output is correct |
8 |
Correct |
14 ms |
63800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
63824 KB |
Output is correct |
2 |
Correct |
13 ms |
63824 KB |
Output is correct |
3 |
Correct |
13 ms |
63824 KB |
Output is correct |
4 |
Correct |
12 ms |
63964 KB |
Output is correct |
5 |
Correct |
13 ms |
63908 KB |
Output is correct |
6 |
Correct |
12 ms |
63996 KB |
Output is correct |
7 |
Correct |
12 ms |
63824 KB |
Output is correct |
8 |
Correct |
13 ms |
63956 KB |
Output is correct |
9 |
Correct |
12 ms |
63836 KB |
Output is correct |
10 |
Correct |
14 ms |
63824 KB |
Output is correct |
11 |
Correct |
13 ms |
63824 KB |
Output is correct |
12 |
Correct |
12 ms |
63824 KB |
Output is correct |
13 |
Correct |
15 ms |
63808 KB |
Output is correct |
14 |
Correct |
12 ms |
63824 KB |
Output is correct |
15 |
Correct |
13 ms |
63824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
63824 KB |
Output is correct |
2 |
Correct |
46 ms |
63824 KB |
Output is correct |
3 |
Correct |
48 ms |
63824 KB |
Output is correct |
4 |
Correct |
84 ms |
63836 KB |
Output is correct |
5 |
Correct |
38 ms |
63824 KB |
Output is correct |
6 |
Correct |
87 ms |
63824 KB |
Output is correct |
7 |
Correct |
30 ms |
63824 KB |
Output is correct |
8 |
Correct |
45 ms |
63992 KB |
Output is correct |
9 |
Correct |
13 ms |
63824 KB |
Output is correct |
10 |
Correct |
42 ms |
63824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
63824 KB |
Output is correct |
2 |
Correct |
13 ms |
63824 KB |
Output is correct |
3 |
Correct |
13 ms |
63980 KB |
Output is correct |
4 |
Correct |
14 ms |
63824 KB |
Output is correct |
5 |
Correct |
13 ms |
63980 KB |
Output is correct |
6 |
Correct |
13 ms |
63824 KB |
Output is correct |
7 |
Correct |
13 ms |
63824 KB |
Output is correct |
8 |
Correct |
14 ms |
63800 KB |
Output is correct |
9 |
Correct |
14 ms |
63824 KB |
Output is correct |
10 |
Correct |
13 ms |
63824 KB |
Output is correct |
11 |
Correct |
13 ms |
63824 KB |
Output is correct |
12 |
Correct |
12 ms |
63964 KB |
Output is correct |
13 |
Correct |
13 ms |
63908 KB |
Output is correct |
14 |
Correct |
12 ms |
63996 KB |
Output is correct |
15 |
Correct |
12 ms |
63824 KB |
Output is correct |
16 |
Correct |
13 ms |
63956 KB |
Output is correct |
17 |
Correct |
12 ms |
63836 KB |
Output is correct |
18 |
Correct |
14 ms |
63824 KB |
Output is correct |
19 |
Correct |
13 ms |
63824 KB |
Output is correct |
20 |
Correct |
12 ms |
63824 KB |
Output is correct |
21 |
Correct |
15 ms |
63808 KB |
Output is correct |
22 |
Correct |
12 ms |
63824 KB |
Output is correct |
23 |
Correct |
13 ms |
63824 KB |
Output is correct |
24 |
Correct |
57 ms |
63824 KB |
Output is correct |
25 |
Correct |
46 ms |
63824 KB |
Output is correct |
26 |
Correct |
48 ms |
63824 KB |
Output is correct |
27 |
Correct |
84 ms |
63836 KB |
Output is correct |
28 |
Correct |
38 ms |
63824 KB |
Output is correct |
29 |
Correct |
87 ms |
63824 KB |
Output is correct |
30 |
Correct |
30 ms |
63824 KB |
Output is correct |
31 |
Correct |
45 ms |
63992 KB |
Output is correct |
32 |
Correct |
13 ms |
63824 KB |
Output is correct |
33 |
Correct |
42 ms |
63824 KB |
Output is correct |
34 |
Correct |
63 ms |
63824 KB |
Output is correct |
35 |
Correct |
33 ms |
63996 KB |
Output is correct |
36 |
Correct |
43 ms |
63976 KB |
Output is correct |
37 |
Correct |
80 ms |
63824 KB |
Output is correct |
38 |
Correct |
21 ms |
63824 KB |
Output is correct |
39 |
Correct |
72 ms |
63968 KB |
Output is correct |
40 |
Correct |
22 ms |
63824 KB |
Output is correct |
41 |
Correct |
51 ms |
63824 KB |
Output is correct |
42 |
Correct |
63 ms |
63824 KB |
Output is correct |
43 |
Correct |
63 ms |
63824 KB |
Output is correct |
44 |
Correct |
71 ms |
63824 KB |
Output is correct |
45 |
Correct |
29 ms |
64080 KB |
Output is correct |
46 |
Correct |
59 ms |
63824 KB |
Output is correct |
47 |
Correct |
67 ms |
63992 KB |
Output is correct |
48 |
Correct |
39 ms |
63992 KB |
Output is correct |
49 |
Correct |
15 ms |
63824 KB |
Output is correct |
50 |
Correct |
75 ms |
63824 KB |
Output is correct |
51 |
Correct |
39 ms |
63824 KB |
Output is correct |
52 |
Correct |
40 ms |
63824 KB |
Output is correct |
53 |
Correct |
84 ms |
63972 KB |
Output is correct |
54 |
Correct |
32 ms |
63824 KB |
Output is correct |
55 |
Correct |
67 ms |
63824 KB |
Output is correct |
56 |
Correct |
78 ms |
63824 KB |
Output is correct |
57 |
Correct |
19 ms |
63824 KB |
Output is correct |
58 |
Correct |
62 ms |
63824 KB |
Output is correct |