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 <vector>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
const ll MOD = 1e9 + 2022;
void chadd(ll &x, ll y) { x = (x + y) % MOD; }
void chmul(ll &x, ll y) { x = (x * y) % MOD; }
const int mxN = 2e5 + 10;
vector<int> adj[mxN];
int n, m;
bool state[mxN];
void init(int N, int M, vector<int> P, vector<int> A) {
n = N; m = M;
for (int i = 1; i < (int)(P.size()); i++) {
adj[P[i]].pb(i);
}
for (int i = 0; i < (int)(A.size()); i++) {
state[n + i] = A[i];
}
}
int count_ways(int L, int R) {
for (int i = L; i <= R; i++) state[i] ^= 1;
vector<vector<ll>> dp(n + m);
for (int i = 0; i < n + m; i++) dp[i].resize(2);
vector<ll> poly(n + m);
auto dfs = [&](auto&& self, int u) -> void {
if (u >= n) {
dp[u][0] = dp[u][1] = 0;
dp[u][state[u]] = 1;
return;
}
for (int v : adj[u]) self(self, v);
int sz = adj[u].size();
for (int i = 0; i <= sz; i++) poly[i] = (i == 0);
for (int v : adj[u]) {
// multiply by (c0 + c1*x)
for (int i = sz; i >= 1; i--) {
poly[i] = (poly[i] * dp[v][0] + poly[i - 1] * dp[v][1]) % MOD;
}
poly[0] = (poly[0] * dp[v][0]) % MOD;
}
dp[u][0] = dp[u][1] = 0;
for (int i = 0; i <= sz; i++) {
chadd(dp[u][0], (sz - i) * poly[i]);
chadd(dp[u][1], i * poly[i]);
}
};
dfs(dfs, 0);
return (int)(dp[0][1]);
}
# | 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... |