Submission #723111

# Submission time Handle Problem Language Result Execution time Memory
723111 2023-04-13T08:49:34 Z 0__0 Bi-ing Lottery Treekets (CCO22_day2problem1) C++17
25 / 25
192 ms 80460 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

#define int unsigned long long

int loc[10000];
int lpar[10000], rpar[10000];
int dp[10000][10000]; // at node i, need j balls from above
int above[10000]; //exclusive
int below[10000]; //inclusive
int sz[10000];

const int MOD = 1e9 + 7;

int factorial[10000];
int inverse[10000];
int inversef[10000];

int choose(int a, int b) {
    if (a < b) return 0LL;
    return ((factorial[a] % MOD * inversef[b] % MOD) % MOD) * (inversef[a - b]) % MOD;
}

void dfs(int node, int par = -1, bool left = true) {

    below[node] = loc[node];
    sz[node] = 1;

    if (lpar[node]) {
        above[lpar[node]] = above[node] + loc[node];
        dfs(lpar[node], node, true);
        below[node] += below[lpar[node]];
        sz[node] += sz[lpar[node]];
    }
    if (rpar[node]) {
        above[rpar[node]] = above[node] + loc[node];
        dfs(rpar[node], node, false);
        below[node] += below[rpar[node]];
        sz[node] += sz[rpar[node]];
    }

    if (sz[node] < below[node]) {
        cout << 0;
        exit(0);
    }

    int lc = lpar[node];
    int rc = rpar[node];
    int leftsz = sz[lc] - below[lc];
    int rightsz = sz[rc] - below[rc];
    int lefttaken = below[lc];
    int righttaken = below[rc];
    int space = sz[node] - lefttaken - righttaken;

    if (!left) {
        swap(lc, rc);
        swap(leftsz, rightsz);
        swap(lefttaken, righttaken);
    }

    // the root is somewhere else
    {
        for (int take = 0; take <= leftsz && take <= loc[node]; take++) {
            int takeo = loc[node] - take;

            if (takeo > rightsz) continue;

            for (int i = 0; i <= above[node] && i + loc[node] <= space; i++) {
                int amtleft = min(leftsz, take + i);
                int remain = take + i - amtleft;
                int amtright = min(rightsz, remain + takeo);

                if (amtleft + amtright < loc[node]) continue;

                int constant = 1;

                if (loc[node] + i == amtleft + amtright + 1) {
                    constant = choose(loc[node], take) % MOD;
                    constant = (constant * choose(i, amtleft - take) % MOD * choose(i - (amtleft - take), 1));
                } else {
                    constant = choose(loc[node], take) % MOD;
                    constant = (constant % MOD * choose(i, amtleft - take));
                }

                dp[node][i] %= MOD;
                int temp = dp[lc][amtleft] * dp[rc][amtright];
                temp = temp % MOD;
                temp = (constant % MOD) * temp;
                temp = temp % MOD;
                dp[node][i] = (dp[node][i] + temp) % MOD;
                if (dp[node][i] < 0) {
                    cout <<  dp[node][i] << " " << dp[lc][amtleft] << " " << dp[rc][amtright] << " " << constant;
                    exit(0);
                }
            }
        }
    }

    // take root with itself
    {
        for (int take = 0; take <= leftsz && take < loc[node]; take++) {
            int takeo = loc[node] - take - 1;
            if (takeo > rightsz) continue;

            for (int i = 0; i <= above[node] && i + loc[node] <= space; i++) {

                int amtleft = min(leftsz, take + i);
                int remain = take + i - amtleft;
                int amtright = min(rightsz, remain + takeo);

                if (amtleft + amtright + 1 < loc[node]) continue;

                if (i + loc[node] != space) {
                    continue;
                }

                int constant;

                if (loc[node] + i == amtleft + amtright + 1) {
                    constant = (choose(loc[node], take) * choose(loc[node] - take, takeo)) % MOD;
                    constant = (constant * choose(i, amtleft - take)) % MOD;
                } else {
                    constant = choose(loc[node], take) % MOD;
                    constant = (constant * choose(i, amtleft - take));
                }
                dp[node][i] %= MOD;
                int temp = dp[lc][amtleft] * dp[rc][amtright];
                temp = temp % MOD;
                temp = (constant % MOD) * temp;
                temp = temp % MOD;
                dp[node][i] = (dp[node][i] + temp) % MOD;
                if (dp[node][i] < 0) {
                    cout <<  dp[node][i] << " " << dp[lc][amtleft] << " " << dp[rc][amtright] << " " << constant;
                    exit(0);
                }
            }
        }
    }
}

int par[10005];

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("input.txt", "r", stdin);
//    freopen("", "w", stdout);
    int n, k; cin >> n >> k;
    factorial[0] = 1;
    for (int i = 1; i <= 9000; i++) {
        factorial[i] = (factorial[i - 1] * i) % MOD;
    }

    inverse[0] = 1;
    inverse[1] = 1;
    for (int i = 2; i <= 9000; i++) {
        inverse[i] = MOD - (MOD / i) * inverse[MOD % i] % MOD;
    }

    inversef[0] = 1;
    for (int i = 1; i <= 9000; i++) {
        inversef[i] = (inversef[i-1] * inverse[i]) % MOD;
    }

    if (k > n) {
        cout << "0\n";
        exit(0);
    }
    for (int i = 1; i <= k; i++) {
        int t; cin >> t;
        loc[t]++;
    }
    for (int i = 1; i <= n; i++) {
        cin >> lpar[i] >> rpar[i];
        par[lpar[i]] = par[rpar[i]] = i;
//        cout << "IAM " << i << endl;
    }

    assert(par[1] == 0);

    dp[0][0] = 1;
    dfs(1);

    cout << dp[1][0] % MOD << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 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 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 18004 KB Output is correct
2 Correct 55 ms 32844 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 1420 KB Output is correct
5 Correct 2 ms 1544 KB Output is correct
6 Correct 11 ms 18004 KB Output is correct
7 Correct 38 ms 26416 KB Output is correct
8 Correct 17 ms 19468 KB Output is correct
9 Correct 35 ms 24440 KB Output is correct
10 Correct 20 ms 20692 KB Output is correct
11 Correct 52 ms 31820 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 35 ms 24524 KB Output is correct
14 Correct 53 ms 29648 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 13 ms 18004 KB Output is correct
17 Correct 35 ms 23948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 17324 KB Output is correct
2 Correct 11 ms 18004 KB Output is correct
3 Correct 12 ms 17312 KB Output is correct
4 Correct 14 ms 17364 KB Output is correct
5 Correct 12 ms 17404 KB Output is correct
6 Correct 16 ms 17364 KB Output is correct
7 Correct 12 ms 17364 KB Output is correct
8 Correct 12 ms 17408 KB Output is correct
9 Correct 12 ms 17364 KB Output is correct
10 Correct 13 ms 17692 KB Output is correct
11 Correct 15 ms 17464 KB Output is correct
12 Correct 19 ms 19600 KB Output is correct
13 Correct 12 ms 17620 KB Output is correct
14 Correct 12 ms 17316 KB Output is correct
15 Correct 12 ms 17356 KB Output is correct
16 Correct 12 ms 17364 KB Output is correct
17 Correct 12 ms 17364 KB Output is correct
18 Correct 14 ms 17620 KB Output is correct
19 Correct 14 ms 17484 KB Output is correct
20 Correct 7 ms 8916 KB Output is correct
21 Correct 15 ms 16516 KB Output is correct
22 Correct 12 ms 17236 KB Output is correct
23 Correct 12 ms 16852 KB Output is correct
24 Correct 13 ms 17376 KB Output is correct
25 Correct 11 ms 15572 KB Output is correct
26 Correct 12 ms 17244 KB Output is correct
27 Correct 13 ms 16596 KB Output is correct
28 Correct 13 ms 17108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 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 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 14 ms 18004 KB Output is correct
21 Correct 55 ms 32844 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 1420 KB Output is correct
24 Correct 2 ms 1544 KB Output is correct
25 Correct 11 ms 18004 KB Output is correct
26 Correct 38 ms 26416 KB Output is correct
27 Correct 17 ms 19468 KB Output is correct
28 Correct 35 ms 24440 KB Output is correct
29 Correct 20 ms 20692 KB Output is correct
30 Correct 52 ms 31820 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 35 ms 24524 KB Output is correct
33 Correct 53 ms 29648 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 13 ms 18004 KB Output is correct
36 Correct 35 ms 23948 KB Output is correct
37 Correct 12 ms 17324 KB Output is correct
38 Correct 11 ms 18004 KB Output is correct
39 Correct 12 ms 17312 KB Output is correct
40 Correct 14 ms 17364 KB Output is correct
41 Correct 12 ms 17404 KB Output is correct
42 Correct 16 ms 17364 KB Output is correct
43 Correct 12 ms 17364 KB Output is correct
44 Correct 12 ms 17408 KB Output is correct
45 Correct 12 ms 17364 KB Output is correct
46 Correct 13 ms 17692 KB Output is correct
47 Correct 15 ms 17464 KB Output is correct
48 Correct 19 ms 19600 KB Output is correct
49 Correct 12 ms 17620 KB Output is correct
50 Correct 12 ms 17316 KB Output is correct
51 Correct 12 ms 17356 KB Output is correct
52 Correct 12 ms 17364 KB Output is correct
53 Correct 12 ms 17364 KB Output is correct
54 Correct 14 ms 17620 KB Output is correct
55 Correct 14 ms 17484 KB Output is correct
56 Correct 7 ms 8916 KB Output is correct
57 Correct 15 ms 16516 KB Output is correct
58 Correct 12 ms 17236 KB Output is correct
59 Correct 12 ms 16852 KB Output is correct
60 Correct 13 ms 17376 KB Output is correct
61 Correct 11 ms 15572 KB Output is correct
62 Correct 12 ms 17244 KB Output is correct
63 Correct 13 ms 16596 KB Output is correct
64 Correct 13 ms 17108 KB Output is correct
65 Correct 14 ms 15672 KB Output is correct
66 Correct 14 ms 17364 KB Output is correct
67 Correct 14 ms 16076 KB Output is correct
68 Correct 15 ms 17492 KB Output is correct
69 Correct 15 ms 17492 KB Output is correct
70 Correct 29 ms 17492 KB Output is correct
71 Correct 16 ms 17484 KB Output is correct
72 Correct 30 ms 17532 KB Output is correct
73 Correct 15 ms 17368 KB Output is correct
74 Correct 2 ms 724 KB Output is correct
75 Correct 12 ms 17316 KB Output is correct
76 Correct 192 ms 80460 KB Output is correct
77 Correct 186 ms 80196 KB Output is correct
78 Correct 2 ms 1876 KB Output is correct
79 Correct 60 ms 33704 KB Output is correct
80 Correct 12 ms 18004 KB Output is correct
81 Correct 3 ms 1364 KB Output is correct
82 Correct 16 ms 17364 KB Output is correct
83 Correct 12 ms 17364 KB Output is correct
84 Correct 1 ms 468 KB Output is correct
85 Correct 2 ms 724 KB Output is correct
86 Correct 2 ms 724 KB Output is correct
87 Correct 12 ms 17332 KB Output is correct
88 Correct 12 ms 17320 KB Output is correct
89 Correct 12 ms 17400 KB Output is correct
90 Correct 11 ms 17364 KB Output is correct
91 Correct 12 ms 17364 KB Output is correct
92 Correct 12 ms 17468 KB Output is correct
93 Correct 13 ms 17620 KB Output is correct
94 Correct 20 ms 19808 KB Output is correct
95 Correct 21 ms 20264 KB Output is correct
96 Correct 12 ms 17404 KB Output is correct
97 Correct 12 ms 17296 KB Output is correct
98 Correct 12 ms 17364 KB Output is correct
99 Correct 13 ms 17364 KB Output is correct
100 Correct 13 ms 17520 KB Output is correct
101 Correct 13 ms 17620 KB Output is correct
102 Correct 9 ms 10580 KB Output is correct
103 Correct 11 ms 16212 KB Output is correct
104 Correct 11 ms 16644 KB Output is correct