// correct/solution-prabowo-inverse.cpp
// O(N + M log MOD + Q log M)
#include "circuit.h"
#include <functional>
#include <utility>
#include <vector>
const int kMod = 1000002022;
const int kPhiMod = kMod / 2 / 223 * 222 / 2242157 * 2242156;
int power(int x, int y) {
int result = 1;
for (; y > 0; y >>= 1) {
if (y & 1) {
result = static_cast<long long>(result) * x % kMod;
}
x = static_cast<long long>(x) * x % kMod;
}
return result;
}
struct Z {
int c, e2, e223;
Z(int val=1) {
e2 = 0, e223 = 0, c = val;
while (c % 2 == 0) {
++e2;
c /= 2;
}
while (c % 223 == 0) {
++e223;
c /= 223;
}
}
Z& operator *= (const Z &rhs) {
c = static_cast<long long>(c) * rhs.c % kMod;
e2 += rhs.e2;
e223 += rhs.e223;
return *this;
}
friend Z operator / (Z lhs, Z rhs) {
lhs.c = static_cast<long long>(lhs.c) * power(rhs.c, kPhiMod - 1) % kMod;
lhs.e2 -= rhs.e2;
lhs.e223 -= rhs.e223;
return lhs;
}
int value() {
return static_cast<long long>(c) *
power(2, e2) % kMod * power(223, e223) % kMod;
}
};
int N;
struct SegTree {
private:
int n;
struct Node {
int val[2];
bool lazy;
Node(int v0=0, int v1=0) {
val[0] = v0;
val[1] = v1;
lazy = false;
}
Node operator + (const Node &other) {
return Node((val[0] + other.val[0]) % kMod,
(val[1] + other.val[1]) % kMod);
}
};
std::vector<Node> nodes;
inline void toggle(int idx) {
std::swap(nodes[idx].val[0], nodes[idx].val[1]);
nodes[idx].lazy = !nodes[idx].lazy;
}
inline void pull(int idx, int l, int r) {
int mid = (l + r) >> 1;
nodes[idx] = nodes[idx + 1] + nodes[idx + (mid - l) * 2];
}
inline void push(int idx, int l, int r) {
if (!nodes[idx].lazy) return;
int mid = (l + r) >> 1;
toggle(idx + 1);
toggle(idx + (mid - l) * 2);
nodes[idx].lazy = false;
}
void build(int idx, int l, int r, const std::vector<std::pair<int, int>> &v) {
if (l + 1 == r) {
nodes[idx] = Node(v[l].first, v[l].second);
return;
}
int mid = (l + r) >> 1;
build(idx + 1, l, mid, v);
build(idx + (mid - l) * 2, mid, r, v);
pull(idx, l, r);
}
void update(int idx, int l, int r, int ll, int rr) {
if (l >= rr || r <= ll) {
return;
}
if (l >= ll && r <= rr) {
return toggle(idx);
}
push(idx, l, r);
int mid = (l + r) >> 1;
update(idx + 1, l, mid, ll, rr);
update(idx + (mid - l) * 2, mid, r, ll, rr);
pull(idx, l, r);
}
public:
void init(const std::vector<std::pair<int, int>> &v) {
n = static_cast<int>(v.size());
nodes.resize(2 * n - 1);
build(0, 0, n, v);
}
void update(int l, int r) {
update(0, 0, n, l, r);
}
int query() {
return nodes[0].val[1];
}
} segtree;
void init(int _N, int M, std::vector<int> P, std::vector<int> A) {
N = _N;
std::vector<std::vector<int>> adj(N + M);
for (int i = 1; i < N + M; ++i) {
adj[P[i]].push_back(i);
}
Z product;
for (int i = 0; i < N; ++i) {
product *= Z(static_cast<int>(adj[i].size()));
}
std::vector<int> contribution(M);
std::function<void(int, Z)> dfs = [&](int u, Z path) {
if (u >= N) {
contribution[u - N] = (product / path).value();
return;
}
path *= Z(static_cast<int>(adj[u].size()));
for (int v : adj[u]) {
dfs(v, path);
}
};
dfs(0, Z());
std::vector<std::pair<int, int>> v(M);
for (int i = 0; i < M; ++i) {
if (A[i]) {
v[i].second = contribution[i];
} else {
v[i].first = contribution[i];
}
}
segtree.init(v);
}
int count_ways(int L, int R) {
segtree.update(L - N, R - N + 1);
return segtree.query();
}
# |
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 |
368 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 |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
220 KB |
Output is correct |
2 |
Correct |
0 ms |
220 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
384 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
476 KB |
Output is correct |
12 |
Correct |
1 ms |
348 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 |
368 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 |
380 KB |
Output is correct |
9 |
Correct |
0 ms |
220 KB |
Output is correct |
10 |
Correct |
0 ms |
220 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
384 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 |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
476 KB |
Output is correct |
19 |
Correct |
1 ms |
476 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
352 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
476 KB |
Output is correct |
37 |
Correct |
1 ms |
476 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 |
368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
652 ms |
4776 KB |
Output is correct |
2 |
Correct |
1084 ms |
9300 KB |
Output is correct |
3 |
Correct |
790 ms |
9268 KB |
Output is correct |
4 |
Correct |
1159 ms |
9336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
652 ms |
4776 KB |
Output is correct |
2 |
Correct |
1084 ms |
9300 KB |
Output is correct |
3 |
Correct |
790 ms |
9268 KB |
Output is correct |
4 |
Correct |
1159 ms |
9336 KB |
Output is correct |
5 |
Correct |
890 ms |
4748 KB |
Output is correct |
6 |
Correct |
886 ms |
9264 KB |
Output is correct |
7 |
Correct |
970 ms |
9260 KB |
Output is correct |
8 |
Correct |
1209 ms |
9328 KB |
Output is correct |
9 |
Correct |
491 ms |
568 KB |
Output is correct |
10 |
Correct |
1274 ms |
848 KB |
Output is correct |
11 |
Correct |
987 ms |
832 KB |
Output is correct |
12 |
Correct |
834 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
220 KB |
Output is correct |
2 |
Correct |
0 ms |
220 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
384 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
476 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
652 ms |
4776 KB |
Output is correct |
14 |
Correct |
1084 ms |
9300 KB |
Output is correct |
15 |
Correct |
790 ms |
9268 KB |
Output is correct |
16 |
Correct |
1159 ms |
9336 KB |
Output is correct |
17 |
Correct |
890 ms |
4748 KB |
Output is correct |
18 |
Correct |
886 ms |
9264 KB |
Output is correct |
19 |
Correct |
970 ms |
9260 KB |
Output is correct |
20 |
Correct |
1209 ms |
9328 KB |
Output is correct |
21 |
Correct |
491 ms |
568 KB |
Output is correct |
22 |
Correct |
1274 ms |
848 KB |
Output is correct |
23 |
Correct |
987 ms |
832 KB |
Output is correct |
24 |
Correct |
834 ms |
720 KB |
Output is correct |
25 |
Correct |
1121 ms |
13736 KB |
Output is correct |
26 |
Correct |
1116 ms |
13984 KB |
Output is correct |
27 |
Correct |
1120 ms |
13920 KB |
Output is correct |
28 |
Correct |
1018 ms |
13864 KB |
Output is correct |
29 |
Correct |
1046 ms |
24900 KB |
Output is correct |
30 |
Correct |
1124 ms |
24872 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 |
368 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 |
380 KB |
Output is correct |
9 |
Correct |
0 ms |
220 KB |
Output is correct |
10 |
Correct |
0 ms |
220 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
384 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 |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
476 KB |
Output is correct |
19 |
Correct |
1 ms |
476 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
352 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
476 KB |
Output is correct |
37 |
Correct |
1 ms |
476 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 |
368 KB |
Output is correct |
43 |
Correct |
787 ms |
640 KB |
Output is correct |
44 |
Correct |
1096 ms |
772 KB |
Output is correct |
45 |
Correct |
1034 ms |
592 KB |
Output is correct |
46 |
Correct |
1098 ms |
976 KB |
Output is correct |
47 |
Correct |
865 ms |
1024 KB |
Output is correct |
48 |
Correct |
1000 ms |
976 KB |
Output is correct |
49 |
Correct |
1025 ms |
1004 KB |
Output is correct |
50 |
Correct |
715 ms |
1004 KB |
Output is correct |
51 |
Correct |
1151 ms |
672 KB |
Output is correct |
52 |
Correct |
999 ms |
672 KB |
Output is correct |
53 |
Correct |
549 ms |
1104 KB |
Output is correct |
54 |
Correct |
987 ms |
1024 KB |
Output is correct |
55 |
Correct |
952 ms |
720 KB |
Output is correct |
56 |
Correct |
1079 ms |
768 KB |
Output is correct |
57 |
Correct |
991 ms |
656 KB |
Output is correct |
58 |
Correct |
782 ms |
1500 KB |
Output is correct |
59 |
Correct |
949 ms |
1428 KB |
Output is correct |
60 |
Correct |
1056 ms |
1488 KB |
Output is correct |
61 |
Correct |
927 ms |
764 KB |
Output is correct |
62 |
Correct |
748 ms |
656 KB |
Output is correct |
63 |
Correct |
972 ms |
644 KB |
Output is correct |
64 |
Correct |
811 ms |
664 KB |
Output is correct |
65 |
Correct |
593 ms |
556 KB |
Output is correct |
66 |
Correct |
883 ms |
848 KB |
Output is correct |
67 |
Correct |
1149 ms |
844 KB |
Output is correct |
68 |
Correct |
944 ms |
848 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 |
368 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 |
380 KB |
Output is correct |
9 |
Correct |
0 ms |
220 KB |
Output is correct |
10 |
Correct |
0 ms |
220 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
384 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 |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
476 KB |
Output is correct |
19 |
Correct |
1 ms |
476 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
352 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
476 KB |
Output is correct |
37 |
Correct |
1 ms |
476 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 |
368 KB |
Output is correct |
43 |
Correct |
652 ms |
4776 KB |
Output is correct |
44 |
Correct |
1084 ms |
9300 KB |
Output is correct |
45 |
Correct |
790 ms |
9268 KB |
Output is correct |
46 |
Correct |
1159 ms |
9336 KB |
Output is correct |
47 |
Correct |
890 ms |
4748 KB |
Output is correct |
48 |
Correct |
886 ms |
9264 KB |
Output is correct |
49 |
Correct |
970 ms |
9260 KB |
Output is correct |
50 |
Correct |
1209 ms |
9328 KB |
Output is correct |
51 |
Correct |
491 ms |
568 KB |
Output is correct |
52 |
Correct |
1274 ms |
848 KB |
Output is correct |
53 |
Correct |
987 ms |
832 KB |
Output is correct |
54 |
Correct |
834 ms |
720 KB |
Output is correct |
55 |
Correct |
1121 ms |
13736 KB |
Output is correct |
56 |
Correct |
1116 ms |
13984 KB |
Output is correct |
57 |
Correct |
1120 ms |
13920 KB |
Output is correct |
58 |
Correct |
1018 ms |
13864 KB |
Output is correct |
59 |
Correct |
1046 ms |
24900 KB |
Output is correct |
60 |
Correct |
1124 ms |
24872 KB |
Output is correct |
61 |
Correct |
787 ms |
640 KB |
Output is correct |
62 |
Correct |
1096 ms |
772 KB |
Output is correct |
63 |
Correct |
1034 ms |
592 KB |
Output is correct |
64 |
Correct |
1098 ms |
976 KB |
Output is correct |
65 |
Correct |
865 ms |
1024 KB |
Output is correct |
66 |
Correct |
1000 ms |
976 KB |
Output is correct |
67 |
Correct |
1025 ms |
1004 KB |
Output is correct |
68 |
Correct |
715 ms |
1004 KB |
Output is correct |
69 |
Correct |
1151 ms |
672 KB |
Output is correct |
70 |
Correct |
999 ms |
672 KB |
Output is correct |
71 |
Correct |
549 ms |
1104 KB |
Output is correct |
72 |
Correct |
987 ms |
1024 KB |
Output is correct |
73 |
Correct |
952 ms |
720 KB |
Output is correct |
74 |
Correct |
1079 ms |
768 KB |
Output is correct |
75 |
Correct |
991 ms |
656 KB |
Output is correct |
76 |
Correct |
782 ms |
1500 KB |
Output is correct |
77 |
Correct |
949 ms |
1428 KB |
Output is correct |
78 |
Correct |
1056 ms |
1488 KB |
Output is correct |
79 |
Correct |
927 ms |
764 KB |
Output is correct |
80 |
Correct |
748 ms |
656 KB |
Output is correct |
81 |
Correct |
972 ms |
644 KB |
Output is correct |
82 |
Correct |
811 ms |
664 KB |
Output is correct |
83 |
Correct |
593 ms |
556 KB |
Output is correct |
84 |
Correct |
883 ms |
848 KB |
Output is correct |
85 |
Correct |
1149 ms |
844 KB |
Output is correct |
86 |
Correct |
944 ms |
848 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
433 ms |
12308 KB |
Output is correct |
89 |
Correct |
1208 ms |
8768 KB |
Output is correct |
90 |
Correct |
999 ms |
8652 KB |
Output is correct |
91 |
Correct |
1111 ms |
13968 KB |
Output is correct |
92 |
Correct |
974 ms |
14036 KB |
Output is correct |
93 |
Correct |
764 ms |
14036 KB |
Output is correct |
94 |
Correct |
1275 ms |
14032 KB |
Output is correct |
95 |
Correct |
1020 ms |
14040 KB |
Output is correct |
96 |
Correct |
1154 ms |
8176 KB |
Output is correct |
97 |
Correct |
1417 ms |
8220 KB |
Output is correct |
98 |
Correct |
898 ms |
17432 KB |
Output is correct |
99 |
Correct |
1072 ms |
13892 KB |
Output is correct |
100 |
Correct |
937 ms |
10768 KB |
Output is correct |
101 |
Correct |
1012 ms |
9788 KB |
Output is correct |
102 |
Correct |
1058 ms |
8204 KB |
Output is correct |
103 |
Correct |
1251 ms |
24912 KB |
Output is correct |
104 |
Correct |
1064 ms |
25396 KB |
Output is correct |
105 |
Correct |
1041 ms |
25464 KB |
Output is correct |
106 |
Correct |
900 ms |
10572 KB |
Output is correct |
107 |
Correct |
1047 ms |
7784 KB |
Output is correct |
108 |
Correct |
1151 ms |
8048 KB |
Output is correct |
109 |
Correct |
1058 ms |
8240 KB |
Output is correct |