#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)
#define TRACE(x) cerr << #x << " = " << x << endl
typedef long long ll;
typedef pair<int, int> point;
const int mod = 1e9 + 7;
int add(int x, int y) {x += y; if(x >= mod) return x - mod; return x;}
int sub(int x, int y) {x -= y; if(x < 0) return x + mod; return x;}
int mul(int x, int y) {return (ll) x * y % mod;}
const int MAXN = 1e3 + 5;
int n, k, pot[MAXN];
string s, A, B;
point dp[MAXN][MAXN][2][2];
bool reduce(string &border){
bool ret = false;
REP(i, sz(border))
if(border[i] == '1')
ret = true;
for(int i = sz(border) - 1; i >= 0; --i)
if(border[i] == '1'){
border[i] = '0';
FOR(j, i + 1, sz(border))
border[j] = '1';
break;
}
return ret;
}
point rek(int depth, int changes, bool flipped, bool take_all, string &border){
point &ret = dp[depth][changes][flipped][take_all];
if(ret != point(-1, -1)) return ret;
if(changes > k) return ret = {0, 0};
if(depth == n - 1){
if(changes == k) { return ret = {1, 0}; }
else return ret = {0, 0};
}
int border_value = (border[depth + 1] == '1');
int curr_value = (s[depth] == 'R');
if(flipped) curr_value = !curr_value;
if(!take_all && curr_value < border_value)
take_all = true;
if(!take_all && curr_value > border_value)
return ret = {0, 0};
int ways = 0, sum = 0;
REP(flip, 2){
if(depth == n - 2 && flip == 1) continue;
int new_flip = flipped ^ flip;
point tmp = rek(depth + 1, changes + flip, new_flip, take_all, border);
ways = add(ways, tmp.first);
sum = add(sum, tmp.second);
}
if(curr_value)
sum = add(sum, mul(ways, pot[n - depth - 2]));
return ret = {ways, sum};
}
void merge(point &A, point &B){
A.first = add(A.first, B.first);
A.second = add(A.second, B.second);
}
int solve(string &border){
if(border[0] == '0') return 0;
REP(i, MAXN) REP(j, MAXN) REP(it1, 2) REP(it2, 2)
dp[i][j][it1][it2] = {-1, -1};
point ret1 = rek(0, 0, false, false, border);
point ret2 = rek(0, 1, true, false, border);
point ret3 = rek(0, 0, true, false, border);
point ret4 = rek(0, 1, false, false, border);
merge(ret1, ret2);
merge(ret1, ret3);
merge(ret1, ret4);
return add(ret1.second, mul(ret1.first, pot[n - 1]));
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
pot[0] = 1;
FOR(i, 1, MAXN)
pot[i] = mul(pot[i - 1], 2);
cin >> n >> k;
cin >> s >> A >> B;
if(!reduce(A))
cout << solve(B);
else
cout << sub( solve(B), solve(A) );
//reduce(A); cout << solve(A);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
32120 KB |
Output is correct |
2 |
Correct |
36 ms |
32120 KB |
Output is correct |
3 |
Correct |
36 ms |
32120 KB |
Output is correct |
4 |
Correct |
35 ms |
32120 KB |
Output is correct |
5 |
Correct |
36 ms |
31992 KB |
Output is correct |
6 |
Correct |
36 ms |
32120 KB |
Output is correct |
7 |
Correct |
43 ms |
32120 KB |
Output is correct |
8 |
Correct |
35 ms |
31992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
31992 KB |
Output is correct |
2 |
Correct |
40 ms |
31992 KB |
Output is correct |
3 |
Correct |
43 ms |
31964 KB |
Output is correct |
4 |
Correct |
35 ms |
31992 KB |
Output is correct |
5 |
Correct |
36 ms |
31992 KB |
Output is correct |
6 |
Correct |
35 ms |
31992 KB |
Output is correct |
7 |
Correct |
37 ms |
31992 KB |
Output is correct |
8 |
Correct |
36 ms |
32120 KB |
Output is correct |
9 |
Correct |
41 ms |
31992 KB |
Output is correct |
10 |
Correct |
41 ms |
31992 KB |
Output is correct |
11 |
Correct |
36 ms |
31992 KB |
Output is correct |
12 |
Correct |
36 ms |
32008 KB |
Output is correct |
13 |
Correct |
40 ms |
31964 KB |
Output is correct |
14 |
Correct |
41 ms |
31992 KB |
Output is correct |
15 |
Correct |
37 ms |
31992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
32124 KB |
Output is correct |
2 |
Correct |
64 ms |
32120 KB |
Output is correct |
3 |
Correct |
67 ms |
32120 KB |
Output is correct |
4 |
Correct |
118 ms |
31996 KB |
Output is correct |
5 |
Correct |
64 ms |
32120 KB |
Output is correct |
6 |
Correct |
125 ms |
32120 KB |
Output is correct |
7 |
Correct |
56 ms |
32120 KB |
Output is correct |
8 |
Correct |
83 ms |
32092 KB |
Output is correct |
9 |
Correct |
44 ms |
32120 KB |
Output is correct |
10 |
Correct |
65 ms |
32120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
32120 KB |
Output is correct |
2 |
Correct |
36 ms |
32120 KB |
Output is correct |
3 |
Correct |
36 ms |
32120 KB |
Output is correct |
4 |
Correct |
35 ms |
32120 KB |
Output is correct |
5 |
Correct |
36 ms |
31992 KB |
Output is correct |
6 |
Correct |
36 ms |
32120 KB |
Output is correct |
7 |
Correct |
43 ms |
32120 KB |
Output is correct |
8 |
Correct |
35 ms |
31992 KB |
Output is correct |
9 |
Correct |
41 ms |
31992 KB |
Output is correct |
10 |
Correct |
40 ms |
31992 KB |
Output is correct |
11 |
Correct |
43 ms |
31964 KB |
Output is correct |
12 |
Correct |
35 ms |
31992 KB |
Output is correct |
13 |
Correct |
36 ms |
31992 KB |
Output is correct |
14 |
Correct |
35 ms |
31992 KB |
Output is correct |
15 |
Correct |
37 ms |
31992 KB |
Output is correct |
16 |
Correct |
36 ms |
32120 KB |
Output is correct |
17 |
Correct |
41 ms |
31992 KB |
Output is correct |
18 |
Correct |
41 ms |
31992 KB |
Output is correct |
19 |
Correct |
36 ms |
31992 KB |
Output is correct |
20 |
Correct |
36 ms |
32008 KB |
Output is correct |
21 |
Correct |
40 ms |
31964 KB |
Output is correct |
22 |
Correct |
41 ms |
31992 KB |
Output is correct |
23 |
Correct |
37 ms |
31992 KB |
Output is correct |
24 |
Correct |
73 ms |
32124 KB |
Output is correct |
25 |
Correct |
64 ms |
32120 KB |
Output is correct |
26 |
Correct |
67 ms |
32120 KB |
Output is correct |
27 |
Correct |
118 ms |
31996 KB |
Output is correct |
28 |
Correct |
64 ms |
32120 KB |
Output is correct |
29 |
Correct |
125 ms |
32120 KB |
Output is correct |
30 |
Correct |
56 ms |
32120 KB |
Output is correct |
31 |
Correct |
83 ms |
32092 KB |
Output is correct |
32 |
Correct |
44 ms |
32120 KB |
Output is correct |
33 |
Correct |
65 ms |
32120 KB |
Output is correct |
34 |
Correct |
139 ms |
32120 KB |
Output is correct |
35 |
Correct |
74 ms |
32116 KB |
Output is correct |
36 |
Correct |
92 ms |
31992 KB |
Output is correct |
37 |
Correct |
118 ms |
32120 KB |
Output is correct |
38 |
Correct |
56 ms |
32120 KB |
Output is correct |
39 |
Correct |
111 ms |
32120 KB |
Output is correct |
40 |
Correct |
52 ms |
32120 KB |
Output is correct |
41 |
Correct |
106 ms |
32120 KB |
Output is correct |
42 |
Correct |
115 ms |
32120 KB |
Output is correct |
43 |
Correct |
113 ms |
31992 KB |
Output is correct |
44 |
Correct |
111 ms |
32120 KB |
Output is correct |
45 |
Correct |
75 ms |
32120 KB |
Output is correct |
46 |
Correct |
107 ms |
32112 KB |
Output is correct |
47 |
Correct |
111 ms |
32140 KB |
Output is correct |
48 |
Correct |
104 ms |
32120 KB |
Output is correct |
49 |
Correct |
46 ms |
32048 KB |
Output is correct |
50 |
Correct |
138 ms |
32152 KB |
Output is correct |
51 |
Correct |
105 ms |
32128 KB |
Output is correct |
52 |
Correct |
97 ms |
32152 KB |
Output is correct |
53 |
Correct |
156 ms |
32224 KB |
Output is correct |
54 |
Correct |
78 ms |
32156 KB |
Output is correct |
55 |
Correct |
120 ms |
32128 KB |
Output is correct |
56 |
Correct |
130 ms |
32120 KB |
Output is correct |
57 |
Correct |
50 ms |
32248 KB |
Output is correct |
58 |
Correct |
115 ms |
32120 KB |
Output is correct |