#include<iostream>
using namespace std;
using ll = long long;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define forsn(i, s, n) for(int i=s; i<(int)n; ++i)
const int MAXN=1010, MOD=1000000007;
int n, k, pot[MAXN], comb[MAXN][MAXN], dp[MAXN][MAXN][2];
string dirs, le, ri;
void pre(string& num){
int pos=num.size()-1;
while(num[pos]=='0') num[pos--]='1';
num[pos]='0';
}
void nxt(string& num){
int pos=num.size()-1;
while(pos>0 && num[pos]=='1') num[pos--]='0';
num[pos]='1';
}
bool allEq(string& num, char ch){
bool ret=true;
for(char c:num) ret&=(c==ch);
return ret;
}
int calc(string num){
int ret=0;
forn(dir, 2){
int xtra=0, sum=0;
bool cur=dir;
forsn(i, 1, num.size()){
bool differs=cur^(num[i]=='0')^(dirs[i-1]=='L');
xtra+=(int)differs;
cur^=differs;
}
forn(i, num.size()) if(num[i]=='1') sum=(sum + pot[n-1-i])%MOD;
if(xtra<=k) ret=(ret+dp[n-num.size()][k+1-xtra][cur]+((ll)comb[n-num.size()][k+1-xtra])*sum)%MOD;
}
return ret;
}
int query(string l, string r){
int ret=0;
for(; l<=r; l.pop_back(), r.pop_back()){
if(l.back()=='1'){
ret=(ret+calc(l))%MOD;
if(allEq(l, '1')) break;
nxt(l);
}
if(r.back()=='0'){
ret=(ret+calc(r))%MOD;
if(allEq(r, '0')) break;
pre(r);
}
}
return ret;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k >> dirs >> le >> ri;
pot[0]=1;
forn(i, n) pot[i+1]=(pot[i]<<1)%MOD;
forn(i, n+1) comb[i][1]=comb[i][i+1]=1;
forsn(i, 2, n+1) forsn(j, 1, n) comb[i][j+1]=(comb[i-1][j+1]+comb[i-1][j])%MOD;
forsn(i, 1, n) forsn(j, 1, i+2) forn(d, 2){
dp[i][j][d]=(dp[i-1][j][d] + dp[i-1][j-1][d^1] + comb[i-1][j-(d^(dirs[n-i-1]=='L'))]*((ll)pot[i-1]))%MOD;
}
cout << query(le, ri) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11220 KB |
Output is correct |
2 |
Correct |
8 ms |
10568 KB |
Output is correct |
3 |
Correct |
9 ms |
10024 KB |
Output is correct |
4 |
Correct |
8 ms |
9384 KB |
Output is correct |
5 |
Correct |
7 ms |
8788 KB |
Output is correct |
6 |
Correct |
6 ms |
8168 KB |
Output is correct |
7 |
Correct |
8 ms |
7764 KB |
Output is correct |
8 |
Correct |
5 ms |
6996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
11220 KB |
Output is correct |
2 |
Correct |
15 ms |
11220 KB |
Output is correct |
3 |
Correct |
18 ms |
11340 KB |
Output is correct |
4 |
Correct |
13 ms |
11272 KB |
Output is correct |
5 |
Correct |
16 ms |
11220 KB |
Output is correct |
6 |
Correct |
13 ms |
11252 KB |
Output is correct |
7 |
Correct |
15 ms |
11220 KB |
Output is correct |
8 |
Correct |
17 ms |
11264 KB |
Output is correct |
9 |
Correct |
13 ms |
11220 KB |
Output is correct |
10 |
Correct |
16 ms |
11256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11220 KB |
Output is correct |
2 |
Correct |
8 ms |
10568 KB |
Output is correct |
3 |
Correct |
9 ms |
10024 KB |
Output is correct |
4 |
Correct |
8 ms |
9384 KB |
Output is correct |
5 |
Correct |
7 ms |
8788 KB |
Output is correct |
6 |
Correct |
6 ms |
8168 KB |
Output is correct |
7 |
Correct |
8 ms |
7764 KB |
Output is correct |
8 |
Correct |
5 ms |
6996 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
16 ms |
11220 KB |
Output is correct |
25 |
Correct |
15 ms |
11220 KB |
Output is correct |
26 |
Correct |
18 ms |
11340 KB |
Output is correct |
27 |
Correct |
13 ms |
11272 KB |
Output is correct |
28 |
Correct |
16 ms |
11220 KB |
Output is correct |
29 |
Correct |
13 ms |
11252 KB |
Output is correct |
30 |
Correct |
15 ms |
11220 KB |
Output is correct |
31 |
Correct |
17 ms |
11264 KB |
Output is correct |
32 |
Correct |
13 ms |
11220 KB |
Output is correct |
33 |
Correct |
16 ms |
11256 KB |
Output is correct |
34 |
Correct |
13 ms |
11180 KB |
Output is correct |
35 |
Correct |
10 ms |
10068 KB |
Output is correct |
36 |
Correct |
12 ms |
10452 KB |
Output is correct |
37 |
Correct |
8 ms |
10572 KB |
Output is correct |
38 |
Correct |
12 ms |
10316 KB |
Output is correct |
39 |
Correct |
12 ms |
10196 KB |
Output is correct |
40 |
Correct |
11 ms |
10324 KB |
Output is correct |
41 |
Correct |
12 ms |
10948 KB |
Output is correct |
42 |
Correct |
15 ms |
11220 KB |
Output is correct |
43 |
Correct |
13 ms |
10708 KB |
Output is correct |
44 |
Correct |
11 ms |
10068 KB |
Output is correct |
45 |
Correct |
12 ms |
10448 KB |
Output is correct |
46 |
Correct |
12 ms |
10244 KB |
Output is correct |
47 |
Correct |
14 ms |
10420 KB |
Output is correct |
48 |
Correct |
11 ms |
10360 KB |
Output is correct |
49 |
Correct |
12 ms |
10324 KB |
Output is correct |
50 |
Correct |
13 ms |
10932 KB |
Output is correct |
51 |
Correct |
12 ms |
10580 KB |
Output is correct |
52 |
Correct |
13 ms |
10836 KB |
Output is correct |
53 |
Correct |
14 ms |
11220 KB |
Output is correct |
54 |
Correct |
14 ms |
11272 KB |
Output is correct |
55 |
Correct |
14 ms |
11164 KB |
Output is correct |
56 |
Correct |
14 ms |
11168 KB |
Output is correct |
57 |
Correct |
13 ms |
11284 KB |
Output is correct |
58 |
Correct |
15 ms |
11280 KB |
Output is correct |