Submission #810905

# Submission time Handle Problem Language Result Execution time Memory
810905 2023-08-06T17:19:43 Z jophyyjh Digital Circuit (IOI22_circuit) C++17
100 / 100
1036 ms 32700 KB
/**
 * I nearly fully-solved this problem in IOI 2022, though I ran out of time. Initially, I
 * was only hoping to score more partials, but it turned out to be a full solution.
 * 
 * For [S1-3], DFS + dp suffice: for each node u in the tree, we compute dp1[u], the num of
 * different assignments of parameters to threshold gates in the subtree of u that will
 * result in u being 1. Similarly, we can define dp0[u].
 * Let v_1, v_2, ..., v_k be all the children of u. Mathematically,
 *      dp1[u] = (dp1[v_1] * dp1[v_2] * ... * dp1[v_k]) * k      <-- k options for u
 *                      -->   + (dp1[v_1] * ... * dp1[v_{k-1}] * dp0[v_k])
 *  (k-1) options for u -->   +  ...
 *                      -->   +  dp0[v_1] * dp1[v_2] * ... * dp1[v_k]) * (k-1)
 *              + ...,
 * and dp0[u] can be found from dp1[u]. 
 * 
 * The huge sum can be computed quite efficiently, again with dp. For each node u, let
 * partial[i] (0 <= i <= k) be
 *    sum{ (dp0[v_1] or dp1[v_1]) * ... * (dp0[v_k] or dp1[v_k]) : exactly i dp1[]'s }.
 * We can find each partial[i] iteratively:
 *         partial[i] <-- partial[i] * dp0[some_v] + partial[i-1] * dp1[some_v].
 * Therefore, dp1[u] = partial[1] * 1 + ... + partial[k] * k. So, O((N+M)^2 * Q). [S4] can
 * be solved too, because we only have to update O(log(N)) dp1[]'s and dp0[]'s in each
 * query.
 * 
 * I thought there must be ways to optimize this dp. Because the source gates can receive
 * arbitrary input, I tried out some examples; for each one of them, I use a variable to
 * denote each source gate and try to find the expression for each gate on paper. I noticed
 * a pattern, but didn't bother to prove it in the contest. Anyway, let's begin.
 * Let f(x) = (dp1[v_1] * x + dp0[v_1])...(dp1[v_k] * x + dp0[v_k]), then
 * partial[i] = coefficient of x^i in f(x). Write f(x) as c_0 + c_1 * x + ... + c_k * x^k.
 * We have
 *          f'(x) = 1 * c_1 + 2 * c_2 * x + 3 * c_3 * x^2 + ... + k * c_k * x^{k-1},
 * which means
 *      dp[u] = c_1 + 2 * c_2 + ... + k * c_k
 *            = f'(1)
 *            = sum{ dp1[v_i] * prod{ (dp1[v_j] + dp0[v_j]) : j != i }  : 1 <= i <= k }
 *            = sum{ dp[v_i] * prod{ sub[v_j] : j != i } }.
 * Here, sub[v] is the num of comb of freely choosing all thresholds in the subtree of v.
 * This quantity can be easily found with DFS. The trick is pretty well-known, at least
 * in the Olympiad maths circle.
 * 
 * After doing some more examples on my scratch paper, I noticed that the formula we
 * obtained above implies that dp[1] is a linear combination of
 * dp[N], dp[N+1], ..., dp[N+M-1], i.e. the inputs. Nice! It remains to compute the
 * coefficient of each input in the final linear comb of dp[1], and use
 * a segment tree with lazy propagation.
 * 
 * Overall, I'd say this is a pretty interesting problem, at least with the parts of dp & 
 * maths. The portion of handling queries with seg tree + lazy is definitely a bit
 * mechanical, but still tests a contestant's data structure skills. Note that the MOD is
 * not a prime! Therefore, dp[u] cannot be sum{ dp[v_i] / sub[v_i] } * prod{ sub[v_i] },
 * cuz a multiplicative inverse may not exist under mod 1000002022. The issue can be
 * resolved by computing the suffix product and prefix product of sub[].
 * 
 * Time Complexity: O(N + (M + Q) * log(M))         (Full solution)
 * Implementation 1                 (DP, maths, segment tree, lazy propagation)
*/

#include <bits/stdc++.h>
#include "circuit.h"

typedef long long   ll;
typedef std::vector<int>  vec;

const ll MOD = 1000002022;


// Our toggleable linear combination segment tree (using lazy propagation) (lol)
class TogLincomb {
private:
    int m;
    std::vector<ll> pre_coeff;
    std::vector<ll> tree;
    std::vector<bool> lazy;     // whether the range has been lazily toggled
    inline void flip(int k, int i, int j) {
        tree[k] = (pre_coeff[j + 1] - pre_coeff[i]) - tree[k], lazy[k] = 1 - lazy[k];
        tree[k] = (tree[k] % MOD + MOD) % MOD;
    }
    inline void push(int k, int i, int j) {
        if (lazy[k]) {
            int mid = (i + j) / 2;
            flip(2 * k, i, mid);
            flip(2 * k + 1, mid + 1, j);
        }
        lazy[k] = false;
    }
    void _toggle(int l, int r, int k, int i, int j) {
        if (i > r || l > j || l > r)
            return;
        if (i == l && j == r) {
            flip(k, i, j);
            return;
        }
        int mid = (i + j) / 2;
        push(k, i, j);
        _toggle(l, std::min(r, mid), 2 * k, i, mid);
        _toggle(std::max(l, mid + 1), r, 2 * k + 1, mid + 1, j);
        tree[k] = tree[2 * k] + tree[2 * k + 1], tree[k] %= MOD;
    }
public:
    TogLincomb()    {}
    TogLincomb(int size, const std::vector<ll>& coeff) {
        m = size;
        pre_coeff.assign(m + 1, 0);
        for (int k = 0; k < m; k++)
            pre_coeff[k + 1] = pre_coeff[k] + coeff[k], pre_coeff[k + 1] %= MOD;
        tree.assign(4 * m, 0);
        lazy.assign(4 * m, false);
    }
    void toggle(int l, int r)   { _toggle(l, r, 1, 0, m - 1); }
    ll sum()    { return tree[1]; }
};

int n;
std::vector<vec> tree;
std::vector<ll> dp, sub;
TogLincomb seg_tree;

void pre_comp(int k) {
    sub[k] = 1;
    if (k >= n)
        return;
    for (int child : tree[k]) {
        pre_comp(child);
        sub[k] *= sub[child], sub[k] %= MOD;
    }
    sub[k] *= int(tree[k].size()), sub[k] %= MOD;
}

void compute(int k) {
    int c = tree[k].size();
    std::vector<ll> prefix(c + 1), suffix(c + 1);
    prefix[0] = suffix[c] = 1;
    for (int i = 0, j = c - 1; i < c; i++, j--) {
        prefix[i + 1] = prefix[i] * sub[tree[k][i]] % MOD;
        suffix[j] = suffix[j + 1] * sub[tree[k][j]] % MOD;
    }
    for (int i = 0; i < c; i++) {
        int child = tree[k][i];
        dp[child] = dp[k] * prefix[i] % MOD * suffix[i + 1] % MOD;
        compute(child);
    }
}

void init(int N, int m, vec P, vec A) {
    n = N;
    tree.assign(n + m, vec());
    for (int k = 1; k < n + m; k++)
        tree[P[k]].push_back(k);
    
    sub.resize(n + m);
    pre_comp(0);
    dp.resize(n + m);
    dp[0] = 1;
    compute(0);
    seg_tree = TogLincomb(m, std::vector<ll>(dp.begin() + n, dp.end()));
    for (int k = 0; k < m; k++) {
        if (A[k])
            seg_tree.toggle(k, k);
    }
}

int count_ways(int l, int r) {
    seg_tree.toggle(l - n, r - n);
    return int(seg_tree.sum());
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 276 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 542 ms 6204 KB Output is correct
2 Correct 706 ms 12104 KB Output is correct
3 Correct 672 ms 12124 KB Output is correct
4 Correct 613 ms 12056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 542 ms 6204 KB Output is correct
2 Correct 706 ms 12104 KB Output is correct
3 Correct 672 ms 12124 KB Output is correct
4 Correct 613 ms 12056 KB Output is correct
5 Correct 626 ms 6176 KB Output is correct
6 Correct 785 ms 12100 KB Output is correct
7 Correct 636 ms 12104 KB Output is correct
8 Correct 826 ms 12104 KB Output is correct
9 Correct 332 ms 592 KB Output is correct
10 Correct 643 ms 1032 KB Output is correct
11 Correct 770 ms 976 KB Output is correct
12 Correct 560 ms 1028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 542 ms 6204 KB Output is correct
14 Correct 706 ms 12104 KB Output is correct
15 Correct 672 ms 12124 KB Output is correct
16 Correct 613 ms 12056 KB Output is correct
17 Correct 626 ms 6176 KB Output is correct
18 Correct 785 ms 12100 KB Output is correct
19 Correct 636 ms 12104 KB Output is correct
20 Correct 826 ms 12104 KB Output is correct
21 Correct 332 ms 592 KB Output is correct
22 Correct 643 ms 1032 KB Output is correct
23 Correct 770 ms 976 KB Output is correct
24 Correct 560 ms 1028 KB Output is correct
25 Correct 802 ms 17972 KB Output is correct
26 Correct 764 ms 18324 KB Output is correct
27 Correct 849 ms 18344 KB Output is correct
28 Correct 583 ms 18336 KB Output is correct
29 Correct 697 ms 30748 KB Output is correct
30 Correct 594 ms 30744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 276 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 605 ms 812 KB Output is correct
44 Correct 743 ms 868 KB Output is correct
45 Correct 764 ms 848 KB Output is correct
46 Correct 542 ms 1208 KB Output is correct
47 Correct 715 ms 1220 KB Output is correct
48 Correct 731 ms 1196 KB Output is correct
49 Correct 719 ms 1200 KB Output is correct
50 Correct 795 ms 1204 KB Output is correct
51 Correct 548 ms 848 KB Output is correct
52 Correct 645 ms 848 KB Output is correct
53 Correct 482 ms 1488 KB Output is correct
54 Correct 688 ms 1200 KB Output is correct
55 Correct 836 ms 1004 KB Output is correct
56 Correct 736 ms 944 KB Output is correct
57 Correct 599 ms 804 KB Output is correct
58 Correct 774 ms 1744 KB Output is correct
59 Correct 790 ms 1872 KB Output is correct
60 Correct 686 ms 1872 KB Output is correct
61 Correct 755 ms 1020 KB Output is correct
62 Correct 695 ms 816 KB Output is correct
63 Correct 677 ms 784 KB Output is correct
64 Correct 722 ms 804 KB Output is correct
65 Correct 332 ms 592 KB Output is correct
66 Correct 793 ms 1036 KB Output is correct
67 Correct 626 ms 1032 KB Output is correct
68 Correct 576 ms 1032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 276 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 542 ms 6204 KB Output is correct
44 Correct 706 ms 12104 KB Output is correct
45 Correct 672 ms 12124 KB Output is correct
46 Correct 613 ms 12056 KB Output is correct
47 Correct 626 ms 6176 KB Output is correct
48 Correct 785 ms 12100 KB Output is correct
49 Correct 636 ms 12104 KB Output is correct
50 Correct 826 ms 12104 KB Output is correct
51 Correct 332 ms 592 KB Output is correct
52 Correct 643 ms 1032 KB Output is correct
53 Correct 770 ms 976 KB Output is correct
54 Correct 560 ms 1028 KB Output is correct
55 Correct 802 ms 17972 KB Output is correct
56 Correct 764 ms 18324 KB Output is correct
57 Correct 849 ms 18344 KB Output is correct
58 Correct 583 ms 18336 KB Output is correct
59 Correct 697 ms 30748 KB Output is correct
60 Correct 594 ms 30744 KB Output is correct
61 Correct 605 ms 812 KB Output is correct
62 Correct 743 ms 868 KB Output is correct
63 Correct 764 ms 848 KB Output is correct
64 Correct 542 ms 1208 KB Output is correct
65 Correct 715 ms 1220 KB Output is correct
66 Correct 731 ms 1196 KB Output is correct
67 Correct 719 ms 1200 KB Output is correct
68 Correct 795 ms 1204 KB Output is correct
69 Correct 548 ms 848 KB Output is correct
70 Correct 645 ms 848 KB Output is correct
71 Correct 482 ms 1488 KB Output is correct
72 Correct 688 ms 1200 KB Output is correct
73 Correct 836 ms 1004 KB Output is correct
74 Correct 736 ms 944 KB Output is correct
75 Correct 599 ms 804 KB Output is correct
76 Correct 774 ms 1744 KB Output is correct
77 Correct 790 ms 1872 KB Output is correct
78 Correct 686 ms 1872 KB Output is correct
79 Correct 755 ms 1020 KB Output is correct
80 Correct 695 ms 816 KB Output is correct
81 Correct 677 ms 784 KB Output is correct
82 Correct 722 ms 804 KB Output is correct
83 Correct 332 ms 592 KB Output is correct
84 Correct 793 ms 1036 KB Output is correct
85 Correct 626 ms 1032 KB Output is correct
86 Correct 576 ms 1032 KB Output is correct
87 Correct 1 ms 208 KB Output is correct
88 Correct 425 ms 16036 KB Output is correct
89 Correct 740 ms 11464 KB Output is correct
90 Correct 731 ms 11340 KB Output is correct
91 Correct 631 ms 18472 KB Output is correct
92 Correct 837 ms 18436 KB Output is correct
93 Correct 819 ms 18376 KB Output is correct
94 Correct 790 ms 18396 KB Output is correct
95 Correct 823 ms 18452 KB Output is correct
96 Correct 699 ms 10912 KB Output is correct
97 Correct 630 ms 10900 KB Output is correct
98 Correct 687 ms 25296 KB Output is correct
99 Correct 807 ms 18304 KB Output is correct
100 Correct 867 ms 14328 KB Output is correct
101 Correct 939 ms 13060 KB Output is correct
102 Correct 710 ms 10908 KB Output is correct
103 Correct 833 ms 30744 KB Output is correct
104 Correct 1036 ms 32692 KB Output is correct
105 Correct 932 ms 32700 KB Output is correct
106 Correct 758 ms 13716 KB Output is correct
107 Correct 716 ms 10312 KB Output is correct
108 Correct 816 ms 10620 KB Output is correct
109 Correct 720 ms 11080 KB Output is correct