#include <stdio.h>
#include <string.h>
#define N 1000
#define MD 1000000007
int pp2[N + 1];
void init() {
int i;
pp2[0] = 1;
for (i = 1; i <= N; i++)
pp2[i] = pp2[i - 1] * 2 % MD;
}
int solve(char *aa, char *cc, int n, int k, int lt) {
static char bb[N + 1];
static int dp[N + 1][2], dq[N + 1][2];
int i, j;
bb[0] = '1';
for (i = 0; i < n - 1; i++)
bb[i + 1] = cc[i] == 'L' ? '0' : '1';
for (j = 0; j <= k; j++)
dp[j][0] = dp[j][1] = 0, dq[j][0] = dq[j][1] = 0;
dp[0][0] = 1, dq[0][0] = 0;
for (i = 0; i < n; i++) {
int a = aa[i] - '0', b = bb[i] - '0';
for (j = k; j >= 0; j--) {
int x0 = dp[j][0], y0 = dq[j][0], x1 = dp[j][1], y1 = dq[j][1], b_;
dp[j][0] = dp[j][1] = dq[j][0] = dq[j][1] = 0;
b_ = b ^ j % 2;
if (b_ != a) {
dp[j][1] = (dp[j][1] + x1) % MD;
dq[j][1] = (dq[j][1] + y1 + (long long) pp2[n - 1 - i] * x1 % MD * b_) % MD;
if (b_ < a) {
dp[j][1] = (dp[j][1] + x0) % MD;
dq[j][1] = (dq[j][1] + y0 + (long long) pp2[n - 1 - i] * x0 % MD * b_) % MD;
}
} else {
dp[j][0] = (dp[j][0] + x0) % MD;
dq[j][0] = (dq[j][0] + y0 + (long long) pp2[n - 1 - i] * x0 % MD * b_) % MD;
dp[j][1] = (dp[j][1] + x1) % MD;
dq[j][1] = (dq[j][1] + y1 + (long long) pp2[n - 1 - i] * x1 % MD * b_) % MD;
}
if (j < k) {
b_ = b ^ (j + 1) % 2;
if (b_ != a) {
dp[j + 1][1] = (dp[j + 1][1] + x1) % MD;
dq[j + 1][1] = (dq[j + 1][1] + y1 + (long long) pp2[n - 1 - i] * x1 % MD * b_) % MD;
if (b_ < a) {
dp[j + 1][1] = (dp[j + 1][1] + x0) % MD;
dq[j + 1][1] = (dq[j + 1][1] + y0 + (long long) pp2[n - 1 - i] * x0 % MD * b_) % MD;
}
} else {
dp[j + 1][0] = (dp[j + 1][0] + x0) % MD;
dq[j + 1][0] = (dq[j + 1][0] + y0 + (long long) pp2[n - 1 - i] * x0 % MD * b_) % MD;
dp[j + 1][1] = (dp[j + 1][1] + x1) % MD;
dq[j + 1][1] = (dq[j + 1][1] + y1 + (long long) pp2[n - 1 - i] * x1 % MD * b_) % MD;
}
}
}
}
return (dq[k][1] + (lt ? 0 : dq[k][0])) % MD;
}
int main() {
static char cc[N + 1], aa[N + 1], bb[N + 1];
int n, k, i, ans;
init();
scanf("%d%d%s%s%s", &n, &k, cc, aa, bb);
ans = (solve(bb, cc, n, k, 0) - solve(aa, cc, n, k, 1)) % MD;
for (i = 0; i < n - 1; i++)
cc[i] = cc[i] == 'L' ? 'R' : 'L';
ans = ((long long) ans + solve(bb, cc, n, k, 0) - solve(aa, cc, n, k, 1)) % MD;
if (ans < 0)
ans += MD;
printf("%d\n", ans);
return 0;
}
Compilation message
ljetopica.c: In function 'main':
ljetopica.c:75:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d%d%s%s%s", &n, &k, cc, aa, bb);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
280 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
288 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
224 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
280 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
280 KB |
Output is correct |
2 |
Correct |
31 ms |
204 KB |
Output is correct |
3 |
Correct |
35 ms |
204 KB |
Output is correct |
4 |
Correct |
65 ms |
284 KB |
Output is correct |
5 |
Correct |
26 ms |
296 KB |
Output is correct |
6 |
Correct |
63 ms |
296 KB |
Output is correct |
7 |
Correct |
14 ms |
212 KB |
Output is correct |
8 |
Correct |
34 ms |
208 KB |
Output is correct |
9 |
Correct |
2 ms |
204 KB |
Output is correct |
10 |
Correct |
28 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
280 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
288 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
224 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
280 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
46 ms |
280 KB |
Output is correct |
25 |
Correct |
31 ms |
204 KB |
Output is correct |
26 |
Correct |
35 ms |
204 KB |
Output is correct |
27 |
Correct |
65 ms |
284 KB |
Output is correct |
28 |
Correct |
26 ms |
296 KB |
Output is correct |
29 |
Correct |
63 ms |
296 KB |
Output is correct |
30 |
Correct |
14 ms |
212 KB |
Output is correct |
31 |
Correct |
34 ms |
208 KB |
Output is correct |
32 |
Correct |
2 ms |
204 KB |
Output is correct |
33 |
Correct |
28 ms |
296 KB |
Output is correct |
34 |
Correct |
49 ms |
284 KB |
Output is correct |
35 |
Correct |
18 ms |
300 KB |
Output is correct |
36 |
Correct |
33 ms |
280 KB |
Output is correct |
37 |
Correct |
58 ms |
280 KB |
Output is correct |
38 |
Correct |
9 ms |
304 KB |
Output is correct |
39 |
Correct |
60 ms |
280 KB |
Output is correct |
40 |
Correct |
7 ms |
284 KB |
Output is correct |
41 |
Correct |
35 ms |
204 KB |
Output is correct |
42 |
Correct |
50 ms |
276 KB |
Output is correct |
43 |
Correct |
42 ms |
204 KB |
Output is correct |
44 |
Correct |
49 ms |
280 KB |
Output is correct |
45 |
Correct |
14 ms |
300 KB |
Output is correct |
46 |
Correct |
39 ms |
204 KB |
Output is correct |
47 |
Correct |
40 ms |
204 KB |
Output is correct |
48 |
Correct |
23 ms |
204 KB |
Output is correct |
49 |
Correct |
2 ms |
204 KB |
Output is correct |
50 |
Correct |
46 ms |
204 KB |
Output is correct |
51 |
Correct |
21 ms |
204 KB |
Output is correct |
52 |
Correct |
23 ms |
280 KB |
Output is correct |
53 |
Correct |
58 ms |
204 KB |
Output is correct |
54 |
Correct |
16 ms |
296 KB |
Output is correct |
55 |
Correct |
45 ms |
204 KB |
Output is correct |
56 |
Correct |
53 ms |
276 KB |
Output is correct |
57 |
Correct |
5 ms |
204 KB |
Output is correct |
58 |
Correct |
44 ms |
204 KB |
Output is correct |