#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int md = int(1e9) + 7;
bool way[N];
int n, k, ms[2][N], f[N][2][N][2], cnt[N][2][N][2];
bool bad;
int sub(int x, int y){
x -= y;
if (x < 0)
x += md;
return x;
}
int sum(int x, int y){
x += y;
if (x >= md)
x -= md;
return x;
}
void SUM(int &x, int y){
x += y;
if (x >= md)
x -= md;
}
int calc(int nm, int mx){
for (int len = 0; len <= n; len++)
for (int tp = 0; tp < 2; tp++)
for (int kl = 0; kl <= k; kl++)
for (int ori = 0; ori < 2; ori++){
f[len][tp][kl][ori] = 0;
cnt[len][tp][kl][ori] = -1;
}
f[0][1][0][1] = f[0][1][0][0] = 1;
cnt[0][1][0][1] = cnt[0][1][0][0] = 1;
for (int len = 0; len < n - 1; len++)
for (int tp = 0; tp < 2; tp++)
for (int kl = 0; kl <= k; kl++)
for (int ori = 0; ori < 2; ori++){
if (cnt[len][tp][kl][ori] < 0) continue;
int nw = (way[len + 1] ^ ori);
int nln = len + 1;
int nt = tp;
int ko = kl;
int no = ori;
bad = 0;
if (tp == 1){
if (ms[nm][len + 1] < nw)
bad = 1;
if (ms[nm][len + 1] > nw)
nt = 0;
}
if (!bad){
SUM(f[nln][nt][ko][no], sum(f[len][tp][kl][ori], f[len][tp][kl][ori]));
if (nw == 1)
SUM(f[nln][nt][ko][no], cnt[len][tp][kl][ori]);
if (cnt[nln][nt][ko][no] < 0)
cnt[nln][nt][ko][no] = 0;
SUM(cnt[nln][nt][ko][no], cnt[len][tp][kl][ori]);
}
if (kl == k) continue;
nw = (way[len + 1] ^ ori ^ 1);
nln = len + 1;
nt = tp;
ko = kl + 1;
no = ori ^ 1;
bad = 0;
if (tp == 1){
if (ms[nm][len + 1] < nw)
bad = 1;
if (ms[nm][len + 1] > nw)
nt = 0;
}
if (!bad){
SUM(f[nln][nt][ko][no], sum(f[len][tp][kl][ori], f[len][tp][kl][ori]));
if (nw == 1)
SUM(f[nln][nt][ko][no], cnt[len][tp][kl][ori]);
if (cnt[nln][nt][ko][no] < 0)
cnt[nln][nt][ko][no] = 0;
SUM(cnt[nln][nt][ko][no], cnt[len][tp][kl][ori]);
}
}
int res = 0;
for (int tp = 0; tp <= mx; tp++)
for (int ori = 0; ori < 2; ori++)
SUM(res, f[n - 1][tp][k][ori]);
return res;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> k;
for (int i = 1; i < n; i++){
char c; cin >> c;
way[i] = bool(c == 'R');
}
for (int i = 0; i < n; i++){
char c; cin >> c;
ms[0][i] = bool(c == '1');
}
for (int i = 0; i < n; i++){
char c; cin >> c;
ms[1][i] = bool(c == '1');
}
cout << sub(calc(1, 1), calc(0, 0));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16384 KB |
Output is correct |
2 |
Correct |
12 ms |
15616 KB |
Output is correct |
3 |
Correct |
11 ms |
14848 KB |
Output is correct |
4 |
Correct |
11 ms |
13952 KB |
Output is correct |
5 |
Correct |
10 ms |
13184 KB |
Output is correct |
6 |
Correct |
10 ms |
12416 KB |
Output is correct |
7 |
Correct |
11 ms |
11648 KB |
Output is correct |
8 |
Correct |
9 ms |
10752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
768 KB |
Output is correct |
5 |
Correct |
5 ms |
768 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
768 KB |
Output is correct |
8 |
Correct |
5 ms |
768 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
5 ms |
768 KB |
Output is correct |
14 |
Correct |
5 ms |
768 KB |
Output is correct |
15 |
Correct |
5 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
32048 KB |
Output is correct |
2 |
Correct |
46 ms |
28800 KB |
Output is correct |
3 |
Correct |
49 ms |
30592 KB |
Output is correct |
4 |
Correct |
94 ms |
32052 KB |
Output is correct |
5 |
Correct |
45 ms |
28032 KB |
Output is correct |
6 |
Correct |
97 ms |
32000 KB |
Output is correct |
7 |
Correct |
33 ms |
23424 KB |
Output is correct |
8 |
Correct |
49 ms |
30336 KB |
Output is correct |
9 |
Correct |
14 ms |
17024 KB |
Output is correct |
10 |
Correct |
47 ms |
29048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16384 KB |
Output is correct |
2 |
Correct |
12 ms |
15616 KB |
Output is correct |
3 |
Correct |
11 ms |
14848 KB |
Output is correct |
4 |
Correct |
11 ms |
13952 KB |
Output is correct |
5 |
Correct |
10 ms |
13184 KB |
Output is correct |
6 |
Correct |
10 ms |
12416 KB |
Output is correct |
7 |
Correct |
11 ms |
11648 KB |
Output is correct |
8 |
Correct |
9 ms |
10752 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
768 KB |
Output is correct |
14 |
Correct |
5 ms |
768 KB |
Output is correct |
15 |
Correct |
5 ms |
768 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
5 ms |
640 KB |
Output is correct |
18 |
Correct |
5 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
768 KB |
Output is correct |
20 |
Correct |
5 ms |
640 KB |
Output is correct |
21 |
Correct |
5 ms |
768 KB |
Output is correct |
22 |
Correct |
5 ms |
768 KB |
Output is correct |
23 |
Correct |
5 ms |
768 KB |
Output is correct |
24 |
Correct |
56 ms |
32048 KB |
Output is correct |
25 |
Correct |
46 ms |
28800 KB |
Output is correct |
26 |
Correct |
49 ms |
30592 KB |
Output is correct |
27 |
Correct |
94 ms |
32052 KB |
Output is correct |
28 |
Correct |
45 ms |
28032 KB |
Output is correct |
29 |
Correct |
97 ms |
32000 KB |
Output is correct |
30 |
Correct |
33 ms |
23424 KB |
Output is correct |
31 |
Correct |
49 ms |
30336 KB |
Output is correct |
32 |
Correct |
14 ms |
17024 KB |
Output is correct |
33 |
Correct |
47 ms |
29048 KB |
Output is correct |
34 |
Correct |
88 ms |
31616 KB |
Output is correct |
35 |
Correct |
45 ms |
22528 KB |
Output is correct |
36 |
Correct |
62 ms |
28032 KB |
Output is correct |
37 |
Correct |
93 ms |
30456 KB |
Output is correct |
38 |
Correct |
29 ms |
18688 KB |
Output is correct |
39 |
Correct |
85 ms |
29304 KB |
Output is correct |
40 |
Correct |
26 ms |
18176 KB |
Output is correct |
41 |
Correct |
75 ms |
31104 KB |
Output is correct |
42 |
Correct |
84 ms |
31872 KB |
Output is correct |
43 |
Correct |
84 ms |
30592 KB |
Output is correct |
44 |
Correct |
82 ms |
29056 KB |
Output is correct |
45 |
Correct |
43 ms |
22264 KB |
Output is correct |
46 |
Correct |
84 ms |
29432 KB |
Output is correct |
47 |
Correct |
79 ms |
29824 KB |
Output is correct |
48 |
Correct |
60 ms |
26752 KB |
Output is correct |
49 |
Correct |
15 ms |
15872 KB |
Output is correct |
50 |
Correct |
88 ms |
31232 KB |
Output is correct |
51 |
Correct |
57 ms |
25984 KB |
Output is correct |
52 |
Correct |
59 ms |
27136 KB |
Output is correct |
53 |
Correct |
96 ms |
32000 KB |
Output is correct |
54 |
Correct |
46 ms |
23808 KB |
Output is correct |
55 |
Correct |
86 ms |
32000 KB |
Output is correct |
56 |
Correct |
93 ms |
32172 KB |
Output is correct |
57 |
Correct |
24 ms |
18944 KB |
Output is correct |
58 |
Correct |
84 ms |
32000 KB |
Output is correct |