#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 2022;
const int TOT = 497758632;
const int MAXN = 121010;
// https://codeforces.com/blog/entry/18051
bool lazy[MAXN]; int val[MAXN<<1], sum[MAXN<<1];
int h = sizeof(int) * 8 - __builtin_clz(MAXN);
void app(int x) {
val[x] = sum[x] - val[x];
if(val[x]<0) val[x]+=MOD;
if(x<MAXN) lazy[x]^=1;
}
void build(int x) {
while(x>>=1) {
int erm = val[x<<1]+val[x<<1|1];
if(erm>=MOD) erm-=MOD;
val[x]=(lazy[x]?(sum[x]<erm?sum[x]-erm+MOD:sum[x]-erm):erm);
}
}
void push(int x) {
for(int s=h;s;--s) {
int i = x>>s;
if(lazy[i]) app(i<<1),app(i<<1|1);
lazy[i]=0;
}
}
void up(int l, int r) {
l += MAXN, r += MAXN;
int l0 = l, r0 = r;
for(;l<r;l>>=1,r>>=1) {
if(l&1) app(l++);
if(r&1) app(--r);
}
build(l0); build(r0-1);
}
int qry(int l, int r) {
l += MAXN, r+=MAXN;
push(l); push(r-1);
int ans = 0;
for(;l<r;l>>=1,r>>=1) {
if(l&1) {ans+=val[l++]; if(ans>=MOD) ans-=MOD;}
if(r&1) {ans+=val[--r]; if(ans>=MOD) ans-=MOD;}
}
return ans;
}
int exp(int a, int b) {
if(!b) return 1;
return (1LL*exp((1LL*a*a)%MOD,b>>1)*(b&1?a:1))%MOD;
}
int n, m;
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
m = M;
int deg[N+M], deg2[N+M], deg223[N+M], weight[N+M], weight2[N+M], weight223[N+M];
fill(deg,deg+N+M,0);
fill(deg2,deg2+N+M,0);
fill(deg223,deg223+N+M,0);
for(int i=1;i<N+M;i++) deg[P[i]]++;
for(int i=0;i<N;i++) {
while(!(deg[i]%2)) deg[i]/=2, deg2[i]++;
while(!(deg[i]%223)) deg[i]/=223, deg223[i]++;
}
int ways = 1, ways2 = 0, ways223 = 0;
for(int i=0;i<N;i++) ways = (1LL * ways * deg[i]) % MOD, ways2+=deg2[i], ways223+=deg223[i];
weight[0]=1; weight2[0] = weight223[0] = 0;
for(int i=1;i<N+M;i++) weight[i] = (1LL*weight[P[i]] * exp(deg[P[i]], TOT - 1))%MOD, weight2[i] = weight2[P[i]] - deg2[P[i]], weight223[i] = weight223[P[i]] - deg223[P[i]];
for(int i=0;i<M;i++) {
sum[i+MAXN] = (1LL * weight[i+N] * ways) % MOD;
sum[i+MAXN] = (1LL * sum[i+MAXN] * exp(2,ways2 + weight2[i+N])) % MOD;
sum[i+MAXN] = (1LL * sum[i+MAXN] * exp(223,ways223 + weight223[i+N])) % MOD;
val[i+MAXN] = sum[i+MAXN]*A[i];
}
for(int i=MAXN;--i;) {
sum[i]=sum[i<<1]+sum[i<<1|1];
if(sum[i]>=MOD) sum[i]-=MOD;
val[i]=val[i<<1]+val[i<<1|1];
if(val[i]>=MOD) val[i]-=MOD;
}
}
int count_ways(int L, int R) {
up(L-n,R-n+1);
return qry(0,m);
}
/*
main() {
int x = 1e9 + 2022; ll totient = 1e9 + 2022;
for(int i=2;x>1;i++) if(!(x%i)) {
while(!(x%i)) x /= i;
totient *= (i-1); totient /= i;
cout << i << " ";
}
cout << totient;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
2 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1368 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1368 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
1 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1368 KB |
Output is correct |
4 |
Correct |
1 ms |
1368 KB |
Output is correct |
5 |
Correct |
1 ms |
1368 KB |
Output is correct |
6 |
Correct |
2 ms |
1368 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
2 ms |
1368 KB |
Output is correct |
9 |
Correct |
2 ms |
1368 KB |
Output is correct |
10 |
Correct |
2 ms |
1368 KB |
Output is correct |
11 |
Correct |
2 ms |
1368 KB |
Output is correct |
12 |
Correct |
2 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
2 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1368 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1368 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
1 ms |
1368 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1368 KB |
Output is correct |
11 |
Correct |
1 ms |
1368 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
1 ms |
1368 KB |
Output is correct |
14 |
Correct |
2 ms |
1368 KB |
Output is correct |
15 |
Correct |
2 ms |
1368 KB |
Output is correct |
16 |
Correct |
2 ms |
1368 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1368 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1368 KB |
Output is correct |
22 |
Correct |
1 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
1368 KB |
Output is correct |
24 |
Correct |
2 ms |
1368 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1368 KB |
Output is correct |
27 |
Correct |
1 ms |
1368 KB |
Output is correct |
28 |
Correct |
2 ms |
1368 KB |
Output is correct |
29 |
Correct |
1 ms |
1368 KB |
Output is correct |
30 |
Correct |
1 ms |
1368 KB |
Output is correct |
31 |
Correct |
1 ms |
1368 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
2 ms |
1368 KB |
Output is correct |
34 |
Correct |
2 ms |
1368 KB |
Output is correct |
35 |
Correct |
1 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1368 KB |
Output is correct |
37 |
Correct |
2 ms |
1368 KB |
Output is correct |
38 |
Correct |
2 ms |
1368 KB |
Output is correct |
39 |
Correct |
1 ms |
1368 KB |
Output is correct |
40 |
Correct |
2 ms |
1368 KB |
Output is correct |
41 |
Correct |
1 ms |
1368 KB |
Output is correct |
42 |
Correct |
1 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
3788 KB |
Output is correct |
2 |
Correct |
618 ms |
6340 KB |
Output is correct |
3 |
Correct |
611 ms |
6340 KB |
Output is correct |
4 |
Correct |
614 ms |
6328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
3788 KB |
Output is correct |
2 |
Correct |
618 ms |
6340 KB |
Output is correct |
3 |
Correct |
611 ms |
6340 KB |
Output is correct |
4 |
Correct |
614 ms |
6328 KB |
Output is correct |
5 |
Correct |
479 ms |
3904 KB |
Output is correct |
6 |
Correct |
646 ms |
6480 KB |
Output is correct |
7 |
Correct |
624 ms |
6324 KB |
Output is correct |
8 |
Correct |
576 ms |
6332 KB |
Output is correct |
9 |
Correct |
277 ms |
1368 KB |
Output is correct |
10 |
Correct |
612 ms |
1624 KB |
Output is correct |
11 |
Correct |
591 ms |
1624 KB |
Output is correct |
12 |
Correct |
558 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1368 KB |
Output is correct |
4 |
Correct |
1 ms |
1368 KB |
Output is correct |
5 |
Correct |
1 ms |
1368 KB |
Output is correct |
6 |
Correct |
2 ms |
1368 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
2 ms |
1368 KB |
Output is correct |
9 |
Correct |
2 ms |
1368 KB |
Output is correct |
10 |
Correct |
2 ms |
1368 KB |
Output is correct |
11 |
Correct |
2 ms |
1368 KB |
Output is correct |
12 |
Correct |
2 ms |
1368 KB |
Output is correct |
13 |
Correct |
419 ms |
3788 KB |
Output is correct |
14 |
Correct |
618 ms |
6340 KB |
Output is correct |
15 |
Correct |
611 ms |
6340 KB |
Output is correct |
16 |
Correct |
614 ms |
6328 KB |
Output is correct |
17 |
Correct |
479 ms |
3904 KB |
Output is correct |
18 |
Correct |
646 ms |
6480 KB |
Output is correct |
19 |
Correct |
624 ms |
6324 KB |
Output is correct |
20 |
Correct |
576 ms |
6332 KB |
Output is correct |
21 |
Correct |
277 ms |
1368 KB |
Output is correct |
22 |
Correct |
612 ms |
1624 KB |
Output is correct |
23 |
Correct |
591 ms |
1624 KB |
Output is correct |
24 |
Correct |
558 ms |
1624 KB |
Output is correct |
25 |
Correct |
663 ms |
8996 KB |
Output is correct |
26 |
Correct |
676 ms |
9024 KB |
Output is correct |
27 |
Correct |
699 ms |
9012 KB |
Output is correct |
28 |
Correct |
541 ms |
9008 KB |
Output is correct |
29 |
Correct |
671 ms |
9012 KB |
Output is correct |
30 |
Correct |
648 ms |
9024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
2 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1368 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1368 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
1 ms |
1368 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1368 KB |
Output is correct |
11 |
Correct |
1 ms |
1368 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
1 ms |
1368 KB |
Output is correct |
14 |
Correct |
2 ms |
1368 KB |
Output is correct |
15 |
Correct |
2 ms |
1368 KB |
Output is correct |
16 |
Correct |
2 ms |
1368 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1368 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1368 KB |
Output is correct |
22 |
Correct |
1 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
1368 KB |
Output is correct |
24 |
Correct |
2 ms |
1368 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1368 KB |
Output is correct |
27 |
Correct |
1 ms |
1368 KB |
Output is correct |
28 |
Correct |
2 ms |
1368 KB |
Output is correct |
29 |
Correct |
1 ms |
1368 KB |
Output is correct |
30 |
Correct |
1 ms |
1368 KB |
Output is correct |
31 |
Correct |
1 ms |
1368 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
2 ms |
1368 KB |
Output is correct |
34 |
Correct |
2 ms |
1368 KB |
Output is correct |
35 |
Correct |
1 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1368 KB |
Output is correct |
37 |
Correct |
2 ms |
1368 KB |
Output is correct |
38 |
Correct |
2 ms |
1368 KB |
Output is correct |
39 |
Correct |
1 ms |
1368 KB |
Output is correct |
40 |
Correct |
2 ms |
1368 KB |
Output is correct |
41 |
Correct |
1 ms |
1368 KB |
Output is correct |
42 |
Correct |
1 ms |
1368 KB |
Output is correct |
43 |
Correct |
400 ms |
1368 KB |
Output is correct |
44 |
Correct |
587 ms |
1620 KB |
Output is correct |
45 |
Correct |
590 ms |
1624 KB |
Output is correct |
46 |
Correct |
590 ms |
1624 KB |
Output is correct |
47 |
Correct |
565 ms |
1624 KB |
Output is correct |
48 |
Correct |
548 ms |
1624 KB |
Output is correct |
49 |
Correct |
536 ms |
1624 KB |
Output is correct |
50 |
Correct |
551 ms |
1624 KB |
Output is correct |
51 |
Correct |
525 ms |
1624 KB |
Output is correct |
52 |
Correct |
576 ms |
1624 KB |
Output is correct |
53 |
Correct |
513 ms |
1368 KB |
Output is correct |
54 |
Correct |
580 ms |
1624 KB |
Output is correct |
55 |
Correct |
582 ms |
1624 KB |
Output is correct |
56 |
Correct |
586 ms |
1656 KB |
Output is correct |
57 |
Correct |
602 ms |
1624 KB |
Output is correct |
58 |
Correct |
561 ms |
1624 KB |
Output is correct |
59 |
Correct |
604 ms |
1624 KB |
Output is correct |
60 |
Correct |
615 ms |
1624 KB |
Output is correct |
61 |
Correct |
612 ms |
1624 KB |
Output is correct |
62 |
Correct |
599 ms |
1624 KB |
Output is correct |
63 |
Correct |
594 ms |
1624 KB |
Output is correct |
64 |
Correct |
603 ms |
1624 KB |
Output is correct |
65 |
Correct |
272 ms |
1368 KB |
Output is correct |
66 |
Correct |
565 ms |
1624 KB |
Output is correct |
67 |
Correct |
545 ms |
1624 KB |
Output is correct |
68 |
Correct |
533 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
2 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1368 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1368 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
1 ms |
1368 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1368 KB |
Output is correct |
11 |
Correct |
1 ms |
1368 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
1 ms |
1368 KB |
Output is correct |
14 |
Correct |
2 ms |
1368 KB |
Output is correct |
15 |
Correct |
2 ms |
1368 KB |
Output is correct |
16 |
Correct |
2 ms |
1368 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1368 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1368 KB |
Output is correct |
22 |
Correct |
1 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
1368 KB |
Output is correct |
24 |
Correct |
2 ms |
1368 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1368 KB |
Output is correct |
27 |
Correct |
1 ms |
1368 KB |
Output is correct |
28 |
Correct |
2 ms |
1368 KB |
Output is correct |
29 |
Correct |
1 ms |
1368 KB |
Output is correct |
30 |
Correct |
1 ms |
1368 KB |
Output is correct |
31 |
Correct |
1 ms |
1368 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
2 ms |
1368 KB |
Output is correct |
34 |
Correct |
2 ms |
1368 KB |
Output is correct |
35 |
Correct |
1 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1368 KB |
Output is correct |
37 |
Correct |
2 ms |
1368 KB |
Output is correct |
38 |
Correct |
2 ms |
1368 KB |
Output is correct |
39 |
Correct |
1 ms |
1368 KB |
Output is correct |
40 |
Correct |
2 ms |
1368 KB |
Output is correct |
41 |
Correct |
1 ms |
1368 KB |
Output is correct |
42 |
Correct |
1 ms |
1368 KB |
Output is correct |
43 |
Correct |
419 ms |
3788 KB |
Output is correct |
44 |
Correct |
618 ms |
6340 KB |
Output is correct |
45 |
Correct |
611 ms |
6340 KB |
Output is correct |
46 |
Correct |
614 ms |
6328 KB |
Output is correct |
47 |
Correct |
479 ms |
3904 KB |
Output is correct |
48 |
Correct |
646 ms |
6480 KB |
Output is correct |
49 |
Correct |
624 ms |
6324 KB |
Output is correct |
50 |
Correct |
576 ms |
6332 KB |
Output is correct |
51 |
Correct |
277 ms |
1368 KB |
Output is correct |
52 |
Correct |
612 ms |
1624 KB |
Output is correct |
53 |
Correct |
591 ms |
1624 KB |
Output is correct |
54 |
Correct |
558 ms |
1624 KB |
Output is correct |
55 |
Correct |
663 ms |
8996 KB |
Output is correct |
56 |
Correct |
676 ms |
9024 KB |
Output is correct |
57 |
Correct |
699 ms |
9012 KB |
Output is correct |
58 |
Correct |
541 ms |
9008 KB |
Output is correct |
59 |
Correct |
671 ms |
9012 KB |
Output is correct |
60 |
Correct |
648 ms |
9024 KB |
Output is correct |
61 |
Correct |
400 ms |
1368 KB |
Output is correct |
62 |
Correct |
587 ms |
1620 KB |
Output is correct |
63 |
Correct |
590 ms |
1624 KB |
Output is correct |
64 |
Correct |
590 ms |
1624 KB |
Output is correct |
65 |
Correct |
565 ms |
1624 KB |
Output is correct |
66 |
Correct |
548 ms |
1624 KB |
Output is correct |
67 |
Correct |
536 ms |
1624 KB |
Output is correct |
68 |
Correct |
551 ms |
1624 KB |
Output is correct |
69 |
Correct |
525 ms |
1624 KB |
Output is correct |
70 |
Correct |
576 ms |
1624 KB |
Output is correct |
71 |
Correct |
513 ms |
1368 KB |
Output is correct |
72 |
Correct |
580 ms |
1624 KB |
Output is correct |
73 |
Correct |
582 ms |
1624 KB |
Output is correct |
74 |
Correct |
586 ms |
1656 KB |
Output is correct |
75 |
Correct |
602 ms |
1624 KB |
Output is correct |
76 |
Correct |
561 ms |
1624 KB |
Output is correct |
77 |
Correct |
604 ms |
1624 KB |
Output is correct |
78 |
Correct |
615 ms |
1624 KB |
Output is correct |
79 |
Correct |
612 ms |
1624 KB |
Output is correct |
80 |
Correct |
599 ms |
1624 KB |
Output is correct |
81 |
Correct |
594 ms |
1624 KB |
Output is correct |
82 |
Correct |
603 ms |
1624 KB |
Output is correct |
83 |
Correct |
272 ms |
1368 KB |
Output is correct |
84 |
Correct |
565 ms |
1624 KB |
Output is correct |
85 |
Correct |
545 ms |
1624 KB |
Output is correct |
86 |
Correct |
533 ms |
1624 KB |
Output is correct |
87 |
Correct |
1 ms |
1368 KB |
Output is correct |
88 |
Correct |
387 ms |
8092 KB |
Output is correct |
89 |
Correct |
633 ms |
5992 KB |
Output is correct |
90 |
Correct |
606 ms |
6020 KB |
Output is correct |
91 |
Correct |
623 ms |
9040 KB |
Output is correct |
92 |
Correct |
662 ms |
9036 KB |
Output is correct |
93 |
Correct |
647 ms |
9008 KB |
Output is correct |
94 |
Correct |
640 ms |
9036 KB |
Output is correct |
95 |
Correct |
614 ms |
9024 KB |
Output is correct |
96 |
Correct |
610 ms |
5816 KB |
Output is correct |
97 |
Correct |
623 ms |
5968 KB |
Output is correct |
98 |
Correct |
552 ms |
4336 KB |
Output is correct |
99 |
Correct |
726 ms |
9008 KB |
Output is correct |
100 |
Correct |
676 ms |
7456 KB |
Output is correct |
101 |
Correct |
677 ms |
6744 KB |
Output is correct |
102 |
Correct |
644 ms |
5888 KB |
Output is correct |
103 |
Correct |
697 ms |
9024 KB |
Output is correct |
104 |
Correct |
685 ms |
9032 KB |
Output is correct |
105 |
Correct |
572 ms |
9036 KB |
Output is correct |
106 |
Correct |
687 ms |
5680 KB |
Output is correct |
107 |
Correct |
613 ms |
5732 KB |
Output is correct |
108 |
Correct |
630 ms |
5756 KB |
Output is correct |
109 |
Correct |
635 ms |
6088 KB |
Output is correct |