#include "circuit.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
const ll MOD = 1e9 + 2022;
void chadd(ll &x, ll y) { x = (x + y) % MOD; }
void chmul(ll &x, ll y) { x = (x * y) % MOD; }
const int mxN = 2e5 + 10;
vector<int> adj[mxN];
int n, m;
bool state[mxN];
void init(int N, int M, vector<int> P, vector<int> A) {
n = N; m = M;
for (int i = 1; i < (int)(P.size()); i++) {
adj[P[i]].pb(i);
}
for (int i = 0; i < (int)(A.size()); i++) {
state[n + i] = A[i];
}
}
int count_ways(int L, int R) {
for (int i = L; i <= R; i++) state[i] ^= 1;
vector<vector<ll>> dp(n + m);
for (int i = 0; i < n + m; i++) dp[i].resize(2);
vector<ll> poly(n + m);
auto dfs = [&](auto&& self, int u) -> void {
if (u >= n) {
dp[u][0] = dp[u][1] = 0;
dp[u][state[u]] = 1;
return;
}
for (int v : adj[u]) self(self, v);
int sz = adj[u].size();
for (int i = 0; i <= sz; i++) poly[i] = (i == 0);
for (int v : adj[u]) {
// multiply by (c0 + c1*x)
for (int i = sz; i >= 1; i--) {
poly[i] = (poly[i] * dp[v][0] + poly[i - 1] * dp[v][1]) % MOD;
}
poly[0] = (poly[0] * dp[v][0]) % MOD;
}
dp[u][0] = dp[u][1] = 0;
for (int i = 0; i <= sz; i++) {
chadd(dp[u][0], (sz - i) * poly[i]);
chadd(dp[u][1], i * poly[i]);
}
};
dfs(dfs, 0);
return (int)(dp[0][1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
9 ms |
5208 KB |
Output is correct |
4 |
Correct |
9 ms |
5208 KB |
Output is correct |
5 |
Correct |
10 ms |
5208 KB |
Output is correct |
6 |
Correct |
15 ms |
5224 KB |
Output is correct |
7 |
Correct |
9 ms |
5204 KB |
Output is correct |
8 |
Correct |
9 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5208 KB |
Output is correct |
7 |
Correct |
4 ms |
5296 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
4 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
9 ms |
5208 KB |
Output is correct |
4 |
Correct |
9 ms |
5208 KB |
Output is correct |
5 |
Correct |
10 ms |
5208 KB |
Output is correct |
6 |
Correct |
15 ms |
5224 KB |
Output is correct |
7 |
Correct |
9 ms |
5204 KB |
Output is correct |
8 |
Correct |
9 ms |
5208 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5208 KB |
Output is correct |
15 |
Correct |
4 ms |
5296 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
2 ms |
5208 KB |
Output is correct |
19 |
Correct |
4 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5208 KB |
Output is correct |
24 |
Correct |
2 ms |
5208 KB |
Output is correct |
25 |
Correct |
3 ms |
5460 KB |
Output is correct |
26 |
Correct |
2 ms |
5204 KB |
Output is correct |
27 |
Correct |
2 ms |
5252 KB |
Output is correct |
28 |
Correct |
4 ms |
5208 KB |
Output is correct |
29 |
Correct |
9 ms |
5208 KB |
Output is correct |
30 |
Correct |
15 ms |
5208 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
2 ms |
5208 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
5464 KB |
Output is correct |
37 |
Correct |
10 ms |
5208 KB |
Output is correct |
38 |
Correct |
10 ms |
5208 KB |
Output is correct |
39 |
Correct |
3 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
5192 KB |
Output is correct |
41 |
Correct |
3 ms |
5464 KB |
Output is correct |
42 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3009 ms |
11128 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3009 ms |
11128 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5208 KB |
Output is correct |
7 |
Correct |
4 ms |
5296 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
4 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Execution timed out |
3009 ms |
11128 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
9 ms |
5208 KB |
Output is correct |
4 |
Correct |
9 ms |
5208 KB |
Output is correct |
5 |
Correct |
10 ms |
5208 KB |
Output is correct |
6 |
Correct |
15 ms |
5224 KB |
Output is correct |
7 |
Correct |
9 ms |
5204 KB |
Output is correct |
8 |
Correct |
9 ms |
5208 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5208 KB |
Output is correct |
15 |
Correct |
4 ms |
5296 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
2 ms |
5208 KB |
Output is correct |
19 |
Correct |
4 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5208 KB |
Output is correct |
24 |
Correct |
2 ms |
5208 KB |
Output is correct |
25 |
Correct |
3 ms |
5460 KB |
Output is correct |
26 |
Correct |
2 ms |
5204 KB |
Output is correct |
27 |
Correct |
2 ms |
5252 KB |
Output is correct |
28 |
Correct |
4 ms |
5208 KB |
Output is correct |
29 |
Correct |
9 ms |
5208 KB |
Output is correct |
30 |
Correct |
15 ms |
5208 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
2 ms |
5208 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
5464 KB |
Output is correct |
37 |
Correct |
10 ms |
5208 KB |
Output is correct |
38 |
Correct |
10 ms |
5208 KB |
Output is correct |
39 |
Correct |
3 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
5192 KB |
Output is correct |
41 |
Correct |
3 ms |
5464 KB |
Output is correct |
42 |
Correct |
3 ms |
5208 KB |
Output is correct |
43 |
Execution timed out |
3041 ms |
6008 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
9 ms |
5208 KB |
Output is correct |
4 |
Correct |
9 ms |
5208 KB |
Output is correct |
5 |
Correct |
10 ms |
5208 KB |
Output is correct |
6 |
Correct |
15 ms |
5224 KB |
Output is correct |
7 |
Correct |
9 ms |
5204 KB |
Output is correct |
8 |
Correct |
9 ms |
5208 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5208 KB |
Output is correct |
15 |
Correct |
4 ms |
5296 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
2 ms |
5208 KB |
Output is correct |
19 |
Correct |
4 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5208 KB |
Output is correct |
24 |
Correct |
2 ms |
5208 KB |
Output is correct |
25 |
Correct |
3 ms |
5460 KB |
Output is correct |
26 |
Correct |
2 ms |
5204 KB |
Output is correct |
27 |
Correct |
2 ms |
5252 KB |
Output is correct |
28 |
Correct |
4 ms |
5208 KB |
Output is correct |
29 |
Correct |
9 ms |
5208 KB |
Output is correct |
30 |
Correct |
15 ms |
5208 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
2 ms |
5208 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
5464 KB |
Output is correct |
37 |
Correct |
10 ms |
5208 KB |
Output is correct |
38 |
Correct |
10 ms |
5208 KB |
Output is correct |
39 |
Correct |
3 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
5192 KB |
Output is correct |
41 |
Correct |
3 ms |
5464 KB |
Output is correct |
42 |
Correct |
3 ms |
5208 KB |
Output is correct |
43 |
Execution timed out |
3009 ms |
11128 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |