#include "circuit.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1'000'002'022;
const ll totient = 497758632;//a^(totient-1) = inv(a)
int n, m;
vector<ll> storage;//When you call the constructor, make sure that everything is reduced modulo MOD
vector<int> a;
ll pow(ll a, ll b)
{
if (b==0ll)
return 1;
ll c = pow(a, b/2ll);
c = (c*c)%MOD;
if (((b/2ll)*(2ll)) != b)//b is odd
c = (c*a)%MOD;
return c;
}
ll inv(ll a)
{
return pow(a, totient-1ll);
}
class Lazy{
public:
int L, R;
ll total_sum;
ll active;//MAKE SURE THAT THIS IS ALWAYS ACCURATE
bool flip;//When I need to flip, I don't need to flip myself, I need to flip my children only
Lazy* left;
Lazy* right;
Lazy()
{
L = 0; R = 0;
total_sum = 0; active = 0; flip = 0;
left = nullptr; right = nullptr;
}
Lazy(int l, int r)
{
L = l; R = r;
flip = 0;
if (L==R)
{
left = nullptr;
right = nullptr;
total_sum = storage[L];
if (a[L] == 1)
active = total_sum;
else
active = 0;
}
else
{
int mid = (l+r)/2;
left = new Lazy(l, mid);
right = new Lazy(mid+1, r);
total_sum = left->total_sum + right->total_sum;
active = left->active + right->active;
}
}
void update()
{
//assumption: My children could have just updated themselves, and so should I
if (left!=nullptr)
active = left->active + right->active;
}
void pancake_flip()
{
//assumption: this is updated, and I want to update myself
active = total_sum - active;
if (flip == 0)
flip = 1;
else
flip = 0;
}
void push_down()
{
//assumption: this is updated, and I want to update my children
if (flip == 1)
{
flip = 0;
if (left != nullptr)
{
left->pancake_flip();
right->pancake_flip();
}
}
}
void range_flip(int range_l, int range_r)
{
int intersect_l = max(range_l, L);
int intersect_r = min(range_r, R);
if ((intersect_l == L) and (intersect_r==R))
{
pancake_flip();
push_down();
}
else if (intersect_l <= intersect_r)
{
push_down();
left->range_flip(range_l, range_r);
right->range_flip(range_l, range_r);
update();
}
}
};
Lazy circ = Lazy();
struct factor{
public:
ll mn;
ll f2;
ll f223;
factor()
{
mn = 1;
f2 = 0;
f223 = 0;
}
factor(ll a, ll b, ll c)
{
mn = a;
f2 = b;
f223 = c;
}
factor(ll n)
{
//call this constructor for the degrees
f2 = 0;
f223 = 0;
while ((n%2)==0)
{
n = n/2;
f2++;
}
while ((n%223)==0)
{
n = n/223;
f223++;
}
mn = n;
}
factor multiply(factor x, factor y)
{
ll a = (x.mn * y.mn)%MOD;
ll b = (x.f2 + y.f2);
ll c = (x.f223 + y.f223);
factor ans = factor(a, b, c);
return ans;
}
factor divide(factor x, factor y)
{
ll d = inv(y.mn);
factor mult = factor(d, 0ll-y.f2, 0ll-y.f223);
return multiply(x, mult);
}
ll conv()
{
ll bb = pow(2ll, f2);
ll cc = pow(223ll, f223);
ll ans = (mn*bb)%MOD;
ans = (ans*cc)%MOD;
return ans;
}
};
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
m = M;
vector<ll> degrees(N, 0);
for (int i=1; i<(N+M); i++)
degrees[P[i]]++;
vector<factor> orig(N);
for (int i=0; i<N; i++)
orig[i] = factor(degrees[i]);
vector<factor> prods(N);
prods[0] = orig[0];
for (int i=1; i<N; i++)
prods[i] = prods[i].multiply(prods[i-1], orig[i]);
vector<factor> quots(N);
quots[0] = prods[N-1].divide(prods[N-1], orig[0]);
for (int i=1; i<N; i++)
quots[i] = quots[i].divide(quots[P[i]], orig[i]);
for (int i=N; i<(N+M); i++)
{
storage.push_back(quots[P[i]].conv());
}
//NOTES: Fill in storage with the appropriate values! Initially it is empty.
//MAKE SURE TO REDUCE ALL VALLUES MODULO MOD, THANK YOU!
a = A;
circ = Lazy(0, M-1);
}
int count_ways(int L, int R) {
L -= n;
R -= n;
circ.range_flip(L, R);
ll fa = circ.active%MOD;
int ffa = fa;
return ffa;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
532 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
608 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
600 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
475 ms |
8148 KB |
Output is correct |
2 |
Correct |
681 ms |
15916 KB |
Output is correct |
3 |
Correct |
694 ms |
16076 KB |
Output is correct |
4 |
Correct |
708 ms |
16072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
475 ms |
8148 KB |
Output is correct |
2 |
Correct |
681 ms |
15916 KB |
Output is correct |
3 |
Correct |
694 ms |
16076 KB |
Output is correct |
4 |
Correct |
708 ms |
16072 KB |
Output is correct |
5 |
Correct |
562 ms |
8148 KB |
Output is correct |
6 |
Correct |
731 ms |
16072 KB |
Output is correct |
7 |
Correct |
763 ms |
16072 KB |
Output is correct |
8 |
Correct |
666 ms |
16072 KB |
Output is correct |
9 |
Correct |
318 ms |
856 KB |
Output is correct |
10 |
Correct |
645 ms |
1368 KB |
Output is correct |
11 |
Correct |
645 ms |
1368 KB |
Output is correct |
12 |
Correct |
614 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
475 ms |
8148 KB |
Output is correct |
14 |
Correct |
681 ms |
15916 KB |
Output is correct |
15 |
Correct |
694 ms |
16076 KB |
Output is correct |
16 |
Correct |
708 ms |
16072 KB |
Output is correct |
17 |
Correct |
562 ms |
8148 KB |
Output is correct |
18 |
Correct |
731 ms |
16072 KB |
Output is correct |
19 |
Correct |
763 ms |
16072 KB |
Output is correct |
20 |
Correct |
666 ms |
16072 KB |
Output is correct |
21 |
Correct |
318 ms |
856 KB |
Output is correct |
22 |
Correct |
645 ms |
1368 KB |
Output is correct |
23 |
Correct |
645 ms |
1368 KB |
Output is correct |
24 |
Correct |
614 ms |
1368 KB |
Output is correct |
25 |
Correct |
757 ms |
23716 KB |
Output is correct |
26 |
Correct |
753 ms |
24264 KB |
Output is correct |
27 |
Correct |
775 ms |
24256 KB |
Output is correct |
28 |
Correct |
601 ms |
24192 KB |
Output is correct |
29 |
Correct |
755 ms |
24260 KB |
Output is correct |
30 |
Correct |
735 ms |
24260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
532 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
608 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
600 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
510 ms |
856 KB |
Output is correct |
44 |
Correct |
616 ms |
1112 KB |
Output is correct |
45 |
Correct |
667 ms |
1112 KB |
Output is correct |
46 |
Correct |
632 ms |
1624 KB |
Output is correct |
47 |
Correct |
614 ms |
1624 KB |
Output is correct |
48 |
Correct |
701 ms |
1624 KB |
Output is correct |
49 |
Correct |
666 ms |
1620 KB |
Output is correct |
50 |
Correct |
613 ms |
1624 KB |
Output is correct |
51 |
Correct |
597 ms |
1112 KB |
Output is correct |
52 |
Correct |
626 ms |
1112 KB |
Output is correct |
53 |
Correct |
579 ms |
856 KB |
Output is correct |
54 |
Correct |
669 ms |
1624 KB |
Output is correct |
55 |
Correct |
661 ms |
1368 KB |
Output is correct |
56 |
Correct |
648 ms |
1112 KB |
Output is correct |
57 |
Correct |
626 ms |
1112 KB |
Output is correct |
58 |
Correct |
635 ms |
1624 KB |
Output is correct |
59 |
Correct |
632 ms |
1624 KB |
Output is correct |
60 |
Correct |
622 ms |
1624 KB |
Output is correct |
61 |
Correct |
672 ms |
1112 KB |
Output is correct |
62 |
Correct |
661 ms |
1112 KB |
Output is correct |
63 |
Correct |
658 ms |
1112 KB |
Output is correct |
64 |
Correct |
671 ms |
1112 KB |
Output is correct |
65 |
Correct |
327 ms |
856 KB |
Output is correct |
66 |
Correct |
654 ms |
1368 KB |
Output is correct |
67 |
Correct |
684 ms |
1368 KB |
Output is correct |
68 |
Correct |
629 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
532 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
608 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
600 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
475 ms |
8148 KB |
Output is correct |
44 |
Correct |
681 ms |
15916 KB |
Output is correct |
45 |
Correct |
694 ms |
16076 KB |
Output is correct |
46 |
Correct |
708 ms |
16072 KB |
Output is correct |
47 |
Correct |
562 ms |
8148 KB |
Output is correct |
48 |
Correct |
731 ms |
16072 KB |
Output is correct |
49 |
Correct |
763 ms |
16072 KB |
Output is correct |
50 |
Correct |
666 ms |
16072 KB |
Output is correct |
51 |
Correct |
318 ms |
856 KB |
Output is correct |
52 |
Correct |
645 ms |
1368 KB |
Output is correct |
53 |
Correct |
645 ms |
1368 KB |
Output is correct |
54 |
Correct |
614 ms |
1368 KB |
Output is correct |
55 |
Correct |
757 ms |
23716 KB |
Output is correct |
56 |
Correct |
753 ms |
24264 KB |
Output is correct |
57 |
Correct |
775 ms |
24256 KB |
Output is correct |
58 |
Correct |
601 ms |
24192 KB |
Output is correct |
59 |
Correct |
755 ms |
24260 KB |
Output is correct |
60 |
Correct |
735 ms |
24260 KB |
Output is correct |
61 |
Correct |
510 ms |
856 KB |
Output is correct |
62 |
Correct |
616 ms |
1112 KB |
Output is correct |
63 |
Correct |
667 ms |
1112 KB |
Output is correct |
64 |
Correct |
632 ms |
1624 KB |
Output is correct |
65 |
Correct |
614 ms |
1624 KB |
Output is correct |
66 |
Correct |
701 ms |
1624 KB |
Output is correct |
67 |
Correct |
666 ms |
1620 KB |
Output is correct |
68 |
Correct |
613 ms |
1624 KB |
Output is correct |
69 |
Correct |
597 ms |
1112 KB |
Output is correct |
70 |
Correct |
626 ms |
1112 KB |
Output is correct |
71 |
Correct |
579 ms |
856 KB |
Output is correct |
72 |
Correct |
669 ms |
1624 KB |
Output is correct |
73 |
Correct |
661 ms |
1368 KB |
Output is correct |
74 |
Correct |
648 ms |
1112 KB |
Output is correct |
75 |
Correct |
626 ms |
1112 KB |
Output is correct |
76 |
Correct |
635 ms |
1624 KB |
Output is correct |
77 |
Correct |
632 ms |
1624 KB |
Output is correct |
78 |
Correct |
622 ms |
1624 KB |
Output is correct |
79 |
Correct |
672 ms |
1112 KB |
Output is correct |
80 |
Correct |
661 ms |
1112 KB |
Output is correct |
81 |
Correct |
658 ms |
1112 KB |
Output is correct |
82 |
Correct |
671 ms |
1112 KB |
Output is correct |
83 |
Correct |
327 ms |
856 KB |
Output is correct |
84 |
Correct |
654 ms |
1368 KB |
Output is correct |
85 |
Correct |
684 ms |
1368 KB |
Output is correct |
86 |
Correct |
629 ms |
1368 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |
88 |
Correct |
434 ms |
20804 KB |
Output is correct |
89 |
Correct |
704 ms |
14516 KB |
Output is correct |
90 |
Correct |
694 ms |
14800 KB |
Output is correct |
91 |
Correct |
694 ms |
24264 KB |
Output is correct |
92 |
Correct |
762 ms |
24300 KB |
Output is correct |
93 |
Correct |
745 ms |
24260 KB |
Output is correct |
94 |
Correct |
718 ms |
24260 KB |
Output is correct |
95 |
Correct |
685 ms |
24288 KB |
Output is correct |
96 |
Correct |
678 ms |
15564 KB |
Output is correct |
97 |
Correct |
743 ms |
15564 KB |
Output is correct |
98 |
Correct |
635 ms |
9048 KB |
Output is correct |
99 |
Correct |
744 ms |
24260 KB |
Output is correct |
100 |
Correct |
730 ms |
19908 KB |
Output is correct |
101 |
Correct |
736 ms |
17824 KB |
Output is correct |
102 |
Correct |
733 ms |
15556 KB |
Output is correct |
103 |
Correct |
777 ms |
24264 KB |
Output is correct |
104 |
Correct |
746 ms |
24288 KB |
Output is correct |
105 |
Correct |
770 ms |
24260 KB |
Output is correct |
106 |
Correct |
679 ms |
13264 KB |
Output is correct |
107 |
Correct |
713 ms |
14024 KB |
Output is correct |
108 |
Correct |
703 ms |
14796 KB |
Output is correct |
109 |
Correct |
705 ms |
15556 KB |
Output is correct |