#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef pair<long long,long long> P;
P dp[1001][1001][2]; //dp[i][j]:i��°���� �����ϰ� j�� ������ �ٲٰ� �������� j=0:���� j=1:�ٸ� �̾���
char str[1001];
int n,k;
long long fp[1001];
P ans(int ind,int x,int t) {
if (dp[ind][x][t].first!=-1) {
return dp[ind][x][t];
}
if (ind==n) {
return dp[ind][x][t]=(x==0?P(1,0):P(0,0));
}
P ret=P(0,0);
if (x>0) {
int dir=0;
if (str[ind]=='L') {
dir=0;
}
else {
dir=1;
}
if (t==0) {
dir^=1;
}
P got=ans(ind+1,x-1,1-t);
if (dir==1) {
got.second+=fp[n-ind-1]*got.first;
got.second%=mod;
}
ret.first+=got.first;
ret.second+=got.second;
}
int dir=0;
if (str[ind]=='L') {
dir=0;
}
else {
dir=1;
}
if (t==1) {
dir^=1;
}
P got=ans(ind+1,x,t);
if (dir==1) {
got.second+=fp[n-ind-1]*got.first;
got.second%=mod;
}
ret.first+=got.first;
ret.second+=got.second;
ret.first%=mod;
ret.second%=mod;
return dp[ind][x][t]=ret;
}
int a[1000];
int b[1000];
P pl(P a,P b) {
return P((a.first+b.first)%mod,(a.second+b.second)%mod);
}
P fa(int t,int ind=1,int x=k) {
P ret=P(0,0);
if (ind!=1) {
int pr=0;
if (str[ind-1]=='R') {
pr=1;
}
if (t) {
pr^=1;
}
if (a[ind-1]<pr) {
return ans(ind,x,t);
}
if (a[ind-1]>pr) {
return P(0,0);
}
}
if (ind==n) {
return x==0?P(1,0):P(0,0);
}
int dir=0;
if (str[ind]=='R') {
dir=1;
}
if (t) {
dir^=1;
}
if (x>0) {
P got=fa(1-t,ind+1,x-1);
if (dir==0) {
got.second+=got.first*fp[n-ind-1];
}
ret=pl(ret,got);
}
P got=fa(t,ind+1,x);
if (dir==1) {
got.second+=got.first*fp[n-ind-1];
}
ret=pl(ret,got);
return ret;
}
P fb(int t,int ind=1,int x=k) {
P ret=P(0,0);
if (ind!=1) {
int pr=0;
if (str[ind-1]=='R') {
pr=1;
}
if (t) {
pr^=1;
}
if (b[ind-1]>pr) {
return ans(ind,x,t);
}
if (b[ind-1]<pr) {
return P(0,0);
}
}
if (ind==n) {
return x==0?P(1,0):P(0,0);
}
int dir=0;
if (str[ind]=='R') {
dir=1;
}
if (t) {
dir^=1;
}
if (x>0) {
P got=fb(1-t,ind+1,x-1);
if (dir==0) {
got.second+=got.first*fp[n-ind-1];
}
ret=pl(ret,got);
}
P got=fb(t,ind+1,x);
if (dir==1) {
got.second+=got.first*fp[n-ind-1];
}
ret=pl(ret,got);
return ret;
}
long long f(P x) {
long long ret=x.second;
ret+=fp[n-1]*x.first;
return ret%mod;
}
int main(void) {
scanf("%d %d",&n,&k);
scanf("%s",str+1);
fp[0]=1;
for(int i=1;i<=n;i++) {
fp[i]=(fp[i-1]*2)%mod;
}
for(int i=0;i<n;i++) {
scanf("%1d",&a[i]);
}
for(int i=0;i<n;i++) {
scanf("%1d",&b[i]);
}
for(int i=0;i<=n;i++) {
for(int j=0;j<=n;j++) {
dp[i][j][0]=P(-1,-1);
dp[i][j][1]=P(-1,-1);
}
}
long long ret=f(fa(0));
ret+=f(fa(1));
ret+=f(fb(0));
ret+=f(fb(1));
ret%=mod;
ret+=mod-f(ans(1,k,0));
ret+=mod-f(ans(1,k,1));
ret%=mod;
printf("%lld",ret);
}
Compilation message
ljetopica.cpp: In function 'int main()':
ljetopica.cpp:158:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
158 | scanf("%d %d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~~
ljetopica.cpp:159:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | scanf("%s",str+1);
| ~~~~~^~~~~~~~~~~~
ljetopica.cpp:165:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
165 | scanf("%1d",&a[i]);
| ~~~~~^~~~~~~~~~~~~
ljetopica.cpp:168:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | scanf("%1d",&b[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
31780 KB |
Output is correct |
2 |
Correct |
14 ms |
30160 KB |
Output is correct |
3 |
Correct |
13 ms |
28612 KB |
Output is correct |
4 |
Correct |
12 ms |
26452 KB |
Output is correct |
5 |
Correct |
12 ms |
23672 KB |
Output is correct |
6 |
Correct |
10 ms |
20996 KB |
Output is correct |
7 |
Correct |
12 ms |
18516 KB |
Output is correct |
8 |
Correct |
8 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
31788 KB |
Output is correct |
2 |
Correct |
36 ms |
31776 KB |
Output is correct |
3 |
Correct |
43 ms |
31760 KB |
Output is correct |
4 |
Correct |
43 ms |
31760 KB |
Output is correct |
5 |
Correct |
45 ms |
31700 KB |
Output is correct |
6 |
Correct |
49 ms |
31760 KB |
Output is correct |
7 |
Correct |
28 ms |
31700 KB |
Output is correct |
8 |
Correct |
34 ms |
31700 KB |
Output is correct |
9 |
Correct |
16 ms |
31744 KB |
Output is correct |
10 |
Correct |
30 ms |
31776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
31780 KB |
Output is correct |
2 |
Correct |
14 ms |
30160 KB |
Output is correct |
3 |
Correct |
13 ms |
28612 KB |
Output is correct |
4 |
Correct |
12 ms |
26452 KB |
Output is correct |
5 |
Correct |
12 ms |
23672 KB |
Output is correct |
6 |
Correct |
10 ms |
20996 KB |
Output is correct |
7 |
Correct |
12 ms |
18516 KB |
Output is correct |
8 |
Correct |
8 ms |
16212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
50 ms |
31788 KB |
Output is correct |
25 |
Correct |
36 ms |
31776 KB |
Output is correct |
26 |
Correct |
43 ms |
31760 KB |
Output is correct |
27 |
Correct |
43 ms |
31760 KB |
Output is correct |
28 |
Correct |
45 ms |
31700 KB |
Output is correct |
29 |
Correct |
49 ms |
31760 KB |
Output is correct |
30 |
Correct |
28 ms |
31700 KB |
Output is correct |
31 |
Correct |
34 ms |
31700 KB |
Output is correct |
32 |
Correct |
16 ms |
31744 KB |
Output is correct |
33 |
Correct |
30 ms |
31776 KB |
Output is correct |
34 |
Correct |
35 ms |
31276 KB |
Output is correct |
35 |
Correct |
26 ms |
28680 KB |
Output is correct |
36 |
Correct |
29 ms |
29780 KB |
Output is correct |
37 |
Correct |
39 ms |
30164 KB |
Output is correct |
38 |
Correct |
18 ms |
29192 KB |
Output is correct |
39 |
Correct |
33 ms |
28920 KB |
Output is correct |
40 |
Correct |
26 ms |
29396 KB |
Output is correct |
41 |
Correct |
32 ms |
30916 KB |
Output is correct |
42 |
Correct |
42 ms |
31608 KB |
Output is correct |
43 |
Correct |
40 ms |
30324 KB |
Output is correct |
44 |
Correct |
33 ms |
28756 KB |
Output is correct |
45 |
Correct |
24 ms |
29652 KB |
Output is correct |
46 |
Correct |
32 ms |
29048 KB |
Output is correct |
47 |
Correct |
33 ms |
29572 KB |
Output is correct |
48 |
Correct |
29 ms |
29284 KB |
Output is correct |
49 |
Correct |
14 ms |
29368 KB |
Output is correct |
50 |
Correct |
49 ms |
30952 KB |
Output is correct |
51 |
Correct |
28 ms |
29984 KB |
Output is correct |
52 |
Correct |
29 ms |
30904 KB |
Output is correct |
53 |
Correct |
39 ms |
31760 KB |
Output is correct |
54 |
Correct |
29 ms |
31756 KB |
Output is correct |
55 |
Correct |
37 ms |
31756 KB |
Output is correct |
56 |
Correct |
38 ms |
31700 KB |
Output is correct |
57 |
Correct |
23 ms |
31688 KB |
Output is correct |
58 |
Correct |
51 ms |
31764 KB |
Output is correct |