#include<bits/stdc++.h>
using namespace std;
using ll=long long;
static constexpr long long MOD=1000000007;
int main(){
int n,k;cin>>n>>k;
string s;cin>>s;
string a,b;cin>>a>>b;
//cerr<<n<<" "<<k<<endl;
//cerr<<s<<endl;
//cerr<<a<<" "<<b<<endl;
auto f=[&](const string&t)->ll {
//cerr<<t<<" "<<s<<endl;
array<vector<ll>,2>dp;
array<vector<ll>,2>sum;
dp[0]=vector<ll>(k+1,0);
dp[1]=vector<ll>(k+1,0);
sum[0]=vector<ll>(k+1,0);
sum[1]=vector<ll>(k+1,0);
if(t[0]=='1'){
dp[0][0]=1;
sum[0][0]=1;
}
else{
dp[1][0]=1;
sum[1][0]=1;
}
//for(int i=0;i<=k;i++)cerr<<dp[0][i]<<" \n"[i==k];
//for(int i=0;i<=k;i++)cerr<<dp[1][i]<<" \n"[i==k];
//cerr<<endl;
for(int i=1;i<n;i++){
array<vector<ll>,2>dp2;
array<vector<ll>,2>sum2;
dp2[0]=vector<ll>(k+1,0);
dp2[1]=vector<ll>(k+1,0);
sum2[0]=vector<ll>(k+1,0);
sum2[1]=vector<ll>(k+1,0);
for(int j=0;j<=k;j++){
if(j+1<=k){
dp2[1][j+1]+=dp[1][j];
if((j+1)&1){
if(s[i-1]=='L'){
sum2[1][j+1]+=sum[1][j]*2+dp[1][j];
if(t[i]=='1')dp2[0][j+1]+=dp[0][j],sum2[0][j+1]+=sum[0][j]*2+dp[0][j];
}
else{
sum2[1][j+1]+=sum[1][j]*2;
if(t[i]=='1')dp2[1][j+1]+=dp[0][j],sum2[1][j+1]+=sum[0][j]*2;
else dp2[0][j+1]+=dp[0][j],sum2[0][j+1]+=sum[0][j]*2;
}
}
else{
if(s[i-1]=='L'){
sum2[1][j+1]+=sum[1][j]*2;
if(t[i]=='1')dp2[1][j+1]+=dp[0][j],sum2[1][j+1]+=sum[0][j]*2;
else dp2[0][j+1]+=dp[0][j],sum2[0][j+1]+=sum[0][j]*2;
}
else{
sum2[1][j+1]+=sum[1][j]*2+dp[1][j];
if(t[i]=='1')dp2[0][j+1]+=dp[0][j],sum2[0][j+1]+=sum[0][j]*2+dp[0][j];
}
}
dp2[0][j+1]%=MOD;
dp2[1][j+1]%=MOD;
sum2[0][j+1]%=MOD;
sum2[1][j+1]%=MOD;
}
dp2[1][j]+=dp[1][j];
if(j&1){
if(s[i-1]=='L'){
sum2[1][j]+=sum[1][j]*2+dp[1][j];
if(t[i]=='1')dp2[0][j]+=dp[0][j],sum2[0][j]+=sum[0][j]*2+dp[0][j];
}
else{
sum2[1][j]+=sum[1][j]*2;
if(t[i]=='1')dp2[1][j]+=dp[0][j],sum2[1][j]+=sum[0][j]*2;
else dp2[0][j]+=dp[0][j],sum2[0][j]+=sum[0][j]*2;
}
}
else{
if(s[i-1]=='L'){
sum2[1][j]+=sum[1][j]*2;
if(t[i]=='1')dp2[1][j]+=dp[0][j],sum2[1][j]+=sum[0][j]*2;
else dp2[0][j]+=dp[0][j],sum2[0][j]+=sum[0][j]*2;
}
else{
sum2[1][j]+=sum[1][j]*2+dp[1][j];
if(t[i]=='1')dp2[0][j]+=dp[0][j],sum2[0][j]+=sum[0][j]*2+dp[0][j];
}
}
dp2[0][j]%=MOD;
dp2[1][j]%=MOD;
sum2[0][j]%=MOD;
sum2[1][j]%=MOD;
}
swap(dp,dp2);
swap(sum,sum2);
//for(int j=0;j<=k;j++)cerr<<dp[0][j]<<" \n"[j==k];
//for(int j=0;j<=k;j++)cerr<<dp[1][j]<<" \n"[j==k];
//cerr<<endl;
}
return sum[0][k]+sum[1][k];
};
ll ans=0;
int id=n-1;
while(a[id]!='1'){
a[id]='1';
id--;
}
a[id]='0';
ans=f(b)-f(a)+MOD;
for(auto&i:s){
if(i=='L')i='R';
else i='L';
}
ans+=f(b)-f(a)+MOD;
ans%=MOD;
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
304 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
340 KB |
Output is correct |
2 |
Correct |
23 ms |
312 KB |
Output is correct |
3 |
Correct |
28 ms |
212 KB |
Output is correct |
4 |
Correct |
53 ms |
340 KB |
Output is correct |
5 |
Correct |
22 ms |
300 KB |
Output is correct |
6 |
Correct |
55 ms |
252 KB |
Output is correct |
7 |
Correct |
14 ms |
212 KB |
Output is correct |
8 |
Correct |
26 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
23 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
304 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
33 ms |
340 KB |
Output is correct |
25 |
Correct |
23 ms |
312 KB |
Output is correct |
26 |
Correct |
28 ms |
212 KB |
Output is correct |
27 |
Correct |
53 ms |
340 KB |
Output is correct |
28 |
Correct |
22 ms |
300 KB |
Output is correct |
29 |
Correct |
55 ms |
252 KB |
Output is correct |
30 |
Correct |
14 ms |
212 KB |
Output is correct |
31 |
Correct |
26 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
212 KB |
Output is correct |
33 |
Correct |
23 ms |
212 KB |
Output is correct |
34 |
Correct |
46 ms |
340 KB |
Output is correct |
35 |
Correct |
15 ms |
316 KB |
Output is correct |
36 |
Correct |
30 ms |
212 KB |
Output is correct |
37 |
Correct |
66 ms |
340 KB |
Output is correct |
38 |
Correct |
7 ms |
296 KB |
Output is correct |
39 |
Correct |
47 ms |
340 KB |
Output is correct |
40 |
Correct |
6 ms |
212 KB |
Output is correct |
41 |
Correct |
29 ms |
212 KB |
Output is correct |
42 |
Correct |
39 ms |
340 KB |
Output is correct |
43 |
Correct |
39 ms |
340 KB |
Output is correct |
44 |
Correct |
46 ms |
340 KB |
Output is correct |
45 |
Correct |
13 ms |
320 KB |
Output is correct |
46 |
Correct |
37 ms |
212 KB |
Output is correct |
47 |
Correct |
40 ms |
340 KB |
Output is correct |
48 |
Correct |
22 ms |
212 KB |
Output is correct |
49 |
Correct |
2 ms |
212 KB |
Output is correct |
50 |
Correct |
89 ms |
340 KB |
Output is correct |
51 |
Correct |
20 ms |
212 KB |
Output is correct |
52 |
Correct |
21 ms |
336 KB |
Output is correct |
53 |
Correct |
52 ms |
340 KB |
Output is correct |
54 |
Correct |
15 ms |
328 KB |
Output is correct |
55 |
Correct |
43 ms |
340 KB |
Output is correct |
56 |
Correct |
47 ms |
340 KB |
Output is correct |
57 |
Correct |
5 ms |
212 KB |
Output is correct |
58 |
Correct |
41 ms |
340 KB |
Output is correct |