#include "circuit.h"
#include <bits/stdc++.h>
#ifdef GUDEB
#define D(x) cerr << #x << ": " << (x) << '\n';
#else
#define D(x) ;
#endif
using namespace std;
using ll = long long;
int n, m;
vector<int> ps;
vector<int> as;
vector<vector<int>> al;
vector<int> tss;
vector<int> tcs;
vector<int> td1;
vector<int> td2;
vector<int> ts1;
vector<int> ts2;
vector<int> mults;
constexpr int m2 = 2242157;
constexpr int mod = 2*223*m2;
constexpr int ord = 222*2242156;
int bexp(ll b, ll e) {
if(e < 0) e += ord;
ll r = 1;
while(e > 0) {
if(e&1) {
r *= b;
r %= mod;
}
e /= 2;
b *= b;
b %= mod;
}
return r;
}
void iwalk(int v) {
tcs[v] = 1;
tss[v] = 1;
int c = al[v].size()-1;
if(ps[v] == -1) c += 1;
if(c == 0) return;
while(c % 2 == 0) {
c /= 2;
ts1[v] += 1;
}
while(c % 223 == 0) {
c /= 223;
ts2[v] += 1;
}
tss[v] = c;
D(v)
D(tss[v])
for(auto a : al[v]) {
if(a == ps[v]) continue;
iwalk(a);
tcs[v] = (1ll*tcs[v]*tss[a]) % mod;
td1[v] += ts1[a];
td2[v] += ts2[a];
}
tss[v] = (1ll*tcs[v]*tss[v]) % mod;
ts1[v] += td1[v];
ts2[v] += td2[v];
}
void mwalk(int v, int ml, int d1, int d2) {
if(v >= n) {
mults[v-n] = ml;
mults[v-n] = (1ll*bexp(2, d1)*mults[v-n]) % mod;
mults[v-n] = (1ll*bexp(223, d2)*mults[v-n]) % mod;
}
for(auto a : al[v]) {
if(a == ps[v]) continue;
ll nm = 1ll*ml*tcs[v];
nm %= mod;
nm *= bexp(tss[a], -1);
nm %= mod;
mwalk(a, nm, d1+td1[v]-ts1[a], d2+td2[v]-ts2[a]);
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
m = M;
ps = std::move(P);
as = std::move(A);
al.resize(N+M);
mults.resize(M);
tcs.resize(N+M);
td1.resize(N+M);
td2.resize(N+M);
tss.resize(N+M);
ts1.resize(N+M);
ts2.resize(N+M);
for(int i = 1; i < n+m; ++i) {
al[i].push_back(ps[i]);
al[ps[i]].push_back(i);
}
iwalk(0);
mwalk(0, 1, 0, 0);
if(0) {
for(int i = 0; i < n+m; ++i) {
cerr << tcs[i] << ' ';
}
cerr << '\n';
for(int i = 0; i < m; ++i) {
cerr << mults[i] << ' ';
}
cerr << '\n';
}
}
int count_ways(int L, int R) {
L -= n;
R -= n;
++R;
int res = 0;
for(int i = L; i < R; ++i) {
as[i] = 1 - as[i];
}
for(int i = 0; i < m; ++i) {
res += as[i] * mults[i];
res %= mod;
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 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 |
464 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
2 ms |
464 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 |
0 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 |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 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 |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
2 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 |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
464 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 |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
464 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 |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 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 |
3086 ms |
6320 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3086 ms |
6320 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 |
336 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 |
464 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
2 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Execution timed out |
3086 ms |
6320 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 |
0 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 |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 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 |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
2 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 |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
464 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 |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
464 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 |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1224 ms |
720 KB |
Output is correct |
44 |
Correct |
1610 ms |
848 KB |
Output is correct |
45 |
Correct |
1917 ms |
848 KB |
Output is correct |
46 |
Correct |
2502 ms |
1232 KB |
Output is correct |
47 |
Correct |
2742 ms |
1188 KB |
Output is correct |
48 |
Correct |
2901 ms |
1232 KB |
Output is correct |
49 |
Correct |
2663 ms |
1232 KB |
Output is correct |
50 |
Correct |
2964 ms |
1232 KB |
Output is correct |
51 |
Correct |
2545 ms |
720 KB |
Output is correct |
52 |
Correct |
2717 ms |
720 KB |
Output is correct |
53 |
Correct |
430 ms |
1104 KB |
Output is correct |
54 |
Correct |
2798 ms |
1104 KB |
Output is correct |
55 |
Correct |
2676 ms |
976 KB |
Output is correct |
56 |
Correct |
2663 ms |
848 KB |
Output is correct |
57 |
Correct |
2512 ms |
720 KB |
Output is correct |
58 |
Correct |
2643 ms |
1616 KB |
Output is correct |
59 |
Correct |
2604 ms |
1576 KB |
Output is correct |
60 |
Correct |
2812 ms |
1616 KB |
Output is correct |
61 |
Correct |
1650 ms |
848 KB |
Output is correct |
62 |
Correct |
1887 ms |
808 KB |
Output is correct |
63 |
Correct |
2071 ms |
720 KB |
Output is correct |
64 |
Correct |
2781 ms |
804 KB |
Output is correct |
65 |
Correct |
756 ms |
592 KB |
Output is correct |
66 |
Correct |
2359 ms |
976 KB |
Output is correct |
67 |
Correct |
2247 ms |
976 KB |
Output is correct |
68 |
Correct |
2280 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 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 |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
2 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 |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
464 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 |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
464 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 |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Execution timed out |
3086 ms |
6320 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |