This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "circuit.h"
#include <bits/stdc++.h>
using ll = long long;
const ll mod = 1e9+2022;
int N, M;
std::vector<int> A;
std::vector<int> adj[200005];
std::pair<int, int> DP[200005];
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
::N = N; ::M = M; ::A = A;
for(int i=1;i<P.size();i++) {
adj[P[i]].push_back(i);
}
}
int count_ways(int L, int R) {
for(int i=L;i<=R;i++) A[i-N] ^= 1;
for(int i=N+M-1;i>=0;i--) {
if(i >= N) {
if(A[i-N]) DP[i] = {0, 1};
else DP[i] = {1, 0};
continue;
}
ll tot = adj[i].size();
DP[i].first = DP[i].second = 0;
ll cmult = 1;
for(auto e:adj[i]) {
tot = tot*(DP[e].first + DP[e].second)%mod;
DP[i].second = (DP[i].second*DP[e].first+(cmult+DP[i].second)*DP[e].second) % mod;
cmult = cmult*(DP[e].first + DP[e].second) % mod;
}
DP[i].first = (tot-DP[i].second);
if(DP[i].first < 0) DP[i].first += mod;
}
//for(int i=N+M-1;i>=0;i--) std::cout << i << ": " << DP[i].first << ' ' << DP[i].second << '\n';
return DP[0].second;
}
/*
int main() {
init(3, 4, {-1, 0, 1, 2, 1, 1, 0}, {1, 0, 1, 0});
std::cout << count_ways(3, 4) << '\n';
std::cout << count_ways(4, 5) << '\n';
std::cout << count_ways(3, 6) << '\n';
}*/
Compilation message (stderr)
circuit.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>)':
circuit.cpp:13:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(int i=1;i<P.size();i++) {
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |