#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int n,k;
string path,a,b;
int power[1001];
inline int add(int i,int j)
{
if(i+j<0)return i+j+mod;
if(i+j>=mod)return i+j-mod;
return i+j;
}
inline int mul(int i,int j)
{
long long temp=(long long)i*j;
temp%=mod;
return temp;
}
void sett()
{
power[0]=1;
for(int i=1;i<=n;i++)
{
power[i]=mul(2,power[i-1]);
}
}
pair<int,int> f[1001][1001][2][2];
pair<int,int> solve(int depth,int br,bool chk,bool con,const string &s)
{
pair<int,int> cur=f[depth][br][chk][con];
if(cur!=make_pair(-1,-1))return cur;
if(depth==path.size())return make_pair(br==k,0);
if(br>k)return make_pair(0,0);
int way=0;
int cf=0;
int sum=0;
bool bv=s[depth+1]=='1';
bool mv=(path[depth]=='R' && (br+chk)%2==0)||(path[depth]=='L' && (br+chk)%2==1);
if(depth==0 && !chk)
{
pair<int,int> temp=solve(depth,br,!chk,con,s);
way=add(way,temp.first);
sum=add(sum,temp.second);
}
if(con)
{
if(mv<=bv)
{
pair<int,int> temp=solve(depth+1,br,chk,mv==bv,s);
way=add(way,temp.first);
sum=add(sum,temp.second);
if(mv)cf=add(cf,temp.first);
}
if(!mv<=bv)
{
pair<int,int> temp=solve(depth+1,br+1,chk,!mv==bv,s);
way=add(way,temp.first);
sum=add(sum,temp.second);
if(!mv)cf=add(cf,temp.first);
}
}
else
{
for(int nbr=0;nbr<2;nbr++)
{
pair<int,int> temp=solve(depth+1,br+nbr,chk,con,s);
way=add(way,temp.first);
sum=add(sum,temp.second);
if(mv!=nbr)cf=add(cf,temp.first);
}
}
sum=add(sum,mul(cf,power[n-depth-2]));
return f[depth][br][chk][con]=make_pair(way,sum);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
cin >> path;
cin >> a;
cin >> b;
sett();
a[0]=b[0]='0';
int reza=0;
int rezb=0;
memset(f,-1,sizeof f);
rezb=solve(0,0,0,1,b).second;
rezb=add(rezb,mul(power[n-1],f[0][0][0][1].first));
memset(f,-1,sizeof f);
reza=solve(0,0,0,1,a).second;
reza=add(reza,mul(power[n-1],f[0][0][0][1].first));
int rez=add(rezb,-reza);
a[0]='1';
for(int i=0;i<a.size();i++)
{
if(a[i]=='1')rez=add(rez,power[n-i-1]);
}
cout << rez;
return 0;
}
Compilation message
ljetopica.cpp: In function 'std::pair<int, int> solve(int, int, bool, bool, const string&)':
ljetopica.cpp:32:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(depth==path.size())return make_pair(br==k,0);
~~~~~^~~~~~~~~~~~~
ljetopica.cpp: In function 'int main()':
ljetopica.cpp:96:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a.size();i++)
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
31768 KB |
Output is correct |
2 |
Correct |
34 ms |
31864 KB |
Output is correct |
3 |
Correct |
35 ms |
31840 KB |
Output is correct |
4 |
Correct |
34 ms |
31864 KB |
Output is correct |
5 |
Correct |
34 ms |
31732 KB |
Output is correct |
6 |
Correct |
34 ms |
31840 KB |
Output is correct |
7 |
Correct |
43 ms |
31844 KB |
Output is correct |
8 |
Correct |
34 ms |
31740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
31708 KB |
Output is correct |
2 |
Correct |
34 ms |
31676 KB |
Output is correct |
3 |
Correct |
33 ms |
31708 KB |
Output is correct |
4 |
Correct |
34 ms |
31708 KB |
Output is correct |
5 |
Correct |
34 ms |
31760 KB |
Output is correct |
6 |
Correct |
33 ms |
31708 KB |
Output is correct |
7 |
Correct |
34 ms |
31752 KB |
Output is correct |
8 |
Correct |
34 ms |
31708 KB |
Output is correct |
9 |
Correct |
34 ms |
31704 KB |
Output is correct |
10 |
Correct |
34 ms |
31644 KB |
Output is correct |
11 |
Correct |
34 ms |
31652 KB |
Output is correct |
12 |
Correct |
34 ms |
31688 KB |
Output is correct |
13 |
Correct |
34 ms |
31736 KB |
Output is correct |
14 |
Correct |
33 ms |
31756 KB |
Output is correct |
15 |
Correct |
33 ms |
31736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
31880 KB |
Output is correct |
2 |
Correct |
65 ms |
31876 KB |
Output is correct |
3 |
Correct |
67 ms |
31876 KB |
Output is correct |
4 |
Correct |
123 ms |
31880 KB |
Output is correct |
5 |
Correct |
63 ms |
31872 KB |
Output is correct |
6 |
Correct |
129 ms |
31868 KB |
Output is correct |
7 |
Correct |
54 ms |
31872 KB |
Output is correct |
8 |
Correct |
73 ms |
31948 KB |
Output is correct |
9 |
Correct |
35 ms |
31816 KB |
Output is correct |
10 |
Correct |
65 ms |
31876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
31768 KB |
Output is correct |
2 |
Correct |
34 ms |
31864 KB |
Output is correct |
3 |
Correct |
35 ms |
31840 KB |
Output is correct |
4 |
Correct |
34 ms |
31864 KB |
Output is correct |
5 |
Correct |
34 ms |
31732 KB |
Output is correct |
6 |
Correct |
34 ms |
31840 KB |
Output is correct |
7 |
Correct |
43 ms |
31844 KB |
Output is correct |
8 |
Correct |
34 ms |
31740 KB |
Output is correct |
9 |
Correct |
33 ms |
31708 KB |
Output is correct |
10 |
Correct |
34 ms |
31676 KB |
Output is correct |
11 |
Correct |
33 ms |
31708 KB |
Output is correct |
12 |
Correct |
34 ms |
31708 KB |
Output is correct |
13 |
Correct |
34 ms |
31760 KB |
Output is correct |
14 |
Correct |
33 ms |
31708 KB |
Output is correct |
15 |
Correct |
34 ms |
31752 KB |
Output is correct |
16 |
Correct |
34 ms |
31708 KB |
Output is correct |
17 |
Correct |
34 ms |
31704 KB |
Output is correct |
18 |
Correct |
34 ms |
31644 KB |
Output is correct |
19 |
Correct |
34 ms |
31652 KB |
Output is correct |
20 |
Correct |
34 ms |
31688 KB |
Output is correct |
21 |
Correct |
34 ms |
31736 KB |
Output is correct |
22 |
Correct |
33 ms |
31756 KB |
Output is correct |
23 |
Correct |
33 ms |
31736 KB |
Output is correct |
24 |
Correct |
76 ms |
31880 KB |
Output is correct |
25 |
Correct |
65 ms |
31876 KB |
Output is correct |
26 |
Correct |
67 ms |
31876 KB |
Output is correct |
27 |
Correct |
123 ms |
31880 KB |
Output is correct |
28 |
Correct |
63 ms |
31872 KB |
Output is correct |
29 |
Correct |
129 ms |
31868 KB |
Output is correct |
30 |
Correct |
54 ms |
31872 KB |
Output is correct |
31 |
Correct |
73 ms |
31948 KB |
Output is correct |
32 |
Correct |
35 ms |
31816 KB |
Output is correct |
33 |
Correct |
65 ms |
31876 KB |
Output is correct |
34 |
Correct |
125 ms |
31880 KB |
Output is correct |
35 |
Correct |
74 ms |
31860 KB |
Output is correct |
36 |
Correct |
97 ms |
31872 KB |
Output is correct |
37 |
Correct |
144 ms |
31876 KB |
Output is correct |
38 |
Correct |
52 ms |
31816 KB |
Output is correct |
39 |
Correct |
114 ms |
31736 KB |
Output is correct |
40 |
Correct |
50 ms |
31836 KB |
Output is correct |
41 |
Correct |
104 ms |
31868 KB |
Output is correct |
42 |
Correct |
122 ms |
31872 KB |
Output is correct |
43 |
Correct |
116 ms |
31864 KB |
Output is correct |
44 |
Correct |
112 ms |
31836 KB |
Output is correct |
45 |
Correct |
69 ms |
31864 KB |
Output is correct |
46 |
Correct |
113 ms |
31864 KB |
Output is correct |
47 |
Correct |
115 ms |
31864 KB |
Output is correct |
48 |
Correct |
88 ms |
31896 KB |
Output is correct |
49 |
Correct |
39 ms |
31868 KB |
Output is correct |
50 |
Correct |
126 ms |
31872 KB |
Output is correct |
51 |
Correct |
86 ms |
31864 KB |
Output is correct |
52 |
Correct |
98 ms |
31864 KB |
Output is correct |
53 |
Correct |
133 ms |
31908 KB |
Output is correct |
54 |
Correct |
73 ms |
31804 KB |
Output is correct |
55 |
Correct |
128 ms |
31944 KB |
Output is correct |
56 |
Correct |
129 ms |
31864 KB |
Output is correct |
57 |
Correct |
47 ms |
31864 KB |
Output is correct |
58 |
Correct |
125 ms |
31972 KB |
Output is correct |