#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i,a) for (int i=0;i<(a);++i)
#define FORD(i,a) for (int i=(a)-1;i>=0;i--)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORTD(i,b,a) for (int i=(b);i>=(a);--i)
#define trav(i,v) for (auto i : v)
#define all(v) v.begin(),v.end()
#define ad push_back
#define fr first
#define sc second
#define mpr(a,b) make_pair(a,b)
#define pir pair<int,int>
#define all(v) v.begin(),v.end()
#define make_unique(v) v.erase(unique(all(v)),v.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define srng mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define y1 EsiHancagorcRepa
const int N=2e3+10;
const ll MOD2=998244353;
const ll MOD=1e9+7;
ll dp[N][N][2][2][2],qan[N][N][2][2][2],P[N];
int s[N],a[N],b[N],qan0,qan1;
bool col0,col1;
char tv;
int main(){
fastio;
srng;
int n,k;
cin>>n>>k;
n--;
FOR(i,n){
char c;
cin>>c;
if (c=='L')s[i]=0;
else s[i]=1;
}
FOR(i,2*n){
if (!i)P[i]=1;
else P[i]=(P[i-1]*2ll)%MOD;
}
cin>>tv;
if (tv==0)assert(0);
FOR(i,n){
char c;
cin>>c;
a[i]=c-'0';
if (tv=='0')a[i]=0;
}
cin>>tv;
if (tv=='0'){
assert(0);
cout<<0<<endl;
return 0;
}
FOR(i,n){
char c;
cin>>c;
b[i]=c-'0';
}
FOR(pla,2)FOR(plb,2)FOR(hak,2)qan[n][0][pla][plb][hak]=1;
//cout<<n<<endl;
FORD(i,n){
//cout<<i<<endl;
FOR(j,k+1){
if (n-i<j)continue;
//cout<<1<<endl;
FOR(pla,2){
FOR(plb,2){
FOR(hak,2){
if (hak)s[i]=1-s[i];
qan0=0;
qan1=0;
col0=true;
col1=true;
if ((pla && a[i]) || (!j && s[i])){
col0=false;
}
if ((plb && !b[i]) || (!j && !s[i])){
col1=false;
}
//cout<<pla<<" "<<plb<<" "<<i<<" "<<col0<<" "<<col1<<endl;
if (s[i]==0)qan1=1;
if (s[i]==1)qan0=1;
int la1=pla;
if (a[i]==0)la1=0;
int lb0=plb;
if (b[i]==1)lb0=0;
if (col0){
int pox=(hak+qan0)%2;
dp[i][j][pla][plb][hak]+=dp[i+1][j-qan0][pla][lb0][pox];
dp[i][j][pla][plb][hak]%=MOD;
qan[i][j][pla][plb][hak]+=qan[i+1][j-qan0][pla][lb0][pox];
qan[i][j][pla][plb][hak]%=MOD;
}
if (col1){
int pox=(hak+qan1)%2;
/*
if (hak && !i && pla==plb && pla){
cout<<la1<<endl;
}*/
dp[i][j][pla][plb][hak]+=(dp[i+1][j-qan1][la1][plb][pox]+(qan[i+1][j-qan1][la1][plb][pox]*P[n-i-1])%MOD)%MOD;
dp[i][j][pla][plb][hak]%=MOD;
qan[i][j][pla][plb][hak]+=qan[i+1][j-qan1][la1][plb][pox];
qan[i][j][pla][plb][hak]%=MOD;
}
if (hak)s[i]=1-s[i];
//if (hak)
//cout<<i<<" "<<pla<<" "<<plb<<" "<<dp[i][j][pla][plb][hak]<<" "<<qan[i][j][pla][plb][hak]<<" "<<col0<<" "<<col1<<endl;
}
}
}
}
}
//cout<<dp[0][k][1][1]<<endl;
dp[0][k][1][1][0]+=(qan[0][k][1][1][0]*P[n])%MOD;
dp[0][k][1][1][0]%=MOD;
dp[0][k][1][1][1]+=(qan[0][k][1][1][1]*P[n])%MOD;
dp[0][k][1][1][1]%=MOD;
//cout<<qan[0][k][1][1]<<endl;
//assert(dp[0][k][1][1][1]>=0 && dp[0][k][1][1][0]>=0);
cout<<(dp[0][k][1][1][1]+dp[0][k][1][1][0])%MOD<<endl;
return 0;
}
/*
7 2 8
C 1
C 2
C 3
C 4
C 5
C 6
O 7
O 7
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8824 KB |
Output is correct |
2 |
Correct |
11 ms |
8440 KB |
Output is correct |
3 |
Correct |
10 ms |
8056 KB |
Output is correct |
4 |
Correct |
10 ms |
7544 KB |
Output is correct |
5 |
Correct |
10 ms |
7160 KB |
Output is correct |
6 |
Correct |
10 ms |
6776 KB |
Output is correct |
7 |
Correct |
9 ms |
6264 KB |
Output is correct |
8 |
Correct |
8 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
59956 KB |
Output is correct |
2 |
Correct |
111 ms |
48528 KB |
Output is correct |
3 |
Correct |
122 ms |
52856 KB |
Output is correct |
4 |
Correct |
166 ms |
71296 KB |
Output is correct |
5 |
Correct |
121 ms |
46684 KB |
Output is correct |
6 |
Correct |
157 ms |
71292 KB |
Output is correct |
7 |
Correct |
71 ms |
34076 KB |
Output is correct |
8 |
Correct |
120 ms |
52188 KB |
Output is correct |
9 |
Correct |
17 ms |
11384 KB |
Output is correct |
10 |
Correct |
111 ms |
49144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8824 KB |
Output is correct |
2 |
Correct |
11 ms |
8440 KB |
Output is correct |
3 |
Correct |
10 ms |
8056 KB |
Output is correct |
4 |
Correct |
10 ms |
7544 KB |
Output is correct |
5 |
Correct |
10 ms |
7160 KB |
Output is correct |
6 |
Correct |
10 ms |
6776 KB |
Output is correct |
7 |
Correct |
9 ms |
6264 KB |
Output is correct |
8 |
Correct |
8 ms |
6008 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
632 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
632 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
2 ms |
508 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
632 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
140 ms |
59956 KB |
Output is correct |
25 |
Correct |
111 ms |
48528 KB |
Output is correct |
26 |
Correct |
122 ms |
52856 KB |
Output is correct |
27 |
Correct |
166 ms |
71296 KB |
Output is correct |
28 |
Correct |
121 ms |
46684 KB |
Output is correct |
29 |
Correct |
157 ms |
71292 KB |
Output is correct |
30 |
Correct |
71 ms |
34076 KB |
Output is correct |
31 |
Correct |
120 ms |
52188 KB |
Output is correct |
32 |
Correct |
17 ms |
11384 KB |
Output is correct |
33 |
Correct |
111 ms |
49144 KB |
Output is correct |
34 |
Correct |
139 ms |
65168 KB |
Output is correct |
35 |
Correct |
75 ms |
34116 KB |
Output is correct |
36 |
Correct |
106 ms |
46968 KB |
Output is correct |
37 |
Correct |
151 ms |
65016 KB |
Output is correct |
38 |
Correct |
40 ms |
21284 KB |
Output is correct |
39 |
Correct |
127 ms |
59612 KB |
Output is correct |
40 |
Correct |
36 ms |
19320 KB |
Output is correct |
41 |
Correct |
116 ms |
55388 KB |
Output is correct |
42 |
Correct |
135 ms |
63900 KB |
Output is correct |
43 |
Correct |
130 ms |
61308 KB |
Output is correct |
44 |
Correct |
124 ms |
59132 KB |
Output is correct |
45 |
Correct |
64 ms |
32248 KB |
Output is correct |
46 |
Correct |
120 ms |
56568 KB |
Output is correct |
47 |
Correct |
123 ms |
59000 KB |
Output is correct |
48 |
Correct |
96 ms |
44408 KB |
Output is correct |
49 |
Correct |
18 ms |
11000 KB |
Output is correct |
50 |
Correct |
139 ms |
65284 KB |
Output is correct |
51 |
Correct |
90 ms |
42332 KB |
Output is correct |
52 |
Correct |
92 ms |
44744 KB |
Output is correct |
53 |
Correct |
154 ms |
70904 KB |
Output is correct |
54 |
Correct |
70 ms |
35292 KB |
Output is correct |
55 |
Correct |
149 ms |
66576 KB |
Output is correct |
56 |
Correct |
148 ms |
69624 KB |
Output is correct |
57 |
Correct |
33 ms |
18680 KB |
Output is correct |
58 |
Correct |
135 ms |
63292 KB |
Output is correct |