#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000002022;
int ckadd(int a, int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int ckmul(int a, int b) {
return a * 1LL * b % MOD;
}
int ckpower(int a, int n) {
int res = 1;
while (n) {
if (n & 1)
res = ckmul(res, a);
a = ckmul(a, a);
n >>= 1;
}
return res;
}
int ckdiv(int a, int b) {
return ckmul(a, ckpower(b, MOD - 2));
}
const int N = (int)2e5 + 6;
int n, m;
int a[N];
int par[N];
int deg[N];
int ANS[N];
bool vis[N];
int prod[N];
vector<int> g[N];
void dfsProd(int v) {
prod[v] = deg[v];
for (int u : g[v]) {
if (u < n) {
dfsProd(u);
prod[v] = ckmul(prod[v], prod[u]);
} else prod[u] = 1;
}
}
void dfsSolve(int v, int x) {
if (v >= n) {
ANS[v] = x;
return;
}
vector<int> L;
vector<int> R;
L.push_back(1);
for (int i = 0; i < (int) g[v].size(); i++) {
L.push_back(ckmul(L.back(), prod[g[v][i]]));
}
R.push_back(1);
for (int i = (int) g[v].size() - 1; i >= 0; i--) {
R.push_back(ckmul(R.back(), prod[g[v][i]]));
}
reverse(R.begin(), R.end());
for (int i = 0; i < (int) g[v].size(); i++) {
dfsSolve(g[v][i], ckmul(x, ckmul(L[i], R[i + 1])));
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N; m = M;
for (int i = n; i < n + m; i++) a[i] = A[i - n];
for (int i = 0; i < n + m; i++) par[i] = P[i];
for (int i = 1; i < n + m; i++) {
g[par[i]].push_back(i);
deg[par[i]]++;
}
dfsProd(0);
// printf("prod = "); for (int i = 0; i < n; i++) printf("%d ", prod[i]); printf("\n");
dfsSolve(0, 1);
}
int count_ways(int L, int R) {
for (int i = L; i <= R; i++) a[i] = 1 - a[i];
int ans = 0;
for (int i = n; i < n + m; i++) if (a[i] == 1) {
ans = ckadd(ans, ANS[i]);
}
return ans;
}
/*
3 4 3
-1 0 1 2 1 1 0
1 0 1 0
3 4
4 5
3 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
5 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
4 ms |
5072 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
5 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
4 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5024 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5120 KB |
Output is correct |
28 |
Correct |
3 ms |
5048 KB |
Output is correct |
29 |
Correct |
3 ms |
4944 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5028 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
4944 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
4944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3018 ms |
7628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3018 ms |
7628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
5 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
4 ms |
5072 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Execution timed out |
3018 ms |
7628 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
5 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
4 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5024 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5120 KB |
Output is correct |
28 |
Correct |
3 ms |
5048 KB |
Output is correct |
29 |
Correct |
3 ms |
4944 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5028 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
4944 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
4944 KB |
Output is correct |
43 |
Correct |
1241 ms |
5200 KB |
Output is correct |
44 |
Correct |
1658 ms |
5200 KB |
Output is correct |
45 |
Correct |
1660 ms |
5200 KB |
Output is correct |
46 |
Correct |
2234 ms |
5320 KB |
Output is correct |
47 |
Correct |
2605 ms |
5328 KB |
Output is correct |
48 |
Correct |
2525 ms |
5328 KB |
Output is correct |
49 |
Correct |
2294 ms |
5328 KB |
Output is correct |
50 |
Correct |
1506 ms |
5328 KB |
Output is correct |
51 |
Correct |
2596 ms |
5260 KB |
Output is correct |
52 |
Correct |
2470 ms |
5252 KB |
Output is correct |
53 |
Correct |
662 ms |
6352 KB |
Output is correct |
54 |
Correct |
2391 ms |
5328 KB |
Output is correct |
55 |
Correct |
2457 ms |
5200 KB |
Output is correct |
56 |
Correct |
2488 ms |
5164 KB |
Output is correct |
57 |
Correct |
2464 ms |
5200 KB |
Output is correct |
58 |
Correct |
2511 ms |
6480 KB |
Output is correct |
59 |
Correct |
2470 ms |
6596 KB |
Output is correct |
60 |
Correct |
2551 ms |
6608 KB |
Output is correct |
61 |
Correct |
1548 ms |
5456 KB |
Output is correct |
62 |
Correct |
1815 ms |
5200 KB |
Output is correct |
63 |
Correct |
1971 ms |
5200 KB |
Output is correct |
64 |
Correct |
2388 ms |
5200 KB |
Output is correct |
65 |
Correct |
740 ms |
5072 KB |
Output is correct |
66 |
Correct |
2094 ms |
5328 KB |
Output is correct |
67 |
Correct |
2041 ms |
5328 KB |
Output is correct |
68 |
Correct |
2170 ms |
5328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
5 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
4 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5024 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5120 KB |
Output is correct |
28 |
Correct |
3 ms |
5048 KB |
Output is correct |
29 |
Correct |
3 ms |
4944 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5028 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
4944 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
4944 KB |
Output is correct |
43 |
Execution timed out |
3018 ms |
7628 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |