#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int64_t(1000002022)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
#define int long long
const int MAXN = 1e5+5;
int n,m;
vector<int> adj[MAXN];
int z[MAXN];
int dp0[MAXN], dp1[MAXN], cnt[MAXN];
void dfs(int s) {
int sz = adj[s].size();
if (sz == 0) {
dp1[s] = z[s];
dp0[s] = z[s]^1LL;
cnt[s] = 1LL;
return;
}
FORX(u,adj[s]) {
dfs(u);
}
vector<int> v;
FORX(u,adj[s]) v.PB(u);
vector<vector<int>> dp(sz+1,vector<int>(sz+1,0LL));
vector<int> num(sz+1,0LL), num1(sz+1,0LL);
FOR(i,0,sz) {
num[i+1] = num[i];
num1[i+1] = num1[i];
if (dp0[v[i]] == 0) num[i+1]++;
if (dp1[v[i]] == 0) num1[i+1]++;
}
// FOR(j,0,sz+1) dp[0][j] = 1LL;
dp[0][0] = 1LL;
FOR(j,1,sz+1) dp[0][j] = (dp[0][j-1]*dp0[v[j-1]])%INF;
FOR(i,1,sz+1) {
FOR(j,1,sz+1) {
dp[i][j] = ((dp[i][j-1]*dp0[v[j-1]])%INF + (dp[i-1][j-1]*dp1[v[j-1]])%INF)%INF;
// if (num[j] > i || num1[j] > sz-i) dp[i][j] = 0LL;
}
}
// if (s == 0) {
// FOR(i,0,sz+1) {
// FOR(j,0,sz+1) cout << dp[i][j] << " ";
// cout << ln;
// }
// }
int total = sz;
FORX(u,adj[s]) total = (total*cnt[u])%INF;
int dpsum = 0LL;
FOR(i,1,sz+1) dpsum = (dpsum+(dp[i][sz]*i)%INF)%INF;
dp1[s] = dpsum;
dp0[s] = (total-dpsum+INF)%INF;
cnt[s] = total;
}
void init(int32_t N, int32_t M, std::vector<int32_t> P, std::vector<int32_t> A) {
n = N; m = M;
FOR(i,1,int32_t(P.size())) adj[P[i]].PB(i);
FOR(i,0,int32_t(A.size())) z[n+i] = A[i];
}
int32_t count_ways(int32_t L, int32_t R) {
FOR(i,L,R+1) z[i] ^= 1;
dfs(0);
// FOR(i,0,n+m) cout << dp1[i] << " ";
// cout << ln;
// FOR(i,0,n+m) cout << dp0[i] << " ";
// cout << ln;
return dp1[0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
61 ms |
10172 KB |
Output is correct |
4 |
Correct |
65 ms |
10616 KB |
Output is correct |
5 |
Correct |
65 ms |
10608 KB |
Output is correct |
6 |
Correct |
60 ms |
10612 KB |
Output is correct |
7 |
Correct |
68 ms |
10560 KB |
Output is correct |
8 |
Correct |
64 ms |
10620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
3 ms |
2716 KB |
Output is correct |
9 |
Correct |
2 ms |
2768 KB |
Output is correct |
10 |
Correct |
3 ms |
2896 KB |
Output is correct |
11 |
Correct |
3 ms |
2896 KB |
Output is correct |
12 |
Correct |
4 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
61 ms |
10172 KB |
Output is correct |
4 |
Correct |
65 ms |
10616 KB |
Output is correct |
5 |
Correct |
65 ms |
10608 KB |
Output is correct |
6 |
Correct |
60 ms |
10612 KB |
Output is correct |
7 |
Correct |
68 ms |
10560 KB |
Output is correct |
8 |
Correct |
64 ms |
10620 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2716 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
3 ms |
2896 KB |
Output is correct |
19 |
Correct |
3 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2640 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 KB |
Output is correct |
22 |
Correct |
4 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
3 ms |
2768 KB |
Output is correct |
25 |
Correct |
3 ms |
2768 KB |
Output is correct |
26 |
Correct |
3 ms |
2768 KB |
Output is correct |
27 |
Correct |
3 ms |
2768 KB |
Output is correct |
28 |
Correct |
3 ms |
2768 KB |
Output is correct |
29 |
Correct |
63 ms |
10596 KB |
Output is correct |
30 |
Correct |
61 ms |
10592 KB |
Output is correct |
31 |
Correct |
3 ms |
2896 KB |
Output is correct |
32 |
Correct |
4 ms |
2768 KB |
Output is correct |
33 |
Correct |
4 ms |
2640 KB |
Output is correct |
34 |
Correct |
3 ms |
2640 KB |
Output is correct |
35 |
Correct |
13 ms |
3116 KB |
Output is correct |
36 |
Correct |
3 ms |
2896 KB |
Output is correct |
37 |
Correct |
65 ms |
10896 KB |
Output is correct |
38 |
Correct |
63 ms |
10912 KB |
Output is correct |
39 |
Correct |
2 ms |
2640 KB |
Output is correct |
40 |
Correct |
2 ms |
2640 KB |
Output is correct |
41 |
Correct |
2 ms |
2640 KB |
Output is correct |
42 |
Correct |
3 ms |
2768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3032 ms |
5852 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3032 ms |
5852 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
3 ms |
2716 KB |
Output is correct |
9 |
Correct |
2 ms |
2768 KB |
Output is correct |
10 |
Correct |
3 ms |
2896 KB |
Output is correct |
11 |
Correct |
3 ms |
2896 KB |
Output is correct |
12 |
Correct |
4 ms |
2640 KB |
Output is correct |
13 |
Execution timed out |
3032 ms |
5852 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
61 ms |
10172 KB |
Output is correct |
4 |
Correct |
65 ms |
10616 KB |
Output is correct |
5 |
Correct |
65 ms |
10608 KB |
Output is correct |
6 |
Correct |
60 ms |
10612 KB |
Output is correct |
7 |
Correct |
68 ms |
10560 KB |
Output is correct |
8 |
Correct |
64 ms |
10620 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2716 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
3 ms |
2896 KB |
Output is correct |
19 |
Correct |
3 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2640 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 KB |
Output is correct |
22 |
Correct |
4 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
3 ms |
2768 KB |
Output is correct |
25 |
Correct |
3 ms |
2768 KB |
Output is correct |
26 |
Correct |
3 ms |
2768 KB |
Output is correct |
27 |
Correct |
3 ms |
2768 KB |
Output is correct |
28 |
Correct |
3 ms |
2768 KB |
Output is correct |
29 |
Correct |
63 ms |
10596 KB |
Output is correct |
30 |
Correct |
61 ms |
10592 KB |
Output is correct |
31 |
Correct |
3 ms |
2896 KB |
Output is correct |
32 |
Correct |
4 ms |
2768 KB |
Output is correct |
33 |
Correct |
4 ms |
2640 KB |
Output is correct |
34 |
Correct |
3 ms |
2640 KB |
Output is correct |
35 |
Correct |
13 ms |
3116 KB |
Output is correct |
36 |
Correct |
3 ms |
2896 KB |
Output is correct |
37 |
Correct |
65 ms |
10896 KB |
Output is correct |
38 |
Correct |
63 ms |
10912 KB |
Output is correct |
39 |
Correct |
2 ms |
2640 KB |
Output is correct |
40 |
Correct |
2 ms |
2640 KB |
Output is correct |
41 |
Correct |
2 ms |
2640 KB |
Output is correct |
42 |
Correct |
3 ms |
2768 KB |
Output is correct |
43 |
Execution timed out |
3094 ms |
2896 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
61 ms |
10172 KB |
Output is correct |
4 |
Correct |
65 ms |
10616 KB |
Output is correct |
5 |
Correct |
65 ms |
10608 KB |
Output is correct |
6 |
Correct |
60 ms |
10612 KB |
Output is correct |
7 |
Correct |
68 ms |
10560 KB |
Output is correct |
8 |
Correct |
64 ms |
10620 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2716 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
3 ms |
2896 KB |
Output is correct |
19 |
Correct |
3 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2640 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 KB |
Output is correct |
22 |
Correct |
4 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
3 ms |
2768 KB |
Output is correct |
25 |
Correct |
3 ms |
2768 KB |
Output is correct |
26 |
Correct |
3 ms |
2768 KB |
Output is correct |
27 |
Correct |
3 ms |
2768 KB |
Output is correct |
28 |
Correct |
3 ms |
2768 KB |
Output is correct |
29 |
Correct |
63 ms |
10596 KB |
Output is correct |
30 |
Correct |
61 ms |
10592 KB |
Output is correct |
31 |
Correct |
3 ms |
2896 KB |
Output is correct |
32 |
Correct |
4 ms |
2768 KB |
Output is correct |
33 |
Correct |
4 ms |
2640 KB |
Output is correct |
34 |
Correct |
3 ms |
2640 KB |
Output is correct |
35 |
Correct |
13 ms |
3116 KB |
Output is correct |
36 |
Correct |
3 ms |
2896 KB |
Output is correct |
37 |
Correct |
65 ms |
10896 KB |
Output is correct |
38 |
Correct |
63 ms |
10912 KB |
Output is correct |
39 |
Correct |
2 ms |
2640 KB |
Output is correct |
40 |
Correct |
2 ms |
2640 KB |
Output is correct |
41 |
Correct |
2 ms |
2640 KB |
Output is correct |
42 |
Correct |
3 ms |
2768 KB |
Output is correct |
43 |
Execution timed out |
3032 ms |
5852 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |