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 <cstdio>
using namespace std;
using ll = long long;
// N ... num threshold gates
// M ... num source gates
vector<int> A;
int N, M;
const int MOD = 1000002022;
vector<vector<int>> tree;
void init(int N, int M, vector<int> P, vector<int> A) {
::A = A;
::N = N;
::M = M;
tree.resize(N);
for (int i = 1; i < P.size(); i++)
tree[P[i]].push_back(i);
}
ll mmod(ll v) {
return ((v % MOD) + MOD) % MOD;
}
pair<ll,ll> countrec(int i) {
if (i >= N) {
if (A[i-N]) return {0, 1};
else return {1, 0};
}
int k = tree[i].size();
vector<pair<ll,ll>> v(k);
for (int j = 0; j < k; j++)
v[j] = countrec(tree[i][j]);
vector<vector<ll>> mul(k+1, vector<ll>(k+1));
mul[0][0] = 1;
for (int j = 1; j <= k; j++)
mul[0][j] = (mul[0][j-1] * v[j-1].first) % MOD;
for (int l = 1; l <= k; l++) {
for (int j = 1; j <= k; j++) {
mul[l][j] = (
mul[l-1][j-1] * v[j-1].second
+ mul[l][j-1] * v[j-1].first
) % MOD;
}
}
ll ans = 0;
for (int l = 1; l <= k; l++) {
ans = (ans + mul[l][k]*l) % MOD;
}
ll tot = k;
for (int j = 0; j < k; j++)
tot = (tot * (v[j].first + v[j].second)) % MOD;
/*printf("%d: tot=%lld ans=%lld\n", i, tot, ans);
printf("V %d: ", i);
for (auto it : v)
printf("{%lld,%lld} ", it.first, it.second);
printf("\n");*/
return {mmod(tot-ans), mmod(ans)};
}
// toggle state of source gates between L and R (inclusive)
// return num distinctive param assignments with gate#0 = 1
int count_ways(int L, int R) {
for (int i = L; i <= R; i++)
A[i-N] = 1-A[i-N];
return countrec(0).second;
}
Compilation message (stderr)
circuit.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>)':
circuit.cpp:21:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | 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... |