Submission #706989

# Submission time Handle Problem Language Result Execution time Memory
706989 2023-03-08T08:31:48 Z Nursik Ljetopica (COI19_ljetopica) C++14
100 / 100
86 ms 63072 KB
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
#include <cstdint>
#include <cassert>
#include <functional>
#include <complex>
#include <random>

using namespace std;
 
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double
 
const ll maxn = 1e3 + 1, maxm = 1e3 + 1;
const ll mod = 1e9 + 7, cmod = 998244353, inf = 1e9, blcok = 400, p2 = 31;
const ld eps = 1e-9;

int n, k;
string s, l, r;
ll dp[maxn][maxn][2][2], go[maxn][maxn][2][2];
void add(ll &x, ll y){
    x += y;
    x %= mod;
}
ll get(string lim){
    memset(dp, 0, sizeof(dp));
    memset(go, 0, sizeof(go));
    ll sum = 0;
    lim = '#' + lim;
    dp[1][0][0][1] = 1;
    go[1][0][0][1] = 1;
    dp[1][0][1][1] = 1;
    go[1][0][1][1] = 1;
    int m = (int)lim.size();
    for (int i = 1; i < m - 1; ++i){
        for (int j = 0; j <= k; ++j){
            for (int fl = 0; fl < 2; ++fl){
                for (int flg = 0; flg < 2; ++flg){
                    if (dp[i][j][fl][flg] > 0){
                        int ni = i + 1, nj = j, nfl = fl, nflg = flg;
                        if (s[i - 1] == 'L'){
                            if (fl == 0){
                                if (lim[i + 1] == '1'){
                                    nflg = 0;
                                }
                                add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2);
                                add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                            }
                            else{
                                if (lim[i + 1] == '1' || nflg == 0){
                                    add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2 + go[i][j][fl][flg]);
                                    add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                }
                            }
                        }
                        else{
                            if (fl == 0){
                                if (lim[i + 1] == '1' || nflg == 0){
                                    add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2 + go[i][j][fl][flg]);
                                    add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                }
                            }
                            else{
                                if (lim[i + 1] == '1'){
                                    nflg = 0;
                                }
                                add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2);
                                add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                            }
                        }
                        ni = i + 1, nj = j + 1, nfl = fl, nflg = flg;
                        if (j + 1 <= k){
                            nfl ^= 1;
                            int cfl = nfl;
                            if (s[i - 1] == 'L'){
                                if (cfl == 0){
                                    if (lim[i + 1] == '1'){
                                        nflg = 0;
                                    }   
                                    add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2);
                                    add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                }   
                                else{
                                  if (lim[i + 1] == '1' || nflg == 0){
                                        add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2 + go[i][j][fl][flg]);
                                        add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                    }
                                }
                            }
                            else{
                                if (cfl == 0){
                                    if (lim[i + 1] == '1' || nflg == 0){
                                        add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2 + go[i][j][fl][flg]);
                                        add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                    }
                                }
                                else{
                                    if (lim[i + 1] == '1'){
                                        nflg = 0;
                                    }
                                    add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2);
                                    add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    for (int i = 0; i < 2; ++i){
        for (int j = 0; j < 2; ++j){
            sum = (sum + dp[m - 1][k][i][j]) % mod;
        }
    }
    return sum;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); 
    cout.tie(0);
    cin >> n >> k;
    cin >> s;
    cin >> l >> r;
   // cout << get(r);
   // exit(0);
    ll ans = (get(r) - get(l) + mod) % mod;
    //exit(0);
    memset(dp, 0, sizeof(dp));
    memset(go, 0, sizeof(go));
    string lim = l;
    lim = '#' + lim;
    dp[1][0][0][1] = 1;
    go[1][0][0][1] = 1;
    dp[1][0][1][1] = 1;
    go[1][0][1][1] = 1;
    int m = (int)lim.size();
    for (int i = 1; i < m - 1; ++i){
        for (int j = 0; j <= k; ++j){
            for (int fl = 0; fl < 2; ++fl){
                for (int flg = 0; flg < 2; ++flg){
                    if (dp[i][j][fl][flg] > 0){
                        int ni = i + 1, nj = j, nfl = fl, nflg = flg;
                        if (s[i - 1] == 'L'){
                            if (fl == 0){
                                if (lim[i + 1] == '0'){
                                    add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2);
                                    add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                }
                            }
                            else{
                                if (lim[i + 1] == '1'){
                                    add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2 + go[i][j][fl][flg]);
                                    add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                }
                            }
                        }
                        else{
                            if (fl == 0){
                                if (lim[i + 1] == '1'){
                                    add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2 + go[i][j][fl][flg]);
                                    add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                }
                            }
                            else{
                                if (lim[i + 1] == '0'){
                                    add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2);
                                    add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                }
                            }
                        }
                        if (j + 1 <= k){
                            nfl ^= 1;
                            nj = j + 1;
                            if (s[i - 1] == 'L'){
                                if (nfl == 0){
                                    if (lim[i + 1] == '0'){
                                        add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2);
                                        add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                    }
                                }
                                else{
                                    if (lim[i + 1] == '1'){
                                        add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2 + go[i][j][fl][flg]);
                                        add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                    }
                                }
                            }
                            else{
                                if (nfl == 0){
                                    if (lim[i + 1] == '1'){
                                        add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2 + go[i][j][fl][flg]);
                                        add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                    }
                                }
                                else{
                                    if (lim[i + 1] == '0'){
                                        add(dp[ni][nj][nfl][nflg], dp[i][j][fl][flg] * 2);
                                        add(go[ni][nj][nfl][nflg], go[i][j][fl][flg]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    for (int i = 0; i < 2; ++i){
        ans = (ans + dp[m - 1][k][i][1]) % mod;
    }
    cout << ans;

}
/*
*/


# Verdict Execution time Memory Grader output
1 Correct 37 ms 63060 KB Output is correct
2 Correct 46 ms 63036 KB Output is correct
3 Correct 38 ms 63060 KB Output is correct
4 Correct 40 ms 63012 KB Output is correct
5 Correct 38 ms 62992 KB Output is correct
6 Correct 38 ms 63060 KB Output is correct
7 Correct 37 ms 63032 KB Output is correct
8 Correct 38 ms 63008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 63000 KB Output is correct
2 Correct 36 ms 63060 KB Output is correct
3 Correct 37 ms 62956 KB Output is correct
4 Correct 38 ms 63012 KB Output is correct
5 Correct 39 ms 63060 KB Output is correct
6 Correct 37 ms 63048 KB Output is correct
7 Correct 37 ms 63060 KB Output is correct
8 Correct 40 ms 62924 KB Output is correct
9 Correct 39 ms 62924 KB Output is correct
10 Correct 38 ms 63068 KB Output is correct
11 Correct 43 ms 63048 KB Output is correct
12 Correct 39 ms 62968 KB Output is correct
13 Correct 37 ms 63064 KB Output is correct
14 Correct 37 ms 63052 KB Output is correct
15 Correct 37 ms 62924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 63052 KB Output is correct
2 Correct 53 ms 63060 KB Output is correct
3 Correct 55 ms 63060 KB Output is correct
4 Correct 73 ms 63040 KB Output is correct
5 Correct 54 ms 63052 KB Output is correct
6 Correct 86 ms 63056 KB Output is correct
7 Correct 48 ms 63012 KB Output is correct
8 Correct 55 ms 63060 KB Output is correct
9 Correct 43 ms 63056 KB Output is correct
10 Correct 60 ms 63056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 63060 KB Output is correct
2 Correct 46 ms 63036 KB Output is correct
3 Correct 38 ms 63060 KB Output is correct
4 Correct 40 ms 63012 KB Output is correct
5 Correct 38 ms 62992 KB Output is correct
6 Correct 38 ms 63060 KB Output is correct
7 Correct 37 ms 63032 KB Output is correct
8 Correct 38 ms 63008 KB Output is correct
9 Correct 37 ms 63000 KB Output is correct
10 Correct 36 ms 63060 KB Output is correct
11 Correct 37 ms 62956 KB Output is correct
12 Correct 38 ms 63012 KB Output is correct
13 Correct 39 ms 63060 KB Output is correct
14 Correct 37 ms 63048 KB Output is correct
15 Correct 37 ms 63060 KB Output is correct
16 Correct 40 ms 62924 KB Output is correct
17 Correct 39 ms 62924 KB Output is correct
18 Correct 38 ms 63068 KB Output is correct
19 Correct 43 ms 63048 KB Output is correct
20 Correct 39 ms 62968 KB Output is correct
21 Correct 37 ms 63064 KB Output is correct
22 Correct 37 ms 63052 KB Output is correct
23 Correct 37 ms 62924 KB Output is correct
24 Correct 62 ms 63052 KB Output is correct
25 Correct 53 ms 63060 KB Output is correct
26 Correct 55 ms 63060 KB Output is correct
27 Correct 73 ms 63040 KB Output is correct
28 Correct 54 ms 63052 KB Output is correct
29 Correct 86 ms 63056 KB Output is correct
30 Correct 48 ms 63012 KB Output is correct
31 Correct 55 ms 63060 KB Output is correct
32 Correct 43 ms 63056 KB Output is correct
33 Correct 60 ms 63056 KB Output is correct
34 Correct 72 ms 63064 KB Output is correct
35 Correct 50 ms 62960 KB Output is correct
36 Correct 57 ms 63060 KB Output is correct
37 Correct 82 ms 63052 KB Output is correct
38 Correct 44 ms 62956 KB Output is correct
39 Correct 70 ms 63056 KB Output is correct
40 Correct 45 ms 63052 KB Output is correct
41 Correct 62 ms 62956 KB Output is correct
42 Correct 68 ms 63056 KB Output is correct
43 Correct 72 ms 63060 KB Output is correct
44 Correct 75 ms 63060 KB Output is correct
45 Correct 50 ms 63024 KB Output is correct
46 Correct 65 ms 63052 KB Output is correct
47 Correct 67 ms 63060 KB Output is correct
48 Correct 59 ms 63000 KB Output is correct
49 Correct 45 ms 62960 KB Output is correct
50 Correct 74 ms 63052 KB Output is correct
51 Correct 55 ms 63052 KB Output is correct
52 Correct 55 ms 63056 KB Output is correct
53 Correct 78 ms 63060 KB Output is correct
54 Correct 51 ms 63072 KB Output is correct
55 Correct 71 ms 63060 KB Output is correct
56 Correct 75 ms 62944 KB Output is correct
57 Correct 45 ms 63060 KB Output is correct
58 Correct 69 ms 63060 KB Output is correct