#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MOD = 1e9+7;
char a[1010], b[1010], s[1010];
pair<ll, ll> dp[1010][1010][4][2];
int n;
ll pw[1010];
pair<ll, ll> solve(int i, int k, int msk, int R){
if (msk==-1) return {0, 0};
if (msk==3 && a[i] > b[i]) return {0, 0};
if (k<0) return {0, 0};
if (i==n){
if (k==0) return {1, 1};
return {0, 0};
}
if (dp[i][k][msk][R].first!=-1) return dp[i][k][msk][R];
auto &[ret, cnt] = dp[i][k][msk][R];
ret = 0, cnt = 0;
int lmsk = 0, rmsk = 0;
if (msk==0) lmsk = rmsk = 0;
else if (msk==1){
if (a[i]=='1') lmsk = -1, rmsk = 1;
else lmsk = 1, rmsk = 0;
}
else if (msk==2){
if (b[i]=='0') lmsk = 2, rmsk = -1;
else lmsk = 0, rmsk = 2;
}
else{
if (a[i]=='1') lmsk = -1, rmsk = 3;
else if (b[i]=='0') lmsk = 3, rmsk = -1;
else lmsk = 1, rmsk = 2;
}
int d = n - i;
if ((int)(s[i]=='R') == R){ ///going left, not changing
auto [ret2, cnt2] = solve(i+1, k, lmsk, R);
cnt += cnt2;
ret += ret2 + pw[d-1] * cnt2;
cnt %= MOD, ret %= MOD;
}
else{ ///going left, changing
auto [ret2, cnt2] = solve(i+1, k-1, lmsk, R^1);
cnt += cnt2;
ret += ret2 + pw[d-1] * cnt2;
cnt %= MOD, ret %= MOD;
}
if ((int)(s[i]=='L') == R){ ///going right
auto [ret2, cnt2] = solve(i+1, k, rmsk, R);
cnt += cnt2;
ret += ret2 + pw[d] * cnt2;
cnt %= MOD, ret %= MOD;
}
else{
auto [ret2, cnt2] = solve(i+1, k-1, rmsk, R^1);
cnt += cnt2;
ret += ret2 + pw[d] * cnt2;
cnt %= MOD, ret %= MOD;
}
return {ret, cnt};
}
int main(){
pw[0] = 1;
for (int i=1;i<1010;i++) pw[i] = pw[i-1] * 2 % MOD;
int k;
scanf("%d %d", &n, &k);
scanf("%s", s+1);
scanf("%s", a);
scanf("%s", b);
for (int i=1;i<=n;i++){
for (int j=0;j<=k;j++){
for (int l=0;l<4;l++){
dp[i][j][l][0].first = -1;
dp[i][j][l][1].first = -1;
}
}
}
ll ans = (solve(1, k, 3, 0).first + solve(1, k, 3, 1).first) % MOD;
printf("%lld\n", ans);
return 0;
}
Compilation message
ljetopica.cpp: In function 'int main()':
ljetopica.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
ljetopica.cpp:83:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%s", s+1);
| ~~~~~^~~~~~~~~~~
ljetopica.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%s", a);
| ~~~~~^~~~~~~~~
ljetopica.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%s", b);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
3924 KB |
Output is correct |
5 |
Correct |
2 ms |
3796 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
2 ms |
3284 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
444 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
448 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
76400 KB |
Output is correct |
2 |
Correct |
65 ms |
54152 KB |
Output is correct |
3 |
Correct |
85 ms |
61408 KB |
Output is correct |
4 |
Correct |
108 ms |
121904 KB |
Output is correct |
5 |
Correct |
61 ms |
51156 KB |
Output is correct |
6 |
Correct |
124 ms |
126900 KB |
Output is correct |
7 |
Correct |
50 ms |
33156 KB |
Output is correct |
8 |
Correct |
78 ms |
60400 KB |
Output is correct |
9 |
Correct |
5 ms |
7124 KB |
Output is correct |
10 |
Correct |
67 ms |
55148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
3924 KB |
Output is correct |
5 |
Correct |
2 ms |
3796 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
2 ms |
3284 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
444 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
448 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
80 ms |
76400 KB |
Output is correct |
25 |
Correct |
65 ms |
54152 KB |
Output is correct |
26 |
Correct |
85 ms |
61408 KB |
Output is correct |
27 |
Correct |
108 ms |
121904 KB |
Output is correct |
28 |
Correct |
61 ms |
51156 KB |
Output is correct |
29 |
Correct |
124 ms |
126900 KB |
Output is correct |
30 |
Correct |
50 ms |
33156 KB |
Output is correct |
31 |
Correct |
78 ms |
60400 KB |
Output is correct |
32 |
Correct |
5 ms |
7124 KB |
Output is correct |
33 |
Correct |
67 ms |
55148 KB |
Output is correct |
34 |
Correct |
94 ms |
93004 KB |
Output is correct |
35 |
Correct |
43 ms |
34788 KB |
Output is correct |
36 |
Correct |
65 ms |
54388 KB |
Output is correct |
37 |
Correct |
48 ms |
117256 KB |
Output is correct |
38 |
Correct |
20 ms |
18352 KB |
Output is correct |
39 |
Correct |
92 ms |
102888 KB |
Output is correct |
40 |
Correct |
19 ms |
16084 KB |
Output is correct |
41 |
Correct |
76 ms |
68656 KB |
Output is correct |
42 |
Correct |
90 ms |
87472 KB |
Output is correct |
43 |
Correct |
92 ms |
87532 KB |
Output is correct |
44 |
Correct |
94 ms |
102100 KB |
Output is correct |
45 |
Correct |
39 ms |
31812 KB |
Output is correct |
46 |
Correct |
79 ms |
80456 KB |
Output is correct |
47 |
Correct |
93 ms |
85212 KB |
Output is correct |
48 |
Correct |
68 ms |
50388 KB |
Output is correct |
49 |
Correct |
4 ms |
6996 KB |
Output is correct |
50 |
Correct |
109 ms |
97804 KB |
Output is correct |
51 |
Correct |
54 ms |
46148 KB |
Output is correct |
52 |
Correct |
58 ms |
49052 KB |
Output is correct |
53 |
Correct |
107 ms |
118552 KB |
Output is correct |
54 |
Correct |
41 ms |
34660 KB |
Output is correct |
55 |
Correct |
97 ms |
95156 KB |
Output is correct |
56 |
Correct |
114 ms |
108152 KB |
Output is correct |
57 |
Correct |
16 ms |
14932 KB |
Output is correct |
58 |
Correct |
88 ms |
84832 KB |
Output is correct |