Submission #1009684

# Submission time Handle Problem Language Result Execution time Memory
1009684 2024-06-27T20:18:46 Z aaaaaarroz Digital Circuit (IOI22_circuit) C++17
18 / 100
3000 ms 4184 KB
// O(Q(N + M)^2)

#include "circuit.h"

#include <algorithm>
#include <vector>

const int kMod = 1000002022;

std::vector<std::vector<int>> adj;
std::vector<int> dp0, dp1;

void dfs(int u) {
  if (adj[u].empty()) {
    return;
  }

  std::vector<int> dp = {1};
  for (int v : adj[u]) {
    dfs(v);
    std::vector<int> prod0(dp.size()), prod1(dp.size());
    for (int i = 0; i < static_cast<int>(dp.size()); ++i) {
      prod0[i] = 1LL * dp[i] * dp0[v] % kMod;
      prod1[i] = 1LL * dp[i] * dp1[v] % kMod;
    }
    dp[0] = prod0[0];
    for (int i = 1; i < static_cast<int>(dp.size()); ++i) {
      dp[i] = (prod0[i] + prod1[i - 1]) % kMod;
    }
    dp.push_back(prod1.back());
  }

  dp0[u] = 0;
  for (int i = 0, sum = 0; i + 1 < static_cast<int>(dp.size()); ++i) {
    sum = (sum + dp[i]) % kMod;
    dp0[u] = (dp0[u] + sum) % kMod;
  }

  dp1[u] = 0;
  for (int i = static_cast<int>(dp.size()) - 1, sum = 0; i >= 1; --i) {
    sum = (sum + dp[i]) % kMod;
    dp1[u] = (dp1[u] + sum) % kMod;
  }
}

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
  adj.resize(N + M);
  for (int i = 1; i < N + M; ++i) {
    adj[P[i]].push_back(i);
  }

  dp0.resize(N + M);
  dp1.resize(N + M);

  for (int i = N; i < N + M; ++i) {
    if (A[i - N]) {
      dp1[i] = 1;
    } else {
      dp0[i] = 1;
    }
  }
}

int count_ways(int L, int R) {
  for (int i = L; i <= R; ++i) {
    std::swap(dp0[i], dp1[i]);
  }
  dfs(0);
  return dp1[0];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 12 ms 536 KB Output is correct
4 Correct 14 ms 344 KB Output is correct
5 Correct 12 ms 344 KB Output is correct
6 Correct 12 ms 344 KB Output is correct
7 Correct 15 ms 344 KB Output is correct
8 Correct 14 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 2 ms 600 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 12 ms 536 KB Output is correct
4 Correct 14 ms 344 KB Output is correct
5 Correct 12 ms 344 KB Output is correct
6 Correct 12 ms 344 KB Output is correct
7 Correct 15 ms 344 KB Output is correct
8 Correct 14 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 2 ms 600 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 2 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 13 ms 344 KB Output is correct
30 Correct 14 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 412 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 3 ms 344 KB Output is correct
36 Correct 2 ms 600 KB Output is correct
37 Correct 12 ms 600 KB Output is correct
38 Correct 12 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3050 ms 4184 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3050 ms 4184 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 2 ms 600 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Execution timed out 3050 ms 4184 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 12 ms 536 KB Output is correct
4 Correct 14 ms 344 KB Output is correct
5 Correct 12 ms 344 KB Output is correct
6 Correct 12 ms 344 KB Output is correct
7 Correct 15 ms 344 KB Output is correct
8 Correct 14 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 2 ms 600 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 2 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 13 ms 344 KB Output is correct
30 Correct 14 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 412 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 3 ms 344 KB Output is correct
36 Correct 2 ms 600 KB Output is correct
37 Correct 12 ms 600 KB Output is correct
38 Correct 12 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Execution timed out 3052 ms 600 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 12 ms 536 KB Output is correct
4 Correct 14 ms 344 KB Output is correct
5 Correct 12 ms 344 KB Output is correct
6 Correct 12 ms 344 KB Output is correct
7 Correct 15 ms 344 KB Output is correct
8 Correct 14 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 2 ms 600 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 2 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 13 ms 344 KB Output is correct
30 Correct 14 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 412 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 3 ms 344 KB Output is correct
36 Correct 2 ms 600 KB Output is correct
37 Correct 12 ms 600 KB Output is correct
38 Correct 12 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Execution timed out 3050 ms 4184 KB Time limit exceeded
44 Halted 0 ms 0 KB -