#include <bits/stdc++.h>
#include "circuit.h"
#include <vector>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
int n, m;
vector<int>child[100001];
bool state[100001];
ll encen[100001][2]; // encendido y apagado
const int MOD = 1000002022;
int past[2002][2002];
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
m = M;
for(int i = 0 ; i < n+m ; i ++){
if(P[i] != -1){
child[P[i]].pb(i);
}
}
for(int i = N ; i <= N+M ; i ++)state[i] = A[i-N];
memset(past, -1, sizeof past);
}
ll mem[2002][2002];
int currentIt = -1;
void newDp(){
currentIt++;
}
ll dp(int parent, int i, int k){
if(k < 0)return 0;
if(i == child[parent].size() and k > 0)return 0;
else if(i == child[parent].size()) return 1;
if(past[i][k] == currentIt)return mem[i][k];
past[i][k] = currentIt;
return mem[i][k] = (dp(parent, i+1, k-1) * encen[child[parent][i]][0] + dp(parent, i+1, k) * encen[child[parent][i]][1])%MOD;
}
void generate(int node){
if(node >= n){
if(state[node]){
encen[node][0] = 1;
encen[node][1] = 0;
}else{
encen[node][0] = 0;
encen[node][1] = 1;
}
return ;
}
for(int i = 0 ; i < child[node].size() ; i ++){
generate(child[node][i]);
}
newDp();
ll enc = 0;
for(int i = 1 ; i <= child[node].size() ; i ++){
enc += dp(node, 0, i) * i;
enc %= MOD;
}
ll allWays = 1;
for(int i = 0 ; i < child[node].size() ; i ++){
allWays *= (encen[child[node][i]][0] + encen[child[node][i]][1]);
allWays %= MOD;
}
allWays *= child[node].size();
allWays %= MOD;
encen[node][0] = enc;
encen[node][1] = ((allWays - enc) + MOD) % MOD;
//cout << "for node " << node << " there are " << encen[node][0] << ", " << encen[node][1] << "\n";
}
int count_ways(int L, int R) {
ll ans = 0;
for(int i = L ; i <= R ; i ++){
state[i] = !state[i];
}
generate(0);
return encen[0][0];
}
Compilation message
circuit.cpp: In function 'long long int dp(int, int, int)':
circuit.cpp:36:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | if(i == child[parent].size() and k > 0)return 0;
| ~~^~~~~~~~~~~~~~~~~~~~~~~
circuit.cpp:37:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | else if(i == child[parent].size()) return 1;
| ~~^~~~~~~~~~~~~~~~~~~~~~~
circuit.cpp: In function 'void generate(int)':
circuit.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i = 0 ; i < child[node].size() ; i ++){
| ~~^~~~~~~~~~~~~~~~~~~~
circuit.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 1 ; i <= child[node].size() ; i ++){
| ~~^~~~~~~~~~~~~~~~~~~~~
circuit.cpp:64:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i = 0 ; i < child[node].size() ; i ++){
| ~~^~~~~~~~~~~~~~~~~~~~
circuit.cpp: In function 'int count_ways(int, int)':
circuit.cpp:77:5: warning: unused variable 'ans' [-Wunused-variable]
77 | ll ans = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18256 KB |
Output is correct |
2 |
Correct |
7 ms |
18256 KB |
Output is correct |
3 |
Correct |
92 ms |
29668 KB |
Output is correct |
4 |
Correct |
108 ms |
30192 KB |
Output is correct |
5 |
Correct |
94 ms |
30228 KB |
Output is correct |
6 |
Correct |
96 ms |
30240 KB |
Output is correct |
7 |
Correct |
122 ms |
30200 KB |
Output is correct |
8 |
Correct |
121 ms |
30268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
18256 KB |
Output is correct |
2 |
Correct |
8 ms |
18324 KB |
Output is correct |
3 |
Correct |
7 ms |
18384 KB |
Output is correct |
4 |
Correct |
8 ms |
18384 KB |
Output is correct |
5 |
Correct |
8 ms |
18372 KB |
Output is correct |
6 |
Correct |
8 ms |
18384 KB |
Output is correct |
7 |
Correct |
9 ms |
18416 KB |
Output is correct |
8 |
Correct |
11 ms |
18384 KB |
Output is correct |
9 |
Correct |
10 ms |
18396 KB |
Output is correct |
10 |
Correct |
9 ms |
18388 KB |
Output is correct |
11 |
Correct |
8 ms |
18384 KB |
Output is correct |
12 |
Correct |
8 ms |
18384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18256 KB |
Output is correct |
2 |
Correct |
7 ms |
18256 KB |
Output is correct |
3 |
Correct |
92 ms |
29668 KB |
Output is correct |
4 |
Correct |
108 ms |
30192 KB |
Output is correct |
5 |
Correct |
94 ms |
30228 KB |
Output is correct |
6 |
Correct |
96 ms |
30240 KB |
Output is correct |
7 |
Correct |
122 ms |
30200 KB |
Output is correct |
8 |
Correct |
121 ms |
30268 KB |
Output is correct |
9 |
Correct |
7 ms |
18256 KB |
Output is correct |
10 |
Correct |
8 ms |
18324 KB |
Output is correct |
11 |
Correct |
7 ms |
18384 KB |
Output is correct |
12 |
Correct |
8 ms |
18384 KB |
Output is correct |
13 |
Correct |
8 ms |
18372 KB |
Output is correct |
14 |
Correct |
8 ms |
18384 KB |
Output is correct |
15 |
Correct |
9 ms |
18416 KB |
Output is correct |
16 |
Correct |
11 ms |
18384 KB |
Output is correct |
17 |
Correct |
10 ms |
18396 KB |
Output is correct |
18 |
Correct |
9 ms |
18388 KB |
Output is correct |
19 |
Correct |
8 ms |
18384 KB |
Output is correct |
20 |
Correct |
8 ms |
18384 KB |
Output is correct |
21 |
Correct |
8 ms |
18384 KB |
Output is correct |
22 |
Correct |
9 ms |
18384 KB |
Output is correct |
23 |
Correct |
8 ms |
18384 KB |
Output is correct |
24 |
Correct |
10 ms |
18384 KB |
Output is correct |
25 |
Correct |
9 ms |
18384 KB |
Output is correct |
26 |
Correct |
10 ms |
18384 KB |
Output is correct |
27 |
Correct |
8 ms |
18384 KB |
Output is correct |
28 |
Correct |
11 ms |
18384 KB |
Output is correct |
29 |
Correct |
101 ms |
30288 KB |
Output is correct |
30 |
Correct |
102 ms |
30180 KB |
Output is correct |
31 |
Correct |
8 ms |
18384 KB |
Output is correct |
32 |
Correct |
8 ms |
18384 KB |
Output is correct |
33 |
Correct |
10 ms |
18384 KB |
Output is correct |
34 |
Correct |
8 ms |
18316 KB |
Output is correct |
35 |
Correct |
20 ms |
19576 KB |
Output is correct |
36 |
Correct |
9 ms |
18384 KB |
Output is correct |
37 |
Correct |
112 ms |
30400 KB |
Output is correct |
38 |
Correct |
106 ms |
30384 KB |
Output is correct |
39 |
Correct |
8 ms |
18384 KB |
Output is correct |
40 |
Correct |
9 ms |
18380 KB |
Output is correct |
41 |
Correct |
8 ms |
18384 KB |
Output is correct |
42 |
Correct |
10 ms |
18508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3086 ms |
20800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3086 ms |
20800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
18256 KB |
Output is correct |
2 |
Correct |
8 ms |
18324 KB |
Output is correct |
3 |
Correct |
7 ms |
18384 KB |
Output is correct |
4 |
Correct |
8 ms |
18384 KB |
Output is correct |
5 |
Correct |
8 ms |
18372 KB |
Output is correct |
6 |
Correct |
8 ms |
18384 KB |
Output is correct |
7 |
Correct |
9 ms |
18416 KB |
Output is correct |
8 |
Correct |
11 ms |
18384 KB |
Output is correct |
9 |
Correct |
10 ms |
18396 KB |
Output is correct |
10 |
Correct |
9 ms |
18388 KB |
Output is correct |
11 |
Correct |
8 ms |
18384 KB |
Output is correct |
12 |
Correct |
8 ms |
18384 KB |
Output is correct |
13 |
Execution timed out |
3086 ms |
20800 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18256 KB |
Output is correct |
2 |
Correct |
7 ms |
18256 KB |
Output is correct |
3 |
Correct |
92 ms |
29668 KB |
Output is correct |
4 |
Correct |
108 ms |
30192 KB |
Output is correct |
5 |
Correct |
94 ms |
30228 KB |
Output is correct |
6 |
Correct |
96 ms |
30240 KB |
Output is correct |
7 |
Correct |
122 ms |
30200 KB |
Output is correct |
8 |
Correct |
121 ms |
30268 KB |
Output is correct |
9 |
Correct |
7 ms |
18256 KB |
Output is correct |
10 |
Correct |
8 ms |
18324 KB |
Output is correct |
11 |
Correct |
7 ms |
18384 KB |
Output is correct |
12 |
Correct |
8 ms |
18384 KB |
Output is correct |
13 |
Correct |
8 ms |
18372 KB |
Output is correct |
14 |
Correct |
8 ms |
18384 KB |
Output is correct |
15 |
Correct |
9 ms |
18416 KB |
Output is correct |
16 |
Correct |
11 ms |
18384 KB |
Output is correct |
17 |
Correct |
10 ms |
18396 KB |
Output is correct |
18 |
Correct |
9 ms |
18388 KB |
Output is correct |
19 |
Correct |
8 ms |
18384 KB |
Output is correct |
20 |
Correct |
8 ms |
18384 KB |
Output is correct |
21 |
Correct |
8 ms |
18384 KB |
Output is correct |
22 |
Correct |
9 ms |
18384 KB |
Output is correct |
23 |
Correct |
8 ms |
18384 KB |
Output is correct |
24 |
Correct |
10 ms |
18384 KB |
Output is correct |
25 |
Correct |
9 ms |
18384 KB |
Output is correct |
26 |
Correct |
10 ms |
18384 KB |
Output is correct |
27 |
Correct |
8 ms |
18384 KB |
Output is correct |
28 |
Correct |
11 ms |
18384 KB |
Output is correct |
29 |
Correct |
101 ms |
30288 KB |
Output is correct |
30 |
Correct |
102 ms |
30180 KB |
Output is correct |
31 |
Correct |
8 ms |
18384 KB |
Output is correct |
32 |
Correct |
8 ms |
18384 KB |
Output is correct |
33 |
Correct |
10 ms |
18384 KB |
Output is correct |
34 |
Correct |
8 ms |
18316 KB |
Output is correct |
35 |
Correct |
20 ms |
19576 KB |
Output is correct |
36 |
Correct |
9 ms |
18384 KB |
Output is correct |
37 |
Correct |
112 ms |
30400 KB |
Output is correct |
38 |
Correct |
106 ms |
30384 KB |
Output is correct |
39 |
Correct |
8 ms |
18384 KB |
Output is correct |
40 |
Correct |
9 ms |
18380 KB |
Output is correct |
41 |
Correct |
8 ms |
18384 KB |
Output is correct |
42 |
Correct |
10 ms |
18508 KB |
Output is correct |
43 |
Execution timed out |
3025 ms |
18512 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18256 KB |
Output is correct |
2 |
Correct |
7 ms |
18256 KB |
Output is correct |
3 |
Correct |
92 ms |
29668 KB |
Output is correct |
4 |
Correct |
108 ms |
30192 KB |
Output is correct |
5 |
Correct |
94 ms |
30228 KB |
Output is correct |
6 |
Correct |
96 ms |
30240 KB |
Output is correct |
7 |
Correct |
122 ms |
30200 KB |
Output is correct |
8 |
Correct |
121 ms |
30268 KB |
Output is correct |
9 |
Correct |
7 ms |
18256 KB |
Output is correct |
10 |
Correct |
8 ms |
18324 KB |
Output is correct |
11 |
Correct |
7 ms |
18384 KB |
Output is correct |
12 |
Correct |
8 ms |
18384 KB |
Output is correct |
13 |
Correct |
8 ms |
18372 KB |
Output is correct |
14 |
Correct |
8 ms |
18384 KB |
Output is correct |
15 |
Correct |
9 ms |
18416 KB |
Output is correct |
16 |
Correct |
11 ms |
18384 KB |
Output is correct |
17 |
Correct |
10 ms |
18396 KB |
Output is correct |
18 |
Correct |
9 ms |
18388 KB |
Output is correct |
19 |
Correct |
8 ms |
18384 KB |
Output is correct |
20 |
Correct |
8 ms |
18384 KB |
Output is correct |
21 |
Correct |
8 ms |
18384 KB |
Output is correct |
22 |
Correct |
9 ms |
18384 KB |
Output is correct |
23 |
Correct |
8 ms |
18384 KB |
Output is correct |
24 |
Correct |
10 ms |
18384 KB |
Output is correct |
25 |
Correct |
9 ms |
18384 KB |
Output is correct |
26 |
Correct |
10 ms |
18384 KB |
Output is correct |
27 |
Correct |
8 ms |
18384 KB |
Output is correct |
28 |
Correct |
11 ms |
18384 KB |
Output is correct |
29 |
Correct |
101 ms |
30288 KB |
Output is correct |
30 |
Correct |
102 ms |
30180 KB |
Output is correct |
31 |
Correct |
8 ms |
18384 KB |
Output is correct |
32 |
Correct |
8 ms |
18384 KB |
Output is correct |
33 |
Correct |
10 ms |
18384 KB |
Output is correct |
34 |
Correct |
8 ms |
18316 KB |
Output is correct |
35 |
Correct |
20 ms |
19576 KB |
Output is correct |
36 |
Correct |
9 ms |
18384 KB |
Output is correct |
37 |
Correct |
112 ms |
30400 KB |
Output is correct |
38 |
Correct |
106 ms |
30384 KB |
Output is correct |
39 |
Correct |
8 ms |
18384 KB |
Output is correct |
40 |
Correct |
9 ms |
18380 KB |
Output is correct |
41 |
Correct |
8 ms |
18384 KB |
Output is correct |
42 |
Correct |
10 ms |
18508 KB |
Output is correct |
43 |
Execution timed out |
3086 ms |
20800 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |