#include "circuit.h"
#include <vector>
#include <cstdio>
using namespace std;
using ll = long long;
// N ... num threshold gates
// M ... num source gates
vector<int> A;
int N, M;
const int MOD = 1000002022;
vector<vector<int>> tree;
void init(int N, int M, vector<int> P, vector<int> A) {
::A = A;
::N = N;
::M = M;
tree.resize(N);
for (int i = 1; i < P.size(); i++)
tree[P[i]].push_back(i);
}
ll mmod(ll v) {
return ((v % MOD) + MOD) % MOD;
}
pair<ll,ll> countrec(int i) {
if (i >= N) {
if (A[i-N]) return {0, 1};
else return {1, 0};
}
int k = tree[i].size();
vector<pair<ll,ll>> v(k);
for (int j = 0; j < k; j++)
v[j] = countrec(tree[i][j]);
vector<vector<ll>> mul(k+1, vector<ll>(k+1));
mul[0][0] = 1;
for (int j = 1; j <= k; j++)
mul[0][j] = (mul[0][j-1] * v[j-1].first) % MOD;
for (int l = 1; l <= k; l++) {
for (int j = 1; j <= k; j++) {
mul[l][j] = (
mul[l-1][j-1] * v[j-1].second
+ mul[l][j-1] * v[j-1].first
) % MOD;
}
}
ll ans = 0;
for (int l = 1; l <= k; l++) {
ans = (ans + mul[l][k]*l) % MOD;
}
ll tot = k;
for (int j = 0; j < k; j++)
tot = (tot * (v[j].first + v[j].second)) % MOD;
/*printf("%d: tot=%lld ans=%lld\n", i, tot, ans);
printf("V %d: ", i);
for (auto it : v)
printf("{%lld,%lld} ", it.first, it.second);
printf("\n");*/
return {mmod(tot-ans), mmod(ans)};
}
// toggle state of source gates between L and R (inclusive)
// return num distinctive param assignments with gate#0 = 1
int count_ways(int L, int R) {
for (int i = L; i <= R; i++)
A[i-N] = 1-A[i-N];
return countrec(0).second;
}
Compilation message
circuit.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>)':
circuit.cpp:21:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 1; i < P.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
47 ms |
7740 KB |
Output is correct |
4 |
Correct |
52 ms |
8236 KB |
Output is correct |
5 |
Correct |
51 ms |
8208 KB |
Output is correct |
6 |
Correct |
50 ms |
8180 KB |
Output is correct |
7 |
Correct |
56 ms |
8212 KB |
Output is correct |
8 |
Correct |
51 ms |
8212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
3 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
564 KB |
Output is correct |
11 |
Correct |
3 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
336 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 |
47 ms |
7740 KB |
Output is correct |
4 |
Correct |
52 ms |
8236 KB |
Output is correct |
5 |
Correct |
51 ms |
8208 KB |
Output is correct |
6 |
Correct |
50 ms |
8180 KB |
Output is correct |
7 |
Correct |
56 ms |
8212 KB |
Output is correct |
8 |
Correct |
51 ms |
8212 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 |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
3 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
2 ms |
564 KB |
Output is correct |
19 |
Correct |
3 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 |
208 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
53 ms |
8224 KB |
Output is correct |
30 |
Correct |
56 ms |
8224 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 |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
208 KB |
Output is correct |
35 |
Correct |
10 ms |
720 KB |
Output is correct |
36 |
Correct |
1 ms |
464 KB |
Output is correct |
37 |
Correct |
53 ms |
8496 KB |
Output is correct |
38 |
Correct |
51 ms |
8460 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
208 KB |
Output is correct |
41 |
Correct |
1 ms |
208 KB |
Output is correct |
42 |
Correct |
2 ms |
368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3005 ms |
2872 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3005 ms |
2872 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
3 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
564 KB |
Output is correct |
11 |
Correct |
3 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Execution timed out |
3005 ms |
2872 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 |
47 ms |
7740 KB |
Output is correct |
4 |
Correct |
52 ms |
8236 KB |
Output is correct |
5 |
Correct |
51 ms |
8208 KB |
Output is correct |
6 |
Correct |
50 ms |
8180 KB |
Output is correct |
7 |
Correct |
56 ms |
8212 KB |
Output is correct |
8 |
Correct |
51 ms |
8212 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 |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
3 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
2 ms |
564 KB |
Output is correct |
19 |
Correct |
3 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 |
208 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
53 ms |
8224 KB |
Output is correct |
30 |
Correct |
56 ms |
8224 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 |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
208 KB |
Output is correct |
35 |
Correct |
10 ms |
720 KB |
Output is correct |
36 |
Correct |
1 ms |
464 KB |
Output is correct |
37 |
Correct |
53 ms |
8496 KB |
Output is correct |
38 |
Correct |
51 ms |
8460 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
208 KB |
Output is correct |
41 |
Correct |
1 ms |
208 KB |
Output is correct |
42 |
Correct |
2 ms |
368 KB |
Output is correct |
43 |
Execution timed out |
3075 ms |
464 KB |
Time limit exceeded |
44 |
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 |
47 ms |
7740 KB |
Output is correct |
4 |
Correct |
52 ms |
8236 KB |
Output is correct |
5 |
Correct |
51 ms |
8208 KB |
Output is correct |
6 |
Correct |
50 ms |
8180 KB |
Output is correct |
7 |
Correct |
56 ms |
8212 KB |
Output is correct |
8 |
Correct |
51 ms |
8212 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 |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
3 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
2 ms |
564 KB |
Output is correct |
19 |
Correct |
3 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 |
208 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
53 ms |
8224 KB |
Output is correct |
30 |
Correct |
56 ms |
8224 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 |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
208 KB |
Output is correct |
35 |
Correct |
10 ms |
720 KB |
Output is correct |
36 |
Correct |
1 ms |
464 KB |
Output is correct |
37 |
Correct |
53 ms |
8496 KB |
Output is correct |
38 |
Correct |
51 ms |
8460 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
208 KB |
Output is correct |
41 |
Correct |
1 ms |
208 KB |
Output is correct |
42 |
Correct |
2 ms |
368 KB |
Output is correct |
43 |
Execution timed out |
3005 ms |
2872 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |