This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "circuit.h"
const ll MOD = 1000002022;
bool test1;
int n;
VVI gp;
VI val;
void init(int N, int M, VI P, VI A) {
test1 = (N == 1);
n = N + M;
gp = VVI(n);
rep(u, n) if (u) gp[P[u]].pb(u);
val = VI(n);
rep(u, M) val[u+N] = A[u];
}
VL dpa; // sev
VL dpb; // spitak
VL subsets(VL& a, VL& b) {
int n = a.size();
VVL dp(n+1, VL(n+1));
dp[0][0] = 1;
replr(j, 1, n) dp[j][0] = dp[j-1][0]*b[j-1]%MOD;
replr(i, 1, n) replr(j, 1, n) {
dp[i][j] = dp[i-1][j-1]*a[i-1]%MOD + dp[i-1][j]*b[i-1]%MOD;
dp[i][j] %= MOD;
}
VL ret(n+1);
replr(i, 0, n) ret[i] = dp[n][i];
return ret;
}
void dfs(int u = 0, int p = -1) {
if (gp[u].size() == 0) {
if (val[u]) dpa[u] = 1;
else dpb[u] = 1;
return;
}
VL a, b;
for (int v : gp[u]) if (v != p) {
dfs(v);
a.pb(dpa[v]);
b.pb(dpb[v]);
}
int c = gp[u].size();
VL s = subsets(a, b);
replr(x, 1, c) {
replr(i, x, c) {
dpa[u] += s[i];
dpa[u] %= MOD;
}
reprl(i, x-1, 0) {
dpb[u] += s[i];
dpb[u] %= MOD;
}
}
/*dpa[u] = (2*a[0]*a[1]%MOD + a[0]*b[1]%MOD + b[0]*a[1]%MOD)%MOD;*/
/*dpb[u] = (2*b[0]*b[1]%MOD + a[0]*b[1]%MOD + b[0]*a[1]%MOD)%MOD;*/
}
int count_ways(int L, int R) {
replr(i, L, R) val[i] = !val[i];
if (test1) {
int sum = 0;
for (int x : val) sum += x;
return sum;
}
dpa = dpb = VL(n);
dfs();
/*rep(u, n) cout << u << ": " << dpa[u] << endl;*/
return dpa[0];
}
# | 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... |