// AM + DG
#include "circuit.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<vpi> vvpi;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef short int si;
typedef vector<si> vsi;
typedef vector<vsi> vvsi;
#define IOS ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define L(varll, mn, mx) for(ll varll = (mn); varll < (mx); varll++)
#define LR(varll, mx, mn) for(ll varll = (mx); varll > (mn); varll--)
#define LI(vari, mn, mx) for(int vari = (mn); vari < (mx); vari++)
#define LIR(vari, mx, mn) for(int vari = (mx); vari > (mn); vari--)
#define INPV(varvec) for(auto& varveci : (varvec)) cin >> varveci
#define fi first
#define se second
#define pb push_back
#define INF(type) numeric_limits<type>::max()
#define NINF(type) numeric_limits<type>::min()
#define TCASES int t; cin >> t; while(t--)
// ! It's not always the case that you can find a modular inverse...
// ! welp, that's sad, that means I don't get to optimize my dp transition
const ll MOD = 1'000'002'022ll;
vvll adj;
vll num_tot;
vll num_act;
vll a_ll;
ll n, m;
ll count_tot(ll i, const vvll& adj) {
ll& ans = num_tot[i];
if(ans == -1ll) {
ll nc = adj[i].size();
ans = max(nc, 1ll);
if(nc > 0ll) {
for(ll j : adj[i]) {
ans = (ans * count_tot(j, adj)) % MOD;
}
}
}
return ans;
}
ll count_act(ll i, const vvll& adj) {
ll& ans = num_act[i];
if(ans == -1ll) {
vll c;
ans = 0ll;
ll nc = adj[i].size();
for(ll j : adj[i]) {
c.pb(count_act(j, adj));
}
L(ci, 0ll, nc) {
ll prod = 1ll;
L(ind, 0ll, nc) {
if(ind == ci) prod = (prod * c[ind]) % MOD;
else prod = (prod * num_tot[adj[i][ind]]) % MOD;
}
ans = (ans + prod) % MOD;
}
}
return ans;
}
void init(int N, int M, vi P, vi A) {
n = N; m = M;
L(i, 0ll, N + M) {
vll adjr;
adj.pb(adjr);
}
L(i, 1ll, N + M) {
adj[P[i]].pb(i);
}
num_tot.reserve(N + M);
num_act.reserve(N + M);
L(i, 0ll, N + M) {
num_tot.pb(-1ll);
num_act.pb(-1ll);
}
a_ll.reserve(M);
for(int v : A) a_ll.pb((ll)v);
copy(a_ll.begin(), a_ll.end(), next(num_act.begin(), n));
count_tot(0ll, adj);
// cout << "anya likes peanuts, anya hates carrots" << endl;
count_act(0ll, adj);
}
int count_ways(int L, int R) {
fill(num_act.begin(), next(num_act.begin(), n), -1ll); // Make -1 again
L(i, L, R + 1ll) {
num_act[i] = 1ll - num_act[i];
}
// count_act(0ll, adj);
// L(i, 0ll, n + m) {
// cout << num_act[i] << " ";
// }
// cout << "\n";
// L(i, 0ll, n + m) {
// cout << num_tot[i] << " ";
// }
// cout << "\n";
return count_act(0ll, adj);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
25 ms |
344 KB |
Output is correct |
4 |
Correct |
26 ms |
344 KB |
Output is correct |
5 |
Correct |
26 ms |
344 KB |
Output is correct |
6 |
Correct |
26 ms |
344 KB |
Output is correct |
7 |
Correct |
26 ms |
524 KB |
Output is correct |
8 |
Correct |
26 ms |
532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
25 ms |
344 KB |
Output is correct |
4 |
Correct |
26 ms |
344 KB |
Output is correct |
5 |
Correct |
26 ms |
344 KB |
Output is correct |
6 |
Correct |
26 ms |
344 KB |
Output is correct |
7 |
Correct |
26 ms |
524 KB |
Output is correct |
8 |
Correct |
26 ms |
532 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 |
436 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
26 ms |
528 KB |
Output is correct |
30 |
Correct |
26 ms |
528 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
508 KB |
Output is correct |
36 |
Correct |
2 ms |
600 KB |
Output is correct |
37 |
Correct |
26 ms |
716 KB |
Output is correct |
38 |
Correct |
26 ms |
720 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
2 ms |
536 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3050 ms |
5040 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3050 ms |
5040 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
13 |
Execution timed out |
3050 ms |
5040 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
25 ms |
344 KB |
Output is correct |
4 |
Correct |
26 ms |
344 KB |
Output is correct |
5 |
Correct |
26 ms |
344 KB |
Output is correct |
6 |
Correct |
26 ms |
344 KB |
Output is correct |
7 |
Correct |
26 ms |
524 KB |
Output is correct |
8 |
Correct |
26 ms |
532 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 |
436 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
26 ms |
528 KB |
Output is correct |
30 |
Correct |
26 ms |
528 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
508 KB |
Output is correct |
36 |
Correct |
2 ms |
600 KB |
Output is correct |
37 |
Correct |
26 ms |
716 KB |
Output is correct |
38 |
Correct |
26 ms |
720 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
2 ms |
536 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Execution timed out |
3092 ms |
796 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
25 ms |
344 KB |
Output is correct |
4 |
Correct |
26 ms |
344 KB |
Output is correct |
5 |
Correct |
26 ms |
344 KB |
Output is correct |
6 |
Correct |
26 ms |
344 KB |
Output is correct |
7 |
Correct |
26 ms |
524 KB |
Output is correct |
8 |
Correct |
26 ms |
532 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 |
436 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
26 ms |
528 KB |
Output is correct |
30 |
Correct |
26 ms |
528 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
508 KB |
Output is correct |
36 |
Correct |
2 ms |
600 KB |
Output is correct |
37 |
Correct |
26 ms |
716 KB |
Output is correct |
38 |
Correct |
26 ms |
720 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
2 ms |
536 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Execution timed out |
3050 ms |
5040 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |