#include "circuit.h"
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define forsn(i, s, n) for(int i=s; i<(int)n; ++i)
#define dforn(i, n) for(int i=n-1; i>=0; --i)
#define dforsn(i, s, n) for(int i=n-1; i>=s; --i)
#define PB push_back
const int MAXN = 100010, MOD = 1000002022;
int n, m, he;
int cal[MAXN][3];
int coef[MAXN];
int tr[2*MAXN], ax[2*MAXN];
bool lazy[MAXN];
vector<int> g[MAXN];
int crt(int x, int y, int z){
return (500001011LL*x + 143498048LL*y + 356502964LL*z)%1000002022;
}
ll ex(ll b, int e, int mod){
ll ret=1;
while(e>0){
if(e&1) ret=(ret*b)%mod;
b=(b*b)%mod;
e>>=1;
}
return ret;
}
void apply(int p){
if(p<m) lazy[p]^=1;
tr[p] = (ax[p] - tr[p] + MOD)%MOD;
}
void push(int p){
for(int s=he; s>0; --s){
int i = p>>s;
if(lazy[i]){
apply(i<<1);
apply(i<<1|1);
lazy[i]=0;
}
}
}
void build(int p){
while(p>1){
p>>=1;
int sum = (tr[p<<1] + tr[p<<1|1])%MOD;
tr[p] = lazy[p]? ((ax[p] - sum + MOD)%MOD) : sum;
}
}
void update(int l, int r){
l+=m, r+=m;
int l0=l, r0=r;
for(; l<r; l>>=1, r>>=1){
if(l&1) apply(l++);
if(r&1) apply(--r);
}
build(l0);
build(r0-1);
}
int query(int l, int r){
int ret=0;
l+=m, r+=m;
push(l);
push(r-1);
for(; l<r; l>>=1, r>>=1){
if(l&1) ret=(ret+tr[l++])%MOD;
if(r&1) ret=(ret+tr[--r])%MOD;
}
return ret;
}
void dfs(int v, int mod, int prod, int cnt, int ind){
if(v>=n){
cal[v-n][ind]=cnt>0? 0 : prod;
return;
}
int x = (int)g[v].size();
while(!(x%mod)) x/=mod, --cnt;
prod = (prod*ex(x, mod-2, mod))%mod;
for(int to:g[v]) dfs(to, mod, prod, cnt, ind);
}
void init(int N, int M, vector<int> P, vector<int> A) {
n=N, m=M, he = 8*sizeof(int) - __builtin_clz(m);
forn(i, n+m) g[P[i]].PB(i);
vector<int> fact = {2, 223, 2242157};
forn(ind, 3){
int pr = fact[ind];
int cnt=0;
ll prod=1;
forn(i, n){
int x = (int)g[i].size();
while(!(x%pr)) x/=pr, cnt++;
prod=(prod*x)%pr;
}
dfs(0, pr, prod, cnt, ind);
}
forn(i, m) coef[i] = crt(cal[i][0], cal[i][1], cal[i][2]);
forn(i, m) ax[m+i]=coef[i];
dforsn(i, 1, m) ax[i]=(ax[i<<1]+ax[i<<1|1])%MOD;
forn(i, m) if(A[i]) update(i, i+1);
}
int count_ways(int L, int R) {
update(L-n, R+1-n);
return query(0, m);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 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 |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2720 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2768 KB |
Output is correct |
11 |
Correct |
2 ms |
2768 KB |
Output is correct |
12 |
Correct |
2 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 |
2 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 |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2680 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2720 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
2 ms |
2640 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
2 ms |
2768 KB |
Output is correct |
19 |
Correct |
2 ms |
2768 KB |
Output is correct |
20 |
Correct |
2 ms |
2640 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 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 |
2708 KB |
Output is correct |
25 |
Correct |
2 ms |
2640 KB |
Output is correct |
26 |
Correct |
3 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2640 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 |
2640 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
1 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
2768 KB |
Output is correct |
37 |
Correct |
2 ms |
2828 KB |
Output is correct |
38 |
Correct |
2 ms |
2768 KB |
Output is correct |
39 |
Correct |
2 ms |
2640 KB |
Output is correct |
40 |
Correct |
3 ms |
2640 KB |
Output is correct |
41 |
Correct |
2 ms |
2640 KB |
Output is correct |
42 |
Correct |
1 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
473 ms |
5364 KB |
Output is correct |
2 |
Correct |
735 ms |
8188 KB |
Output is correct |
3 |
Correct |
780 ms |
8288 KB |
Output is correct |
4 |
Correct |
743 ms |
7796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
473 ms |
5364 KB |
Output is correct |
2 |
Correct |
735 ms |
8188 KB |
Output is correct |
3 |
Correct |
780 ms |
8288 KB |
Output is correct |
4 |
Correct |
743 ms |
7796 KB |
Output is correct |
5 |
Correct |
683 ms |
5448 KB |
Output is correct |
6 |
Correct |
847 ms |
8308 KB |
Output is correct |
7 |
Correct |
736 ms |
8296 KB |
Output is correct |
8 |
Correct |
765 ms |
8272 KB |
Output is correct |
9 |
Correct |
302 ms |
2768 KB |
Output is correct |
10 |
Correct |
785 ms |
2908 KB |
Output is correct |
11 |
Correct |
794 ms |
2912 KB |
Output is correct |
12 |
Correct |
676 ms |
2912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2720 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2768 KB |
Output is correct |
11 |
Correct |
2 ms |
2768 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
473 ms |
5364 KB |
Output is correct |
14 |
Correct |
735 ms |
8188 KB |
Output is correct |
15 |
Correct |
780 ms |
8288 KB |
Output is correct |
16 |
Correct |
743 ms |
7796 KB |
Output is correct |
17 |
Correct |
683 ms |
5448 KB |
Output is correct |
18 |
Correct |
847 ms |
8308 KB |
Output is correct |
19 |
Correct |
736 ms |
8296 KB |
Output is correct |
20 |
Correct |
765 ms |
8272 KB |
Output is correct |
21 |
Correct |
302 ms |
2768 KB |
Output is correct |
22 |
Correct |
785 ms |
2908 KB |
Output is correct |
23 |
Correct |
794 ms |
2912 KB |
Output is correct |
24 |
Correct |
676 ms |
2912 KB |
Output is correct |
25 |
Correct |
783 ms |
11092 KB |
Output is correct |
26 |
Correct |
950 ms |
11240 KB |
Output is correct |
27 |
Correct |
862 ms |
11324 KB |
Output is correct |
28 |
Correct |
636 ms |
11216 KB |
Output is correct |
29 |
Correct |
899 ms |
17484 KB |
Output is correct |
30 |
Correct |
537 ms |
17484 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 |
2 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 |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2680 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2720 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
2 ms |
2640 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
2 ms |
2768 KB |
Output is correct |
19 |
Correct |
2 ms |
2768 KB |
Output is correct |
20 |
Correct |
2 ms |
2640 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 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 |
2708 KB |
Output is correct |
25 |
Correct |
2 ms |
2640 KB |
Output is correct |
26 |
Correct |
3 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2640 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 |
2640 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
1 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
2768 KB |
Output is correct |
37 |
Correct |
2 ms |
2828 KB |
Output is correct |
38 |
Correct |
2 ms |
2768 KB |
Output is correct |
39 |
Correct |
2 ms |
2640 KB |
Output is correct |
40 |
Correct |
3 ms |
2640 KB |
Output is correct |
41 |
Correct |
2 ms |
2640 KB |
Output is correct |
42 |
Correct |
1 ms |
2640 KB |
Output is correct |
43 |
Correct |
562 ms |
2896 KB |
Output is correct |
44 |
Correct |
735 ms |
2896 KB |
Output is correct |
45 |
Correct |
789 ms |
2896 KB |
Output is correct |
46 |
Correct |
450 ms |
3096 KB |
Output is correct |
47 |
Correct |
748 ms |
3100 KB |
Output is correct |
48 |
Correct |
559 ms |
2988 KB |
Output is correct |
49 |
Correct |
627 ms |
3024 KB |
Output is correct |
50 |
Correct |
697 ms |
3024 KB |
Output is correct |
51 |
Correct |
788 ms |
2896 KB |
Output is correct |
52 |
Correct |
656 ms |
2896 KB |
Output is correct |
53 |
Correct |
562 ms |
3152 KB |
Output is correct |
54 |
Correct |
792 ms |
3024 KB |
Output is correct |
55 |
Correct |
767 ms |
2896 KB |
Output is correct |
56 |
Correct |
678 ms |
2896 KB |
Output is correct |
57 |
Correct |
569 ms |
2896 KB |
Output is correct |
58 |
Correct |
613 ms |
3280 KB |
Output is correct |
59 |
Correct |
734 ms |
3444 KB |
Output is correct |
60 |
Correct |
667 ms |
3448 KB |
Output is correct |
61 |
Correct |
764 ms |
2896 KB |
Output is correct |
62 |
Correct |
634 ms |
2896 KB |
Output is correct |
63 |
Correct |
675 ms |
2896 KB |
Output is correct |
64 |
Correct |
659 ms |
2896 KB |
Output is correct |
65 |
Correct |
297 ms |
2768 KB |
Output is correct |
66 |
Correct |
762 ms |
3028 KB |
Output is correct |
67 |
Correct |
773 ms |
3024 KB |
Output is correct |
68 |
Correct |
658 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 |
2 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 |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2680 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2720 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
2 ms |
2640 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
2 ms |
2768 KB |
Output is correct |
19 |
Correct |
2 ms |
2768 KB |
Output is correct |
20 |
Correct |
2 ms |
2640 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 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 |
2708 KB |
Output is correct |
25 |
Correct |
2 ms |
2640 KB |
Output is correct |
26 |
Correct |
3 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2640 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 |
2640 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
1 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
2768 KB |
Output is correct |
37 |
Correct |
2 ms |
2828 KB |
Output is correct |
38 |
Correct |
2 ms |
2768 KB |
Output is correct |
39 |
Correct |
2 ms |
2640 KB |
Output is correct |
40 |
Correct |
3 ms |
2640 KB |
Output is correct |
41 |
Correct |
2 ms |
2640 KB |
Output is correct |
42 |
Correct |
1 ms |
2640 KB |
Output is correct |
43 |
Correct |
473 ms |
5364 KB |
Output is correct |
44 |
Correct |
735 ms |
8188 KB |
Output is correct |
45 |
Correct |
780 ms |
8288 KB |
Output is correct |
46 |
Correct |
743 ms |
7796 KB |
Output is correct |
47 |
Correct |
683 ms |
5448 KB |
Output is correct |
48 |
Correct |
847 ms |
8308 KB |
Output is correct |
49 |
Correct |
736 ms |
8296 KB |
Output is correct |
50 |
Correct |
765 ms |
8272 KB |
Output is correct |
51 |
Correct |
302 ms |
2768 KB |
Output is correct |
52 |
Correct |
785 ms |
2908 KB |
Output is correct |
53 |
Correct |
794 ms |
2912 KB |
Output is correct |
54 |
Correct |
676 ms |
2912 KB |
Output is correct |
55 |
Correct |
783 ms |
11092 KB |
Output is correct |
56 |
Correct |
950 ms |
11240 KB |
Output is correct |
57 |
Correct |
862 ms |
11324 KB |
Output is correct |
58 |
Correct |
636 ms |
11216 KB |
Output is correct |
59 |
Correct |
899 ms |
17484 KB |
Output is correct |
60 |
Correct |
537 ms |
17484 KB |
Output is correct |
61 |
Correct |
562 ms |
2896 KB |
Output is correct |
62 |
Correct |
735 ms |
2896 KB |
Output is correct |
63 |
Correct |
789 ms |
2896 KB |
Output is correct |
64 |
Correct |
450 ms |
3096 KB |
Output is correct |
65 |
Correct |
748 ms |
3100 KB |
Output is correct |
66 |
Correct |
559 ms |
2988 KB |
Output is correct |
67 |
Correct |
627 ms |
3024 KB |
Output is correct |
68 |
Correct |
697 ms |
3024 KB |
Output is correct |
69 |
Correct |
788 ms |
2896 KB |
Output is correct |
70 |
Correct |
656 ms |
2896 KB |
Output is correct |
71 |
Correct |
562 ms |
3152 KB |
Output is correct |
72 |
Correct |
792 ms |
3024 KB |
Output is correct |
73 |
Correct |
767 ms |
2896 KB |
Output is correct |
74 |
Correct |
678 ms |
2896 KB |
Output is correct |
75 |
Correct |
569 ms |
2896 KB |
Output is correct |
76 |
Correct |
613 ms |
3280 KB |
Output is correct |
77 |
Correct |
734 ms |
3444 KB |
Output is correct |
78 |
Correct |
667 ms |
3448 KB |
Output is correct |
79 |
Correct |
764 ms |
2896 KB |
Output is correct |
80 |
Correct |
634 ms |
2896 KB |
Output is correct |
81 |
Correct |
675 ms |
2896 KB |
Output is correct |
82 |
Correct |
659 ms |
2896 KB |
Output is correct |
83 |
Correct |
297 ms |
2768 KB |
Output is correct |
84 |
Correct |
762 ms |
3028 KB |
Output is correct |
85 |
Correct |
773 ms |
3024 KB |
Output is correct |
86 |
Correct |
658 ms |
3024 KB |
Output is correct |
87 |
Correct |
1 ms |
2640 KB |
Output is correct |
88 |
Correct |
482 ms |
10176 KB |
Output is correct |
89 |
Correct |
716 ms |
8016 KB |
Output is correct |
90 |
Correct |
834 ms |
7892 KB |
Output is correct |
91 |
Correct |
860 ms |
11336 KB |
Output is correct |
92 |
Correct |
972 ms |
11336 KB |
Output is correct |
93 |
Correct |
653 ms |
10512 KB |
Output is correct |
94 |
Correct |
864 ms |
11332 KB |
Output is correct |
95 |
Correct |
870 ms |
11336 KB |
Output is correct |
96 |
Correct |
680 ms |
7744 KB |
Output is correct |
97 |
Correct |
803 ms |
7752 KB |
Output is correct |
98 |
Correct |
706 ms |
12756 KB |
Output is correct |
99 |
Correct |
873 ms |
11220 KB |
Output is correct |
100 |
Correct |
767 ms |
9264 KB |
Output is correct |
101 |
Correct |
767 ms |
8900 KB |
Output is correct |
102 |
Correct |
767 ms |
7760 KB |
Output is correct |
103 |
Correct |
958 ms |
17480 KB |
Output is correct |
104 |
Correct |
774 ms |
17988 KB |
Output is correct |
105 |
Correct |
691 ms |
18000 KB |
Output is correct |
106 |
Correct |
708 ms |
8948 KB |
Output is correct |
107 |
Correct |
726 ms |
7388 KB |
Output is correct |
108 |
Correct |
708 ms |
7480 KB |
Output is correct |
109 |
Correct |
612 ms |
7888 KB |
Output is correct |