Submission #723061

# Submission time Handle Problem Language Result Execution time Memory
723061 2023-04-13T08:04:18 Z loctildore Bi-ing Lottery Treekets (CCO22_day2problem1) C++14
12 / 25
1000 ms 43268 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define endl '\n'
#define all(x) begin(x), end(x)
#define MOD 1000000007
int n, k;
int sz[4069];
int balls[4069];
int lft[4069], rht[4069], dir[4069];
ll fact[4069];
bool done[4069][4069];
ll dp[4069][4069];
int calsz(int x) {
    if (!x) return 0;
    return sz[x] = calsz(lft[x]) + calsz(rht[x]) + 1 - balls[x];
}
ll expo(ll x, int c) {
    if (c == 1) {
        return x;
    }
    ll tmp = expo(x, c/2);
    tmp = (tmp * tmp) % MOD;
    return (tmp * (c % 2 ? x : 1)) % MOD;
}
ll perm(int x, int y) {
    ll rtn = fact[x];
    rtn = (rtn * expo(fact[x - y], MOD - 2)) % MOD;
    return rtn;
}
ll fnd(int x, int carry) {
    if (x == 0) return carry == 0;
    if (done[x][carry]) return dp[x][carry];
    done[x][carry] = true;
    if (carry > sz[x]) return dp[x][carry] = 0;
    if (carry == sz[x]) {
        ll tmp = (fnd(lft[x], sz[lft[x]]) * fnd(rht[x], sz[rht[x]])) % MOD;
        tmp = (tmp * perm(carry + balls[x], balls[x])) % MOD;
        return dp[x][carry] = tmp;
    }
    if (dir[x] && balls[x] + carry >= sz[rht[x]]) {
        ll tmp = (fnd(lft[x], balls[x] + carry - sz[rht[x]]) * fnd(rht[x], sz[rht[x]])) % MOD;
        tmp = (tmp * perm(carry + balls[x], balls[x])) % MOD;
        dp[x][carry] = (dp[x][carry] + tmp) % MOD;
    }
    if (!dir[x] && balls[x] + carry >= sz[lft[x]]) {
        ll tmp = (fnd(lft[x], sz[lft[x]]) * fnd(rht[x], balls[x] + carry - sz[lft[x]])) % MOD;
        tmp = (tmp * perm(carry + balls[x], balls[x])) % MOD;
        dp[x][carry] = (dp[x][carry] + tmp) % MOD;
    }
    for (int i = 0; i <= balls[x]; i++) {
        ll tmp;
        if (dir[x]) {
            if (sz[rht[x]] <= carry + i) continue;
            tmp = (fnd(lft[x], balls[x] - i) * fnd(rht[x], carry + i)) % MOD;
        }
        else {
            if (sz[lft[x]] <= carry + i) continue;
            tmp = (fnd(lft[x], carry + i) * fnd(rht[x], balls[x] - i)) % MOD;
        }
        tmp = (tmp * perm(balls[x], balls[x] - i)) % MOD;
        tmp = (tmp * perm(carry + i, i)) % MOD;
        dp[x][carry] = (dp[x][carry] + tmp) % MOD;
    }
    //cout<<x<<' '<<carry<<'?'<<sz[x]<<':'<<dp[x][carry]<<endl;
    return dp[x][carry];
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin>>n>>k;
    fact[0] = 1;
    for (int i = 1; i < 4069; i++) {
        fact[i] = (fact[i-1] * i) % MOD;
    }
    for (int i = 0; i < k; i++) {
        int a;
        cin>>a;
        balls[a]++;
    }
    for (int i = 1; i <= n; i++) {
        cin>>lft[i]>>rht[i];
        dir[rht[i]] = true;
    }
    calsz(1);
    cout<<fnd(1, 0)<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 440 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 444 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 32924 KB Output is correct
2 Execution timed out 1070 ms 43268 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 32596 KB Output is correct
2 Correct 19 ms 32920 KB Output is correct
3 Correct 19 ms 32588 KB Output is correct
4 Correct 24 ms 32612 KB Output is correct
5 Correct 21 ms 32584 KB Output is correct
6 Correct 21 ms 32664 KB Output is correct
7 Correct 19 ms 32664 KB Output is correct
8 Correct 21 ms 32588 KB Output is correct
9 Correct 23 ms 32596 KB Output is correct
10 Correct 27 ms 32776 KB Output is correct
11 Correct 20 ms 32672 KB Output is correct
12 Correct 55 ms 33204 KB Output is correct
13 Correct 18 ms 32712 KB Output is correct
14 Correct 26 ms 32556 KB Output is correct
15 Correct 22 ms 32608 KB Output is correct
16 Correct 22 ms 32612 KB Output is correct
17 Correct 18 ms 32584 KB Output is correct
18 Correct 26 ms 32688 KB Output is correct
19 Correct 22 ms 32596 KB Output is correct
20 Correct 13 ms 16476 KB Output is correct
21 Correct 20 ms 30956 KB Output is correct
22 Correct 23 ms 32288 KB Output is correct
23 Correct 24 ms 31656 KB Output is correct
24 Correct 20 ms 32596 KB Output is correct
25 Correct 20 ms 29132 KB Output is correct
26 Correct 18 ms 32340 KB Output is correct
27 Correct 20 ms 31040 KB Output is correct
28 Correct 19 ms 32052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 440 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 444 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 24 ms 32924 KB Output is correct
21 Execution timed out 1070 ms 43268 KB Time limit exceeded
22 Halted 0 ms 0 KB -