#include "circuit.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debug_out(){cerr<<endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cerr << H << ' ';
debug_out(T...);
}
const int mod = 1000002022;
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define add(x, y) (x + y >= mod? x+y-mod: x+y)
#define sub(x, y) (x - y < 0? x-y+mod: x-y)
const int maxn = 1e5 + 10;
int n, m, p[maxn << 1], a[maxn], dp[maxn], val[maxn], cnt[maxn][2], phi = 1 * 222 * 2242156;
pii seg[maxn << 2];
bool lazy[maxn << 2];
vector<int> g[maxn];
int pw(int a, int b){
// debug(a, b);
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) res = 1ll * res * a % mod;
// debug(res);
return res;
}
pair<int,pii> dfs(int v){
if (v >= n){
val[v] = 1;
return {1, {0, 0}};
}
int x = g[v].size();
while(x % 2 == 0){
cnt[v][0]++;
x /= 2;
}
while(x % 223 == 0){
cnt[v][1]++;
x /= 223;
}
val[v] = x;
pair<int,pii> res = {val[v], {cnt[v][0], cnt[v][1]}};
for (auto u: g[v]){
pair<int,pii> tmp = dfs(u);
res.F = 1ll * res.F * tmp.F % mod;
res.S.F += tmp.S.F;
res.S.S += tmp.S.S;
}
return res;
}
void solve(int v, pair<int,pii> tmp){
//debug(v);
if (v >= n){
dp[v-n] = 1ll * tmp.F * pw(2, tmp.S.F) % mod * pw(223, tmp.S.S) % mod;
// debug(v, dp[v-n], tmp.F, tmp.S.F, tmp.S.S);
return;
}
tmp.F = 1ll * tmp.F * pw(val[v], phi-1) % mod;
tmp.S.F -= cnt[v][0];
tmp.S.S -= cnt[v][1];
for (auto u: g[v]){
solve(u, tmp);
}
}
pii operator +(pii a, pii b){
return MP(add(a.F, b.F), add(a.S, b.S));
}
void shift(int v, int l, int r);
void build(int v, int l, int r){
if (l + 1 == r){
seg[v] = {dp[l], 0};
if (a[l] == 0) swap(seg[v].F, seg[v].S);
return;
}
int mid = (l + r) >> 1;
build(v << 1, l, mid);
build(v << 1 | 1, mid, r);
seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
void change(int v, int l, int r, int ql, int qr){
if (qr <= l || r <= ql) return;
if (ql <= l && r <= qr){
swap(seg[v].F, seg[v].S);
lazy[v] = !lazy[v];
return;
}
shift(v, l, r);
int mid = (l + r) >> 1;
change(v << 1, l, mid, ql, qr);
change(v << 1 | 1, mid, r, ql, qr);
seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
void shift(int v, int l, int r){
if (!lazy[v]) return;
int mid = (l + r) >> 1;
change(v << 1, l, mid, l, mid);
change(v << 1 | 1, mid, r, mid, r);
lazy[v] = false;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
m = M;
for (int i = 1; i < n+m; i++){
p[i] = P[i];
g[p[i]].push_back(i);
}
for (int i = 0; i < m; i++) a[i] = A[i];
// debug(1);
pair<int,pii> tmp = dfs(0);
// debug(tmp.F, tmp.S.F, tmp.S.S);
// debug(2);
solve(0, tmp);
// debug(3);
build(1, 0, m);
}
int count_ways(int L, int R) {
L -= n;
R -= n;
change(1, 0, m, L, R+1);
return seg[1].F;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2592 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 |
2612 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 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 |
2768 KB |
Output is correct |
7 |
Correct |
3 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2712 KB |
Output is correct |
10 |
Correct |
2 ms |
2768 KB |
Output is correct |
11 |
Correct |
2 ms |
2768 KB |
Output is correct |
12 |
Correct |
1 ms |
2768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2592 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 |
2612 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
1 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 |
2768 KB |
Output is correct |
15 |
Correct |
3 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2712 KB |
Output is correct |
18 |
Correct |
2 ms |
2768 KB |
Output is correct |
19 |
Correct |
2 ms |
2768 KB |
Output is correct |
20 |
Correct |
1 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
1 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2768 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
2 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
2860 KB |
Output is correct |
37 |
Correct |
3 ms |
2896 KB |
Output is correct |
38 |
Correct |
2 ms |
2844 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 |
2 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
449 ms |
5980 KB |
Output is correct |
2 |
Correct |
697 ms |
9180 KB |
Output is correct |
3 |
Correct |
708 ms |
9160 KB |
Output is correct |
4 |
Correct |
689 ms |
9172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
449 ms |
5980 KB |
Output is correct |
2 |
Correct |
697 ms |
9180 KB |
Output is correct |
3 |
Correct |
708 ms |
9160 KB |
Output is correct |
4 |
Correct |
689 ms |
9172 KB |
Output is correct |
5 |
Correct |
607 ms |
6108 KB |
Output is correct |
6 |
Correct |
731 ms |
9180 KB |
Output is correct |
7 |
Correct |
745 ms |
9184 KB |
Output is correct |
8 |
Correct |
797 ms |
9160 KB |
Output is correct |
9 |
Correct |
380 ms |
2896 KB |
Output is correct |
10 |
Correct |
624 ms |
3024 KB |
Output is correct |
11 |
Correct |
784 ms |
3024 KB |
Output is correct |
12 |
Correct |
773 ms |
3024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 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 |
2768 KB |
Output is correct |
7 |
Correct |
3 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2712 KB |
Output is correct |
10 |
Correct |
2 ms |
2768 KB |
Output is correct |
11 |
Correct |
2 ms |
2768 KB |
Output is correct |
12 |
Correct |
1 ms |
2768 KB |
Output is correct |
13 |
Correct |
449 ms |
5980 KB |
Output is correct |
14 |
Correct |
697 ms |
9180 KB |
Output is correct |
15 |
Correct |
708 ms |
9160 KB |
Output is correct |
16 |
Correct |
689 ms |
9172 KB |
Output is correct |
17 |
Correct |
607 ms |
6108 KB |
Output is correct |
18 |
Correct |
731 ms |
9180 KB |
Output is correct |
19 |
Correct |
745 ms |
9184 KB |
Output is correct |
20 |
Correct |
797 ms |
9160 KB |
Output is correct |
21 |
Correct |
380 ms |
2896 KB |
Output is correct |
22 |
Correct |
624 ms |
3024 KB |
Output is correct |
23 |
Correct |
784 ms |
3024 KB |
Output is correct |
24 |
Correct |
773 ms |
3024 KB |
Output is correct |
25 |
Correct |
675 ms |
12768 KB |
Output is correct |
26 |
Correct |
637 ms |
12928 KB |
Output is correct |
27 |
Correct |
875 ms |
12872 KB |
Output is correct |
28 |
Correct |
623 ms |
12912 KB |
Output is correct |
29 |
Correct |
816 ms |
23760 KB |
Output is correct |
30 |
Correct |
692 ms |
23852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2592 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 |
2612 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
1 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 |
2768 KB |
Output is correct |
15 |
Correct |
3 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2712 KB |
Output is correct |
18 |
Correct |
2 ms |
2768 KB |
Output is correct |
19 |
Correct |
2 ms |
2768 KB |
Output is correct |
20 |
Correct |
1 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
1 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2768 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
2 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
2860 KB |
Output is correct |
37 |
Correct |
3 ms |
2896 KB |
Output is correct |
38 |
Correct |
2 ms |
2844 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 |
2 ms |
2640 KB |
Output is correct |
43 |
Correct |
530 ms |
2896 KB |
Output is correct |
44 |
Correct |
673 ms |
2976 KB |
Output is correct |
45 |
Correct |
743 ms |
3024 KB |
Output is correct |
46 |
Correct |
703 ms |
3152 KB |
Output is correct |
47 |
Correct |
707 ms |
3152 KB |
Output is correct |
48 |
Correct |
771 ms |
3152 KB |
Output is correct |
49 |
Correct |
655 ms |
3152 KB |
Output is correct |
50 |
Correct |
714 ms |
3152 KB |
Output is correct |
51 |
Correct |
819 ms |
3024 KB |
Output is correct |
52 |
Correct |
722 ms |
3024 KB |
Output is correct |
53 |
Correct |
607 ms |
3408 KB |
Output is correct |
54 |
Correct |
608 ms |
3152 KB |
Output is correct |
55 |
Correct |
636 ms |
3024 KB |
Output is correct |
56 |
Correct |
639 ms |
3060 KB |
Output is correct |
57 |
Correct |
702 ms |
2896 KB |
Output is correct |
58 |
Correct |
581 ms |
3792 KB |
Output is correct |
59 |
Correct |
620 ms |
3800 KB |
Output is correct |
60 |
Correct |
755 ms |
3792 KB |
Output is correct |
61 |
Correct |
771 ms |
3152 KB |
Output is correct |
62 |
Correct |
714 ms |
2944 KB |
Output is correct |
63 |
Correct |
757 ms |
2896 KB |
Output is correct |
64 |
Correct |
742 ms |
2896 KB |
Output is correct |
65 |
Correct |
217 ms |
2768 KB |
Output is correct |
66 |
Correct |
509 ms |
3024 KB |
Output is correct |
67 |
Correct |
769 ms |
3024 KB |
Output is correct |
68 |
Correct |
619 ms |
3024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2592 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 |
2612 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
1 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 |
2768 KB |
Output is correct |
15 |
Correct |
3 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2712 KB |
Output is correct |
18 |
Correct |
2 ms |
2768 KB |
Output is correct |
19 |
Correct |
2 ms |
2768 KB |
Output is correct |
20 |
Correct |
1 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
1 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2768 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
2 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
2860 KB |
Output is correct |
37 |
Correct |
3 ms |
2896 KB |
Output is correct |
38 |
Correct |
2 ms |
2844 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 |
2 ms |
2640 KB |
Output is correct |
43 |
Correct |
449 ms |
5980 KB |
Output is correct |
44 |
Correct |
697 ms |
9180 KB |
Output is correct |
45 |
Correct |
708 ms |
9160 KB |
Output is correct |
46 |
Correct |
689 ms |
9172 KB |
Output is correct |
47 |
Correct |
607 ms |
6108 KB |
Output is correct |
48 |
Correct |
731 ms |
9180 KB |
Output is correct |
49 |
Correct |
745 ms |
9184 KB |
Output is correct |
50 |
Correct |
797 ms |
9160 KB |
Output is correct |
51 |
Correct |
380 ms |
2896 KB |
Output is correct |
52 |
Correct |
624 ms |
3024 KB |
Output is correct |
53 |
Correct |
784 ms |
3024 KB |
Output is correct |
54 |
Correct |
773 ms |
3024 KB |
Output is correct |
55 |
Correct |
675 ms |
12768 KB |
Output is correct |
56 |
Correct |
637 ms |
12928 KB |
Output is correct |
57 |
Correct |
875 ms |
12872 KB |
Output is correct |
58 |
Correct |
623 ms |
12912 KB |
Output is correct |
59 |
Correct |
816 ms |
23760 KB |
Output is correct |
60 |
Correct |
692 ms |
23852 KB |
Output is correct |
61 |
Correct |
530 ms |
2896 KB |
Output is correct |
62 |
Correct |
673 ms |
2976 KB |
Output is correct |
63 |
Correct |
743 ms |
3024 KB |
Output is correct |
64 |
Correct |
703 ms |
3152 KB |
Output is correct |
65 |
Correct |
707 ms |
3152 KB |
Output is correct |
66 |
Correct |
771 ms |
3152 KB |
Output is correct |
67 |
Correct |
655 ms |
3152 KB |
Output is correct |
68 |
Correct |
714 ms |
3152 KB |
Output is correct |
69 |
Correct |
819 ms |
3024 KB |
Output is correct |
70 |
Correct |
722 ms |
3024 KB |
Output is correct |
71 |
Correct |
607 ms |
3408 KB |
Output is correct |
72 |
Correct |
608 ms |
3152 KB |
Output is correct |
73 |
Correct |
636 ms |
3024 KB |
Output is correct |
74 |
Correct |
639 ms |
3060 KB |
Output is correct |
75 |
Correct |
702 ms |
2896 KB |
Output is correct |
76 |
Correct |
581 ms |
3792 KB |
Output is correct |
77 |
Correct |
620 ms |
3800 KB |
Output is correct |
78 |
Correct |
755 ms |
3792 KB |
Output is correct |
79 |
Correct |
771 ms |
3152 KB |
Output is correct |
80 |
Correct |
714 ms |
2944 KB |
Output is correct |
81 |
Correct |
757 ms |
2896 KB |
Output is correct |
82 |
Correct |
742 ms |
2896 KB |
Output is correct |
83 |
Correct |
217 ms |
2768 KB |
Output is correct |
84 |
Correct |
509 ms |
3024 KB |
Output is correct |
85 |
Correct |
769 ms |
3024 KB |
Output is correct |
86 |
Correct |
619 ms |
3024 KB |
Output is correct |
87 |
Correct |
1 ms |
2640 KB |
Output is correct |
88 |
Correct |
345 ms |
12184 KB |
Output is correct |
89 |
Correct |
863 ms |
9428 KB |
Output is correct |
90 |
Correct |
694 ms |
9104 KB |
Output is correct |
91 |
Correct |
630 ms |
13040 KB |
Output is correct |
92 |
Correct |
835 ms |
13008 KB |
Output is correct |
93 |
Correct |
826 ms |
12980 KB |
Output is correct |
94 |
Correct |
886 ms |
13016 KB |
Output is correct |
95 |
Correct |
861 ms |
13008 KB |
Output is correct |
96 |
Correct |
637 ms |
8392 KB |
Output is correct |
97 |
Correct |
846 ms |
8256 KB |
Output is correct |
98 |
Correct |
614 ms |
18248 KB |
Output is correct |
99 |
Correct |
834 ms |
12884 KB |
Output is correct |
100 |
Correct |
719 ms |
9928 KB |
Output is correct |
101 |
Correct |
610 ms |
9420 KB |
Output is correct |
102 |
Correct |
768 ms |
8300 KB |
Output is correct |
103 |
Correct |
802 ms |
23880 KB |
Output is correct |
104 |
Correct |
875 ms |
23596 KB |
Output is correct |
105 |
Correct |
765 ms |
23632 KB |
Output is correct |
106 |
Correct |
732 ms |
11492 KB |
Output is correct |
107 |
Correct |
732 ms |
9032 KB |
Output is correct |
108 |
Correct |
755 ms |
8892 KB |
Output is correct |
109 |
Correct |
730 ms |
8392 KB |
Output is correct |