#include<iostream>
#define MAXN 1005
#define MOD 1000000007
#define LL long long
using namespace std;
int N, K;
char seq[MAXN], A[MAXN], B[MAXN];
LL pow2[MAXN];
LL dp[MAXN][MAXN][2][2], dp2[MAXN][MAXN][2][2]; //num, contribution
//n,k, invert, t
LL f(int n, int k, int i, int t) {
if (k < 0) {return(0);}
return(dp[n][k][i][t]);
}
LL f2(int n, int k, int i, int t) {
if (k < 0) {return(0);}
return(dp2[n][k][i][t]);
}
int cost(int i, char At, char Do) {
if (i) {return(At == Do);}
return(At != Do);
}
LL slv(char* V) {
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][1][0]=dp[0][0][1][1]=1;
for (int n=1; n<N; n++) {
for (int k=0; k<=K; k++) {
for (int i=0; i<=1; i++) {
char L='L', R='R'; //R is the actual way to go right
//if (i) {swap(L,R);}
for (int t=0; t<=1; t++) {
if (t == 0) {
//not tight
dp[n][k][i][t]=(f(n-1, k-1, not(i), 0)+f(n-1, k, i, 0))%MOD;
dp2[n][k][i][t]=(f2(n-1, k-1, not(i), 0)+f2(n-1, k, i, 0)+f(n-1, k-cost(i, seq[n], R), (seq[n] != R), 0)*pow2[n-1])%MOD;
} else {
dp[n][k][i][t]=f(n-1, k-cost(i, seq[n], L), (seq[n] != L), V[n] == '0'); //choose 0
dp2[n][k][i][t]=f2(n-1, k-cost(i, seq[n], L), (seq[n] != L), V[n] == '0'); //choose 0
//printf("%d %d %d %d %lld %lld %d\n", n,k,i,t, f(n-1, k-cost(i, seq[n], L), (seq[n] != L), V[n] == '0'), f(n,k, i, t), cost(i, seq[n], L));
if (V[n] == '1') {
dp[n][k][i][t]=(dp[n][k][i][t] + f(n-1, k-cost(i, seq[n], R),(seq[n] != R), 1))%MOD; //choose 1 so increase hamming weight if seq[n] = 'L'
dp2[n][k][i][t]=(dp2[n][k][i][t] + f2(n-1, k-cost(i, seq[n], R), (seq[n] != R), 1) + f(n-1, k-cost(i, seq[n], R), (seq[n] != R), 1)*pow2[n-1])%MOD;
}
}
}
}
}
}
//printf("%lld %lld\n", dp[N-1][K][0][1], dp[N-1][K][1][1]);
return((dp2[N-1][K][0][1]+dp2[N-1][K][1][1] + (dp[N-1][K][0][1]+dp[N-1][K][1][1])*pow2[N-1])%MOD);
}
void initpow() {
pow2[0]=1;
for (int i=1; i<=N; i++) {
pow2[i]=(2*pow2[i-1])%MOD;
}
}
int main() {
//freopen("ljin.txt","r",stdin);
scanf("%d %d", &N, &K);
for (int i=1; i<N; i++) {
scanf("\n%c", &seq[N-i]);
}
for (int i=0; i<N; i++) {
scanf("\n%c", &A[N-i]);
}
for (int i=0; i<N; i++) {
scanf("\n%c", &B[N-i]);
}
initpow();
LL ans=(slv(B)-slv(A)+MOD)%MOD;
for (int i=1; i<=N; i++) {
if (A[i] == '1') {
ans=(ans+pow2[i-1])%MOD;
}
}
printf("%lld", ans);
}
Compilation message
ljetopica.cpp: In function 'int main()':
ljetopica.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~~
ljetopica.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("\n%c", &seq[N-i]);
~~~~~^~~~~~~~~~~~~~~~~~~
ljetopica.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("\n%c", &A[N-i]);
~~~~~^~~~~~~~~~~~~~~~~
ljetopica.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("\n%c", &B[N-i]);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8440 KB |
Output is correct |
2 |
Correct |
8 ms |
8056 KB |
Output is correct |
3 |
Correct |
10 ms |
7676 KB |
Output is correct |
4 |
Correct |
8 ms |
7160 KB |
Output is correct |
5 |
Correct |
7 ms |
6776 KB |
Output is correct |
6 |
Correct |
8 ms |
6396 KB |
Output is correct |
7 |
Correct |
8 ms |
6008 KB |
Output is correct |
8 |
Correct |
7 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
44296 KB |
Output is correct |
2 |
Correct |
54 ms |
33168 KB |
Output is correct |
3 |
Correct |
60 ms |
36700 KB |
Output is correct |
4 |
Correct |
111 ms |
63184 KB |
Output is correct |
5 |
Correct |
51 ms |
31824 KB |
Output is correct |
6 |
Correct |
114 ms |
63372 KB |
Output is correct |
7 |
Correct |
33 ms |
22648 KB |
Output is correct |
8 |
Correct |
60 ms |
36216 KB |
Output is correct |
9 |
Correct |
15 ms |
9592 KB |
Output is correct |
10 |
Correct |
55 ms |
33652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8440 KB |
Output is correct |
2 |
Correct |
8 ms |
8056 KB |
Output is correct |
3 |
Correct |
10 ms |
7676 KB |
Output is correct |
4 |
Correct |
8 ms |
7160 KB |
Output is correct |
5 |
Correct |
7 ms |
6776 KB |
Output is correct |
6 |
Correct |
8 ms |
6396 KB |
Output is correct |
7 |
Correct |
8 ms |
6008 KB |
Output is correct |
8 |
Correct |
7 ms |
5624 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
508 KB |
Output is correct |
24 |
Correct |
74 ms |
44296 KB |
Output is correct |
25 |
Correct |
54 ms |
33168 KB |
Output is correct |
26 |
Correct |
60 ms |
36700 KB |
Output is correct |
27 |
Correct |
111 ms |
63184 KB |
Output is correct |
28 |
Correct |
51 ms |
31824 KB |
Output is correct |
29 |
Correct |
114 ms |
63372 KB |
Output is correct |
30 |
Correct |
33 ms |
22648 KB |
Output is correct |
31 |
Correct |
60 ms |
36216 KB |
Output is correct |
32 |
Correct |
15 ms |
9592 KB |
Output is correct |
33 |
Correct |
55 ms |
33652 KB |
Output is correct |
34 |
Correct |
88 ms |
52448 KB |
Output is correct |
35 |
Correct |
36 ms |
22956 KB |
Output is correct |
36 |
Correct |
54 ms |
33040 KB |
Output is correct |
37 |
Correct |
121 ms |
60124 KB |
Output is correct |
38 |
Correct |
22 ms |
14764 KB |
Output is correct |
39 |
Correct |
97 ms |
56952 KB |
Output is correct |
40 |
Correct |
19 ms |
13692 KB |
Output is correct |
41 |
Correct |
64 ms |
40312 KB |
Output is correct |
42 |
Correct |
83 ms |
49784 KB |
Output is correct |
43 |
Correct |
84 ms |
49692 KB |
Output is correct |
44 |
Correct |
97 ms |
56696 KB |
Output is correct |
45 |
Correct |
33 ms |
21624 KB |
Output is correct |
46 |
Correct |
78 ms |
45816 KB |
Output is correct |
47 |
Correct |
89 ms |
48268 KB |
Output is correct |
48 |
Correct |
54 ms |
30960 KB |
Output is correct |
49 |
Correct |
11 ms |
9208 KB |
Output is correct |
50 |
Correct |
96 ms |
54904 KB |
Output is correct |
51 |
Correct |
46 ms |
28796 KB |
Output is correct |
52 |
Correct |
50 ms |
30456 KB |
Output is correct |
53 |
Correct |
109 ms |
63224 KB |
Output is correct |
54 |
Correct |
34 ms |
23420 KB |
Output is correct |
55 |
Correct |
90 ms |
53752 KB |
Output is correct |
56 |
Correct |
102 ms |
60024 KB |
Output is correct |
57 |
Correct |
18 ms |
13560 KB |
Output is correct |
58 |
Correct |
81 ms |
48504 KB |
Output is correct |