This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int MOD = 1000000007;
inline int add(int a, int b){return (a+b>=MOD)?a+b-MOD:a+b;}
inline void inc(int& a, int b){a = add(a, b);}
inline int sub(int a, int b){return (a-b<0)?a-b+MOD:a-b;}
inline void dec(int &a, int b){a = sub(a, b);}
inline int mul(int a, int b){return 1ll*a*b%MOD;}
inline void grow(int &a, int b){a = mul(a, b);}
int n, k, dp1[N][N][2][2][2], dp2[N][N][2][2][2], ans;
int moves[N], lo[N], hi[N]; string _str;
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> _str;
for(int i = 0; i<n-1; ++i)
moves[i] = _str[i]=='L'?0:1;
cin >> _str;
for(int i = 0; i<n; ++i)
lo[i] = _str[i]-'0';
cin >> _str;
for(int i = 0; i<n; ++i)
hi[i] = _str[i]-'0';
dp1[0][0][1][1][0] = dp1[0][0][1][1][1] = dp2[0][0][1][1][0] = dp2[0][0][1][1][1] = 1;
for(int pos = 0; pos<n-1; ++pos)
for(int take = 0; take<=k; ++take)
for(int smol = 0; smol<2; ++smol)
for(int fat = 0; fat<2; ++fat)
for(int flag = 0; flag<2; ++flag){
int nxt = moves[pos]^flag;
int v1 = dp1[pos][take][smol][fat][flag];
int v2 = dp2[pos][take][smol][fat][flag];
// do nothing
if((!smol || (nxt>=lo[pos+1])) && (!fat || (nxt<=hi[pos+1])))
inc(dp1[pos+1][take][smol && nxt==lo[pos+1]][fat && nxt==hi[pos+1]][flag], add(mul(v1, 2), mul(nxt==1, v2))),
inc(dp2[pos+1][take][smol && nxt==lo[pos+1]][fat && nxt==hi[pos+1]][flag], v2);
// flip it
if(take==k) continue;
nxt ^= 1;
if((!smol || (nxt>=lo[pos+1])) && (!fat || (nxt<=hi[pos+1])))
inc(dp1[pos+1][take+1][smol && nxt==lo[pos+1]][fat && nxt==hi[pos+1]][flag^1], add(mul(v1, 2), mul(nxt==1, v2))),
inc(dp2[pos+1][take+1][smol && nxt==lo[pos+1]][fat && nxt==hi[pos+1]][flag^1], v2);
}
for(int a = 0; a<2; ++a)
for(int b = 0; b<2; ++b)
for(int c = 0; c<2; ++c)
inc(ans, dp1[n-1][k][a][b][c]);
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |