Submission #222633

# Submission time Handle Problem Language Result Execution time Memory
222633 2020-04-13T13:11:28 Z kingfran1907 Ljetopica (COI19_ljetopica) C++14
100 / 100
212 ms 64392 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long llint;

const int maxn = 1010;
const llint mod = 1e9+7;

int n, k;
char niz[maxn];
char a[maxn];
char b[maxn];
llint pot[maxn];
char tren[maxn];
llint dp[maxn][maxn][2][2][2];

pair<llint, llint> f(int x, int k, bool rev, bool flag) {
    if (k < 0) return make_pair(0, 0);
    if (x == n) return make_pair(0, (k == 0));

    llint &ret1 = dp[x][k][rev][flag][0];
    llint &ret2 = dp[x][k][rev][flag][1];
    if (ret1 != -1) return make_pair(ret1, ret2);

    pair<int, int> tren;
    ret1 = ret2 = 0;
    if (rev) {
        if (niz[x - 1] == 'R') {
            if (::tren[x] == '0' && flag) tren = f(x + 1, k, true, true);
            else tren = f(x + 1, k, true, false);
            ret1 += tren.first, ret1 %= mod;
            ret2 += tren.second, ret2 %= mod;
            //printf("lef (%d %d %d %d): %d %d\n", x, k, rev, flag, tren.first, tren.second);

            if (::tren[x] == '0' && flag) tren = make_pair(0, 0);
            else if (::tren[x] == '1' && flag) tren = f(x + 1, k - 1, false, true);
            else if (!flag) tren = f(x + 1, k - 1, false, false);
            ret1 += tren.first, ret1 %= mod;
            ret2 += tren.second, ret2 %= mod;
            ret1 += tren.second * pot[n - x - 1], ret1 %= mod;
            //printf("rig (%d %d %d %d): %d %d\n", x, k, rev, flag, tren.first, tren.second);
        } else {
            if (::tren[x] == '0' && flag) tren = make_pair(0, 0);
            else if (::tren[x] == '1' && flag) tren = f(x + 1, k, true, true);
            else if (!flag) tren = f(x + 1, k, true, false);
            ret1 += tren.first, ret1 %= mod;
            ret2 += tren.second, ret2 %= mod;
            ret1 += tren.second * pot[n - x - 1], ret1 %= mod;
            //printf("rig (%d %d %d %d): %d %d\n", x, k, rev, flag, tren.first, tren.second);

            if (::tren[x] == '0' && flag) tren = f(x + 1, k - 1, false, true);
            else tren = f(x + 1, k - 1, false, false);
            ret1 += tren.first, ret1 %= mod;
            ret2 += tren.second, ret2 %= mod;
            //printf("lef (%d %d %d %d): %d %d\n", x, k, rev, flag, tren.first, tren.second);
        }
    } else {
        if (niz[x - 1] == 'L') {
            if (::tren[x] == '0' && flag) tren = f(x + 1, k, false, true);
            else tren = f(x + 1, k, false, false);
            ret1 += tren.first, ret1 %= mod;
            ret2 += tren.second, ret2 %= mod;

            if (::tren[x] == '0' && flag) tren = make_pair(0, 0);
            else if (::tren[x] == '1' && flag) tren = f(x + 1, k - 1, true, true);
            else if (!flag) tren = f(x + 1, k - 1, true, false);
            ret1 += tren.first, ret1 %= mod;
            ret2 += tren.second, ret2 %= mod;
            ret1 += tren.second * pot[n - x - 1], ret1 %= mod;
        } else {
            if (::tren[x] == '0' && flag) tren = make_pair(0, 0);
            else if (::tren[x] == '1' && flag) tren = f(x + 1, k, false, true);
            else if (!flag) tren = f(x + 1, k, false, false);
            ret1 += tren.first, ret1 %= mod;
            ret2 += tren.second, ret2 %= mod;
            ret1 += tren.second * pot[n - x - 1], ret1 %= mod;

            if (::tren[x] == '0' && flag) tren = f(x + 1, k - 1, true, true);
            else tren = f(x + 1, k - 1, true, false);
            ret1 += tren.first, ret1 %= mod;
            ret2 += tren.second, ret2 %= mod;
        }
    }
    return make_pair(ret1, ret2);
}

int main() {
    scanf("%d%d", &n, &k);
    scanf("%s%s%s", niz, a, b);

    pot[0] = 1;
    for (int i = 1; i < maxn; i++)
        pot[i] = pot[i - 1] * 2, pot[i] %= mod;

    bool flag = false;
    for (int i = 1; i < n; i++)
        if (a[i] == '1') flag = true;

    llint sola = 0;
    if (flag) {
        for (int i = n - 1; i > 0; i--) {
            if (a[i] == '1') {
                a[i] = '0';
                break;
            } else a[i] = '1';
        }

        for (int i = 0; i < n; i++)
            tren[i] = a[i];

        memset(dp, -1, sizeof dp);
        //printf("a: (%lld %lld) (%lld %lld)\n", f(1, k, false, true).first, f(1, k, false, true).second, f(1, k, true, true).first, f(1, k, true, true).second);
        sola = f(1, k, false, true).first + f(1, k, true, true).first + pot[n - 1] * (f(1, k, false, true).second + f(1, k, true, true).second);
        sola %= mod;
    }

    for (int i = 0; i < n; i++)
        tren[i] = b[i];

    memset(dp, -1, sizeof dp);
    //printf("b: (%lld %lld) (%lld %lld)\n", f(1, k, false, true).first, f(1, k, false, true).second, f(1, k, true, true).first, f(1, k, true, true).second);
    llint solb = f(1, k, false, true).first + f(1, k, true, true).first + pot[n - 1] * (f(1, k, false, true).second + f(1, k, true, true).second);
    solb %= mod;

    //printf("debug: %lld %lld\n", sola, solb);
    printf("%lld\n", (solb + mod - sola) % mod);
    return 0;
}

Compilation message

ljetopica.cpp: In function 'int main()':
ljetopica.cpp:88: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:89:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s%s%s", niz, a, b);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 47 ms 64380 KB Output is correct
2 Correct 46 ms 64248 KB Output is correct
3 Correct 44 ms 64248 KB Output is correct
4 Correct 46 ms 64248 KB Output is correct
5 Correct 45 ms 64256 KB Output is correct
6 Correct 46 ms 64248 KB Output is correct
7 Correct 47 ms 64248 KB Output is correct
8 Correct 55 ms 64248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 64248 KB Output is correct
2 Correct 46 ms 64248 KB Output is correct
3 Correct 46 ms 64256 KB Output is correct
4 Correct 46 ms 64248 KB Output is correct
5 Correct 47 ms 64248 KB Output is correct
6 Correct 46 ms 64248 KB Output is correct
7 Correct 47 ms 64248 KB Output is correct
8 Correct 49 ms 64256 KB Output is correct
9 Correct 45 ms 64248 KB Output is correct
10 Correct 45 ms 64248 KB Output is correct
11 Correct 46 ms 64256 KB Output is correct
12 Correct 47 ms 64248 KB Output is correct
13 Correct 46 ms 64376 KB Output is correct
14 Correct 46 ms 64248 KB Output is correct
15 Correct 45 ms 64248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 64256 KB Output is correct
2 Correct 87 ms 64248 KB Output is correct
3 Correct 96 ms 64328 KB Output is correct
4 Correct 164 ms 64256 KB Output is correct
5 Correct 92 ms 64376 KB Output is correct
6 Correct 212 ms 64256 KB Output is correct
7 Correct 86 ms 64332 KB Output is correct
8 Correct 100 ms 64256 KB Output is correct
9 Correct 57 ms 64248 KB Output is correct
10 Correct 94 ms 64248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 64380 KB Output is correct
2 Correct 46 ms 64248 KB Output is correct
3 Correct 44 ms 64248 KB Output is correct
4 Correct 46 ms 64248 KB Output is correct
5 Correct 45 ms 64256 KB Output is correct
6 Correct 46 ms 64248 KB Output is correct
7 Correct 47 ms 64248 KB Output is correct
8 Correct 55 ms 64248 KB Output is correct
9 Correct 45 ms 64248 KB Output is correct
10 Correct 46 ms 64248 KB Output is correct
11 Correct 46 ms 64256 KB Output is correct
12 Correct 46 ms 64248 KB Output is correct
13 Correct 47 ms 64248 KB Output is correct
14 Correct 46 ms 64248 KB Output is correct
15 Correct 47 ms 64248 KB Output is correct
16 Correct 49 ms 64256 KB Output is correct
17 Correct 45 ms 64248 KB Output is correct
18 Correct 45 ms 64248 KB Output is correct
19 Correct 46 ms 64256 KB Output is correct
20 Correct 47 ms 64248 KB Output is correct
21 Correct 46 ms 64376 KB Output is correct
22 Correct 46 ms 64248 KB Output is correct
23 Correct 45 ms 64248 KB Output is correct
24 Correct 101 ms 64256 KB Output is correct
25 Correct 87 ms 64248 KB Output is correct
26 Correct 96 ms 64328 KB Output is correct
27 Correct 164 ms 64256 KB Output is correct
28 Correct 92 ms 64376 KB Output is correct
29 Correct 212 ms 64256 KB Output is correct
30 Correct 86 ms 64332 KB Output is correct
31 Correct 100 ms 64256 KB Output is correct
32 Correct 57 ms 64248 KB Output is correct
33 Correct 94 ms 64248 KB Output is correct
34 Correct 177 ms 64224 KB Output is correct
35 Correct 127 ms 64352 KB Output is correct
36 Correct 140 ms 64320 KB Output is correct
37 Correct 171 ms 64256 KB Output is correct
38 Correct 86 ms 64324 KB Output is correct
39 Correct 176 ms 64256 KB Output is correct
40 Correct 80 ms 64276 KB Output is correct
41 Correct 165 ms 64248 KB Output is correct
42 Correct 188 ms 64248 KB Output is correct
43 Correct 165 ms 64328 KB Output is correct
44 Correct 177 ms 64348 KB Output is correct
45 Correct 103 ms 64248 KB Output is correct
46 Correct 150 ms 64232 KB Output is correct
47 Correct 163 ms 64248 KB Output is correct
48 Correct 142 ms 64324 KB Output is correct
49 Correct 52 ms 64332 KB Output is correct
50 Correct 180 ms 64248 KB Output is correct
51 Correct 128 ms 64328 KB Output is correct
52 Correct 140 ms 64328 KB Output is correct
53 Correct 205 ms 64360 KB Output is correct
54 Correct 121 ms 64328 KB Output is correct
55 Correct 201 ms 64268 KB Output is correct
56 Correct 191 ms 64248 KB Output is correct
57 Correct 82 ms 64392 KB Output is correct
58 Correct 176 ms 64224 KB Output is correct