답안 #692510

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
692510 2023-02-01T17:06:25 Z CatalinT 디지털 회로 (IOI22_circuit) C++17
46 / 100
3000 ms 3664 KB
#include "circuit.h"

using namespace std;

#include <iostream>
#include <vector>
#include <cassert>

template <std::uint32_t mod>
class modint {
private:
    std::uint32_t n;
public:
    modint() : n(0) {};
    modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {};
    static constexpr std::uint32_t get_mod() { return mod; }
    std::uint32_t get() const { return n; }
    bool operator==(const modint& m) const { return n == m.n; }
    bool operator!=(const modint& m) const { return n != m.n; }
    modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
    modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
    modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
    modint operator+(const modint& m) const { return modint(*this) += m; }
    modint operator/(const modint& m) const { return modint(*this) * m.inv(); }
    modint operator-(const modint& m) const { return modint(*this) -= m; }
    modint operator*(const modint& m) const { return modint(*this) *= m; }
    modint inv() const { return (*this).pow(mod - 2); }
    modint pow(std::uint64_t b) const {
        modint ans = 1, m = modint(*this);
        while (b) {
            if (b & 1) ans *= m;
            m *= m;
            b >>= 1;
        }
        return ans;
    }
};

using Mint = modint<1'000'002'022>; 

int N, M;

vector<int> P;
vector<int> A;

vector<vector<int>> tree;

vector<Mint> weight;
vector<Mint> tot_prod;

Mint dfs1(int v) {
    if (v >= N) {
        return tot_prod[v] = 1;
    }

    Mint & res = tot_prod[v];
    res = size(tree[v]);
    for (auto u : tree[v]) {
        res *= dfs1(u);
    }
    return res;
}

void dfs2(int v, Mint w) {
    if (v >= N) {
        weight[v - N] = w;
        return;
    }

    int n = size(tree[v]);

    assert(n);

    vector<Mint> suf(n);

    suf[n-1] = tot_prod[tree[v].back()];
    for (int i = n - 2; i >= 0; i--) {
        suf[i] = tot_prod[tree[v][i]] * suf[i+1];
    }    

    Mint pref = 1;
    for (int i = 0; i < n; i++) {
        Mint level = (i + 1 < n ? suf[i+1]: 1);
        level *= pref;
        dfs2(tree[v][i], w * level); 
        pref *= tot_prod[tree[v][i]];
    }
}

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    ::N = N;
    ::M = M;
    ::P = P;
    ::A = A;

    tree.resize(N);

    for (int i = 1; i < N + M; i++) {
        tree[P[i]].push_back(i);
    }

    tot_prod.resize(N + M);
    dfs1(0);
    cerr << "dfs1 done\n";
    weight.resize(M);
    dfs2(0, 1);
    cerr << "dfs2 done\n";

    // for (int i = 0; i < N; i++) {
    //     cerr << "tot_prod[" << i << "] = " << tot_prod[i].get() << "\n";
    // }

    // for (int i = 0; i < M; i++) {
    //     cerr << "weight[" << i << "] = " << weight[i].get() << "\n";
    // }
}

int count_ways(int L, int R) {
    // naive
    // cerr << "count: " << L << " " << R << "\n";
    L -= N;
    R -= N;

    for (int i = L; i <= R; i++) {
        A[i] ^= 1;
    }

    Mint res = 0;
    for (int i = 0; i < M; i++)
        res += weight[i] * Mint(A[i]);

    return res.get();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 208 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 220 KB Output is correct
2 Correct 1 ms 220 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 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 336 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 268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 208 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 220 KB Output is correct
10 Correct 1 ms 220 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 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 268 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 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 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 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 292 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 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 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3066 ms 3664 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3066 ms 3664 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 220 KB Output is correct
2 Correct 1 ms 220 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 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 336 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 268 KB Output is correct
13 Execution timed out 3066 ms 3664 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 208 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 220 KB Output is correct
10 Correct 1 ms 220 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 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 268 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 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 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 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 292 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 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 208 KB Output is correct
43 Correct 1412 ms 592 KB Output is correct
44 Correct 1692 ms 592 KB Output is correct
45 Correct 1925 ms 544 KB Output is correct
46 Correct 2584 ms 804 KB Output is correct
47 Correct 2670 ms 720 KB Output is correct
48 Correct 2757 ms 720 KB Output is correct
49 Correct 2680 ms 720 KB Output is correct
50 Correct 2757 ms 808 KB Output is correct
51 Correct 2832 ms 488 KB Output is correct
52 Correct 2586 ms 496 KB Output is correct
53 Correct 813 ms 1188 KB Output is correct
54 Correct 2645 ms 720 KB Output is correct
55 Correct 2865 ms 592 KB Output is correct
56 Correct 2693 ms 556 KB Output is correct
57 Correct 2758 ms 464 KB Output is correct
58 Correct 2849 ms 1360 KB Output is correct
59 Correct 2629 ms 1444 KB Output is correct
60 Correct 2952 ms 1360 KB Output is correct
61 Correct 1613 ms 720 KB Output is correct
62 Correct 1949 ms 464 KB Output is correct
63 Correct 2160 ms 420 KB Output is correct
64 Correct 2764 ms 464 KB Output is correct
65 Correct 698 ms 504 KB Output is correct
66 Correct 2185 ms 592 KB Output is correct
67 Correct 2214 ms 592 KB Output is correct
68 Correct 2316 ms 676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 208 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 220 KB Output is correct
10 Correct 1 ms 220 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 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 268 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 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 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 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 292 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 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 208 KB Output is correct
43 Execution timed out 3066 ms 3664 KB Time limit exceeded
44 Halted 0 ms 0 KB -