//modified from koosaga's solution
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define MOD 1000000007
typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
const ll INF=63;
const int mxn=1e3+5;
bool DEBUG=0;
bitset<mxn>bs;
string s,t,a,b;
ll ans=0ll;
int n,k;
vll pw2;
bool vis[mxn][mxn][2][2];
pll dp[mxn][mxn][2][2]; //res, cnt of '1'
ll add(ll a, ll b){
a%=MOD; b%=MOD;
a+=b;
if(a>=MOD)a-=MOD;
if(a<0)a+=MOD;
return a;
}
void init(int n){
pw2.resize(n+1);
pw2[0]=1ll;
for(int i=1; i<=n; i++){
pw2[i]=pw2[i-1]*2ll%MOD;
}
return;
}
pll f(int ptr, int left, bool match1, bool match2){
if(left<0)return pll(0,0);
if(ptr==n)return pll(0,left==0);
if(vis[ptr][left][match1][match2])return dp[ptr][left][match1][match2];
pll res(0,0);
{
bool nxtmatch = s[ptr]=='0';
int kr = match1!=nxtmatch;
pll r = f(ptr+1,left-kr,nxtmatch,bool(match2&(t[ptr]=='0')));
res.fi = add(res.fi,r.fi);
res.se = add(res.se,r.se);
}
if(match2==0||t[ptr]=='1'){
bool nxtmatch = s[ptr]=='1';
int kr = match1!=nxtmatch;
pll r = f(ptr+1,left-kr,nxtmatch,match2);
res.fi = add(res.fi,r.fi);
res.fi = add(res.fi,r.se*pw2[n-1-ptr]);
res.se = add(res.se,r.se);
}
//cerr<<ptr<<" "<<left<<" "<<match1<<" "<<match2<<": "<<res.fi<<" "<<res.se<<"\n";
vis[ptr][left][match1][match2]=1;
return dp[ptr][left][match1][match2]=res;
}
ll solve(string x){
t=x;
memset(vis,0,sizeof(vis));
pll res1 = f(1,k,1,1);
pll res2 = f(1,k,0,1);
//cerr<<x<<" "<<res1.fi<<" "<<res1.se<<" "<<res2.fi<<" "<<res2.se<<"\n";
res1.fi = add(res1.fi,res2.fi);
res1.se = add(res1.se,res2.se);
res1.fi = add(res1.fi, res1.se*pw2[n-1]);
return res1.fi;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
cin>>n>>k;
cin>>s>>a>>b;
s="R"+s;
for(int i=0; i<n; i++)
if(s[i]=='L')s[i]='0';
else s[i]='1';
init(n);
a.back()--;
for(int i=n-1; i>=0; i--){
if(a[i]<'0'){
a[i-1]--;
a[i]='1';
}else break;
}
//cerr<<a<<" "<<b<<"\n";
ans = solve(b);
//cerr<<ans<<"\n";
if(a[0]=='1'){
ans = add(ans,MOD-solve(a));
}
cout<<ans<<"\n";
return 0;
}
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8576 KB |
Output is correct |
2 |
Correct |
5 ms |
8320 KB |
Output is correct |
3 |
Correct |
6 ms |
8064 KB |
Output is correct |
4 |
Correct |
5 ms |
7936 KB |
Output is correct |
5 |
Correct |
6 ms |
7808 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
6 ms |
7296 KB |
Output is correct |
8 |
Correct |
5 ms |
7144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4352 KB |
Output is correct |
2 |
Correct |
3 ms |
4352 KB |
Output is correct |
3 |
Correct |
3 ms |
4352 KB |
Output is correct |
4 |
Correct |
4 ms |
4352 KB |
Output is correct |
5 |
Correct |
4 ms |
4352 KB |
Output is correct |
6 |
Correct |
3 ms |
4352 KB |
Output is correct |
7 |
Correct |
3 ms |
4352 KB |
Output is correct |
8 |
Correct |
3 ms |
4352 KB |
Output is correct |
9 |
Correct |
4 ms |
4352 KB |
Output is correct |
10 |
Correct |
4 ms |
4404 KB |
Output is correct |
11 |
Correct |
4 ms |
4352 KB |
Output is correct |
12 |
Correct |
3 ms |
4352 KB |
Output is correct |
13 |
Correct |
4 ms |
4352 KB |
Output is correct |
14 |
Correct |
4 ms |
4352 KB |
Output is correct |
15 |
Correct |
3 ms |
4352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
34168 KB |
Output is correct |
2 |
Correct |
80 ms |
28448 KB |
Output is correct |
3 |
Correct |
92 ms |
30532 KB |
Output is correct |
4 |
Correct |
190 ms |
39672 KB |
Output is correct |
5 |
Correct |
82 ms |
27512 KB |
Output is correct |
6 |
Correct |
217 ms |
39744 KB |
Output is correct |
7 |
Correct |
58 ms |
21088 KB |
Output is correct |
8 |
Correct |
85 ms |
30200 KB |
Output is correct |
9 |
Correct |
9 ms |
9728 KB |
Output is correct |
10 |
Correct |
79 ms |
28664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8576 KB |
Output is correct |
2 |
Correct |
5 ms |
8320 KB |
Output is correct |
3 |
Correct |
6 ms |
8064 KB |
Output is correct |
4 |
Correct |
5 ms |
7936 KB |
Output is correct |
5 |
Correct |
6 ms |
7808 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
6 ms |
7296 KB |
Output is correct |
8 |
Correct |
5 ms |
7144 KB |
Output is correct |
9 |
Correct |
3 ms |
4352 KB |
Output is correct |
10 |
Correct |
3 ms |
4352 KB |
Output is correct |
11 |
Correct |
3 ms |
4352 KB |
Output is correct |
12 |
Correct |
4 ms |
4352 KB |
Output is correct |
13 |
Correct |
4 ms |
4352 KB |
Output is correct |
14 |
Correct |
3 ms |
4352 KB |
Output is correct |
15 |
Correct |
3 ms |
4352 KB |
Output is correct |
16 |
Correct |
3 ms |
4352 KB |
Output is correct |
17 |
Correct |
4 ms |
4352 KB |
Output is correct |
18 |
Correct |
4 ms |
4404 KB |
Output is correct |
19 |
Correct |
4 ms |
4352 KB |
Output is correct |
20 |
Correct |
3 ms |
4352 KB |
Output is correct |
21 |
Correct |
4 ms |
4352 KB |
Output is correct |
22 |
Correct |
4 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
4352 KB |
Output is correct |
24 |
Correct |
99 ms |
34168 KB |
Output is correct |
25 |
Correct |
80 ms |
28448 KB |
Output is correct |
26 |
Correct |
92 ms |
30532 KB |
Output is correct |
27 |
Correct |
190 ms |
39672 KB |
Output is correct |
28 |
Correct |
82 ms |
27512 KB |
Output is correct |
29 |
Correct |
217 ms |
39744 KB |
Output is correct |
30 |
Correct |
58 ms |
21088 KB |
Output is correct |
31 |
Correct |
85 ms |
30200 KB |
Output is correct |
32 |
Correct |
9 ms |
9728 KB |
Output is correct |
33 |
Correct |
79 ms |
28664 KB |
Output is correct |
34 |
Correct |
215 ms |
36716 KB |
Output is correct |
35 |
Correct |
99 ms |
21240 KB |
Output is correct |
36 |
Correct |
137 ms |
27644 KB |
Output is correct |
37 |
Correct |
221 ms |
36708 KB |
Output is correct |
38 |
Correct |
65 ms |
14868 KB |
Output is correct |
39 |
Correct |
171 ms |
33872 KB |
Output is correct |
40 |
Correct |
54 ms |
13756 KB |
Output is correct |
41 |
Correct |
165 ms |
31888 KB |
Output is correct |
42 |
Correct |
213 ms |
35868 KB |
Output is correct |
43 |
Correct |
190 ms |
34680 KB |
Output is correct |
44 |
Correct |
196 ms |
33800 KB |
Output is correct |
45 |
Correct |
124 ms |
20216 KB |
Output is correct |
46 |
Correct |
180 ms |
32376 KB |
Output is correct |
47 |
Correct |
186 ms |
33392 KB |
Output is correct |
48 |
Correct |
133 ms |
26300 KB |
Output is correct |
49 |
Correct |
13 ms |
9600 KB |
Output is correct |
50 |
Correct |
206 ms |
36600 KB |
Output is correct |
51 |
Correct |
129 ms |
25340 KB |
Output is correct |
52 |
Correct |
148 ms |
26644 KB |
Output is correct |
53 |
Correct |
234 ms |
39416 KB |
Output is correct |
54 |
Correct |
105 ms |
21768 KB |
Output is correct |
55 |
Correct |
225 ms |
37548 KB |
Output is correct |
56 |
Correct |
226 ms |
38776 KB |
Output is correct |
57 |
Correct |
50 ms |
13568 KB |
Output is correct |
58 |
Correct |
230 ms |
35836 KB |
Output is correct |