답안 #238656

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
238656 2020-06-12T09:18:11 Z Kastanda Ljetopica (COI19_ljetopica) C++11
100 / 100
28 ms 18176 KB
// Criminal
#include<bits/stdc++.h>
using namespace std;
const int N = 1009, Mod = 1e9 + 7;
int n, k, Pw[N], C[N][N];
pair < int , int > dp[2][N][N];
string A, B, D, E;
inline int Diff(string s, string d, int l = n - 1)
{
    int Cnt = 0;
    for (int i = 0; i < l; i ++)
        Cnt += (d[i] != s[i]) ^ (Cnt & 1);
    return Cnt;
}
inline int Get(string s, int l)
{
    int df1 = Diff(s, D, l);
    int df2 = Diff(s, E, l);
    int tot = 0;
    if (df1 <= k)
    {
        for (int i = 0; i < l; i ++)
            if (s[i] == '1')
                tot = (tot + Pw[n - 2 - i] * 1LL * C[k - df1][n - 1 - l]) % Mod;
        tot = (tot + Pw[n - 1] * 1LL * C[k - df1][n - 1 - l]) % Mod;
        tot = (tot + dp[0][l][k - df1].first) % Mod;
    }
    if (df2 <= k)
    {
        for (int i = 0; i < l; i ++)
            if (s[i] == '1')
                tot = (tot + Pw[n - 2 - i] * 1LL * C[k - df2][n - 1 - l]) % Mod;
        tot = (tot + Pw[n - 1] * 1LL * C[k - df2][n - 1 - l]) % Mod;
        tot = (tot + dp[1][l][k - df2].first) % Mod;
    }
    return (tot);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> k >> D >> A >> B;
    A = A.substr(1); B = B.substr(1); E = D;
    for (int i = 0; i < (int)D.size(); i ++)
        D[i] = (D[i] == 'L' ? '0' : '1'),
        E[i] = (E[i] == 'R' ? '0' : '1');

    for (int i = Pw[0] = 1; i < N; i ++)
        Pw[i] = Pw[i - 1] * 2LL % Mod;
    for (int i = 0; i < N; i ++)
        for (int j = C[0][i] = C[i][i] = 1; j < i; j ++)
            C[j][i] = (C[j][i - 1] + C[j - 1][i - 1]) % Mod;

    for (int w = 0; w < 2; w ++)
    {
        string d = w ? E : D;
        dp[w][n - 1][0] = {0, 1};
        for (int i = n - 2; ~ i; i --)
            for (int j = 0; i + j <= n - 1 && j <= k; j ++)
            {
                dp[w][i][j].first = dp[w][i + 1][j].first;
                dp[w][i][j].second = dp[w][i + 1][j].second;
                if (j)
                {
                    dp[w][i][j].first = (dp[w][i][j].first + dp[w][i + 1][j - 1].first) % Mod;
                    dp[w][i][j].second = (dp[w][i][j].second + dp[w][i + 1][j - 1].second) % Mod;
                }
                if ((d[i] - '0') ^ (k - j & 1))
                    dp[w][i][j].first = (dp[w][i][j].first + Pw[n - 2 - i] * 1LL * dp[w][i + 1][j].second) % Mod;
                else if (j)
                    dp[w][i][j].first = (dp[w][i][j].first + Pw[n - 2 - i] * 1LL * dp[w][i + 1][j - 1].second) % Mod;
            }
    }

    if (A == B)
    {
        if (Diff(A, D) != k && Diff(A, E) != k)
            return !printf("0\n");
        int tot = 0;
        for (int i = 0; i < n - 1; i ++)
            if (A[i] == '1')
                tot = (tot + Pw[n - 1 - i - 1]) % Mod;
        tot = (tot + Pw[n - 1]) % Mod;
        return !printf("%d\n", tot);
    }
    int i = 0;
    while (A[i] == B[i])
        i ++;
    assert(A[i] == '0');
    int tot = 0;
    if (Diff(A, D) == k || Diff(A, E) == k)
    {
        for (int i = 0; i < n - 1; i ++)
            if (A[i] == '1')
                tot = (tot + Pw[n - 1 - i - 1]) % Mod;
        tot = (tot + Pw[n - 1]) % Mod;
    }
    if (Diff(B, D) == k || Diff(B, E) == k)
    {
        for (int i = 0; i < n - 1; i ++)
            if (B[i] == '1')
                tot = (tot + Pw[n - 1 - i - 1]) % Mod;
        tot = (tot + Pw[n - 1]) % Mod;
    }
    i ++;
    while (i < n - 1)
    {
        if (A[i] == '0')
        {
            string s = A;
            s[i] = '1';
            tot = (tot + Get(s, i + 1)) % Mod;
        }
        if (B[i] == '1')
        {

            string s = B;
            s[i] = '0';
            tot = (tot + Get(s, i + 1)) % Mod;
        }
        i ++;
    }
    return !printf("%d\n", tot);
}

Compilation message

ljetopica.cpp: In function 'int main()':
ljetopica.cpp:68:39: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
                 if ((d[i] - '0') ^ (k - j & 1))
                                     ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12416 KB Output is correct
2 Correct 11 ms 11776 KB Output is correct
3 Correct 11 ms 11520 KB Output is correct
4 Correct 10 ms 11136 KB Output is correct
5 Correct 11 ms 10752 KB Output is correct
6 Correct 10 ms 10368 KB Output is correct
7 Correct 11 ms 9984 KB Output is correct
8 Correct 11 ms 9600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4480 KB Output is correct
2 Correct 8 ms 4528 KB Output is correct
3 Correct 7 ms 4480 KB Output is correct
4 Correct 7 ms 4480 KB Output is correct
5 Correct 7 ms 4480 KB Output is correct
6 Correct 8 ms 4608 KB Output is correct
7 Correct 8 ms 4480 KB Output is correct
8 Correct 7 ms 4480 KB Output is correct
9 Correct 8 ms 4480 KB Output is correct
10 Correct 7 ms 4480 KB Output is correct
11 Correct 7 ms 4480 KB Output is correct
12 Correct 7 ms 4480 KB Output is correct
13 Correct 7 ms 4480 KB Output is correct
14 Correct 8 ms 4480 KB Output is correct
15 Correct 8 ms 4608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 18176 KB Output is correct
2 Correct 27 ms 17272 KB Output is correct
3 Correct 27 ms 17792 KB Output is correct
4 Correct 26 ms 18176 KB Output is correct
5 Correct 24 ms 17024 KB Output is correct
6 Correct 26 ms 18176 KB Output is correct
7 Correct 18 ms 15488 KB Output is correct
8 Correct 26 ms 17792 KB Output is correct
9 Correct 13 ms 12672 KB Output is correct
10 Correct 25 ms 17408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12416 KB Output is correct
2 Correct 11 ms 11776 KB Output is correct
3 Correct 11 ms 11520 KB Output is correct
4 Correct 10 ms 11136 KB Output is correct
5 Correct 11 ms 10752 KB Output is correct
6 Correct 10 ms 10368 KB Output is correct
7 Correct 11 ms 9984 KB Output is correct
8 Correct 11 ms 9600 KB Output is correct
9 Correct 8 ms 4480 KB Output is correct
10 Correct 8 ms 4528 KB Output is correct
11 Correct 7 ms 4480 KB Output is correct
12 Correct 7 ms 4480 KB Output is correct
13 Correct 7 ms 4480 KB Output is correct
14 Correct 8 ms 4608 KB Output is correct
15 Correct 8 ms 4480 KB Output is correct
16 Correct 7 ms 4480 KB Output is correct
17 Correct 8 ms 4480 KB Output is correct
18 Correct 7 ms 4480 KB Output is correct
19 Correct 7 ms 4480 KB Output is correct
20 Correct 7 ms 4480 KB Output is correct
21 Correct 7 ms 4480 KB Output is correct
22 Correct 8 ms 4480 KB Output is correct
23 Correct 8 ms 4608 KB Output is correct
24 Correct 28 ms 18176 KB Output is correct
25 Correct 27 ms 17272 KB Output is correct
26 Correct 27 ms 17792 KB Output is correct
27 Correct 26 ms 18176 KB Output is correct
28 Correct 24 ms 17024 KB Output is correct
29 Correct 26 ms 18176 KB Output is correct
30 Correct 18 ms 15488 KB Output is correct
31 Correct 26 ms 17792 KB Output is correct
32 Correct 13 ms 12672 KB Output is correct
33 Correct 25 ms 17408 KB Output is correct
34 Correct 25 ms 18048 KB Output is correct
35 Correct 19 ms 14848 KB Output is correct
36 Correct 21 ms 16640 KB Output is correct
37 Correct 20 ms 17408 KB Output is correct
38 Correct 18 ms 13312 KB Output is correct
39 Correct 23 ms 16768 KB Output is correct
40 Correct 16 ms 13184 KB Output is correct
41 Correct 23 ms 17920 KB Output is correct
42 Correct 25 ms 18128 KB Output is correct
43 Correct 24 ms 17408 KB Output is correct
44 Correct 24 ms 16640 KB Output is correct
45 Correct 19 ms 14848 KB Output is correct
46 Correct 23 ms 17016 KB Output is correct
47 Correct 23 ms 17152 KB Output is correct
48 Correct 21 ms 16256 KB Output is correct
49 Correct 15 ms 12032 KB Output is correct
50 Correct 24 ms 17792 KB Output is correct
51 Correct 21 ms 16128 KB Output is correct
52 Correct 22 ms 16640 KB Output is correct
53 Correct 26 ms 18176 KB Output is correct
54 Correct 20 ms 15616 KB Output is correct
55 Correct 25 ms 18176 KB Output is correct
56 Correct 26 ms 18176 KB Output is correct
57 Correct 17 ms 13568 KB Output is correct
58 Correct 25 ms 18176 KB Output is correct