#include "circuit.h"
#include <iostream>
#include <numeric>
#include <algorithm>
#include <vector>
using namespace std;
// If only one the inputs is turned on, vastaus on tulo kaikista / (tulo polulla olevista)
// If all of the inputs are turned on, vastaus on tulo kaikista.
// If two of the inputs are turned on, vastaus on 2 * tulo kaikista / (tulo poluilla (juureen) olevista)
// If three inputs are turned on, vastaus on 2 * tulo kaikista / (tulo poluilla (juureen) olevista)
// |
// | all / 1-path
// |
// 1
//
// |
// |
// |
// x
// / \
// / \ all / 1-path + all / 2-path - all / union + all / union = all / 1-path + all / 2-path
// / \ |---------------------------------------| |----------|
// 1 2 c_x = 1 c_x = 2
//
//
// |
// |
// | all / 1-path + all / 2-path + all / 3-path - all / 12-path - all / 13-path - all / 13-path + all / 123-path
// x |-------------------------------------------------------------------------------------------------------------|
// / \ c_x = 1
// / | \ all / 12-path + all / 13-path + all / 23-path - all / 123-path * 2
// / | \ |----------------------------------------------------------------|
// 1 2 3 c_x = 2
// all / 123-path
// |------------|
// c_x = 3
const int M = 1'000'002'022;
using i64 = long long;
vector<vector<int>> children;
vector<int> a;
vector<int> dp;
int add(int a, int b) {
a += b;
if (a >= M) a -= M;
return a;
}
int mul(int a, int b) {
return i64(a) * b % M;
}
int prod_dfs(int u) {
int prod = children[u].empty() ? 1 : children[u].size();
for (int v : children[u]) {
prod = mul(prod, prod_dfs(v));
}
dp[u] = prod;
return prod;
}
vector<int> oth_value;
void dfs(int u, int oth) {
oth_value[u] = oth;
vector<int> values{1};
for (int v : children[u]) {
values.push_back(dp[v]);
}
values.push_back(1);
reverse(values.begin(), values.end());
vector<int> rvalues = values;
partial_sum(values.begin(), values.end(), values.begin(), mul);
partial_sum(rvalues.rbegin(), rvalues.rend(), rvalues.rbegin(), mul);
values.pop_back();
for (int v : children[u]) {
values.pop_back();
int value = mul(oth, mul(values.back(), rvalues.back()));
dfs(v, value);
rvalues.pop_back();
}
}
int n;
void init(int n, int m, vector<int> p, vector<int> a) {
children.resize(n + m);
for (int i = 1; i < n + m; ++i) {
children[p[i]].push_back(i);
}
dp.assign(n + m, 0);
oth_value.assign(n + m, 0);
prod_dfs(0);
dfs(0, 1);
oth_value = vector<int>(oth_value.begin() + n, oth_value.end());
::n = n;
::a = a;
}
int count_ways(int l, int r) {
l -= n;
r -= n;
for (int i = l; i <= r; ++i) {
a[i] ^= 1;
}
int ans = 0;
for (int i = 0; i < (int) a.size(); ++i) {
if (a[i]) {
ans = add(ans, oth_value[i]);
}
}
return ans;
}
Compilation message
circuit.cpp:25:1: warning: multi-line comment [-Wcomment]
25 | // / \
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 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 |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 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 |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 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 |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 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 |
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 |
2 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 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 |
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 |
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 |
2 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3079 ms |
4284 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3079 ms |
4284 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 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 |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Execution timed out |
3079 ms |
4284 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 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 |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 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 |
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 |
2 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 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 |
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 |
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 |
2 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1268 ms |
592 KB |
Output is correct |
44 |
Correct |
1709 ms |
720 KB |
Output is correct |
45 |
Correct |
1789 ms |
592 KB |
Output is correct |
46 |
Correct |
2118 ms |
848 KB |
Output is correct |
47 |
Correct |
2619 ms |
848 KB |
Output is correct |
48 |
Correct |
2615 ms |
848 KB |
Output is correct |
49 |
Correct |
2617 ms |
848 KB |
Output is correct |
50 |
Correct |
1450 ms |
848 KB |
Output is correct |
51 |
Correct |
2633 ms |
596 KB |
Output is correct |
52 |
Correct |
2584 ms |
596 KB |
Output is correct |
53 |
Correct |
800 ms |
1616 KB |
Output is correct |
54 |
Correct |
2702 ms |
888 KB |
Output is correct |
55 |
Correct |
2769 ms |
720 KB |
Output is correct |
56 |
Correct |
2560 ms |
668 KB |
Output is correct |
57 |
Correct |
2584 ms |
464 KB |
Output is correct |
58 |
Correct |
2674 ms |
1872 KB |
Output is correct |
59 |
Correct |
2724 ms |
1872 KB |
Output is correct |
60 |
Correct |
2935 ms |
1872 KB |
Output is correct |
61 |
Correct |
1530 ms |
848 KB |
Output is correct |
62 |
Correct |
2078 ms |
592 KB |
Output is correct |
63 |
Correct |
1855 ms |
464 KB |
Output is correct |
64 |
Correct |
2617 ms |
592 KB |
Output is correct |
65 |
Correct |
793 ms |
464 KB |
Output is correct |
66 |
Correct |
2132 ms |
720 KB |
Output is correct |
67 |
Correct |
2172 ms |
720 KB |
Output is correct |
68 |
Correct |
2337 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 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 |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 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 |
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 |
2 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 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 |
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 |
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 |
2 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Execution timed out |
3079 ms |
4284 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |