Submission #1029994

# Submission time Handle Problem Language Result Execution time Memory
1029994 2024-07-21T15:35:50 Z 0npata Digital Circuit (IOI22_circuit) C++17
0 / 100
3000 ms 8912 KB
#include "circuit.h"
#include<bits/stdc++.h>

using namespace std;

#define vec vector
#define int long long

const int MOD = 1'000'002'022;

const int MXN = 100'005;

int black[MXN];
int total[MXN];
int white[MXN];
vec<int32_t> par;

vec<int32_t> a;

vec<int> tree[MXN];
int n, m;

void compute(int u) {
	black[u] = 0;
	if(u >= n) {
		total[u] = 1;
		black[u] = a[u-n];
		white[u] = 1 - a[u-n];
		return;
	}
	// cerr << "PROCESSING: " << u << '\n';
	total[u] = tree[u].size();
	for(int v : tree[u]) {
		total[u] *= total[v];
		total[u] %= MOD;
	}
	int k = tree[u].size();
	vec<vec<int>> h(k+1, vec<int>(k+1, 0));
	h[0][0] = 1;
	for(int i = 0; i<k; i++) {
		h[i+1][k] = h[i][k] * white[tree[u][i]];
		h[i+1][k] %= MOD;
		for(int j = 0; j<k; j++) {
			h[i+1][j] += h[i][j] * white[tree[u][i]];
			h[i+1][j] %= MOD;
			h[i+1][j+1] += h[i][j] * black[tree[u][i]];
			h[i+1][j+1] %= MOD;
		}
	}


	for(int i = 0; i<=k; i++) {
		black[u] += h[k][i]*i;
		black[u] %= MOD;
	}
	white[u] = total[u] + MOD - black[u];
	white[u] %= MOD;
}

void dfs1(int u = 0) {
	for(int v : tree[u]) {
		dfs1(v);
	}
	compute(u);
	// cerr << "BLACK: " << black[u] << " WHITE: " << white[u] << '\n';
}

void init(int32_t N, int32_t M, std::vector<int32_t> P, std::vector<int32_t> A) {
	n = N;
	m = M;
	a = A;
	par = P;

	for(int i = 1; i<N+M; i++) {
		tree[P[i]].push_back(i);
	}
}

int32_t count_ways(int32_t L, int32_t R) {

	if(L == R) {
		a[L] = 1-a[L];
		int cur = L;
		while(cur != -1) {
			compute(cur);
			cur = par[L];
		}
	}
	else {
		for(int i = L; i<=R; i++) {
			a[i-n] = 1-a[i-n];
		}
		dfs1();
	}

	return black[0];
}
# Verdict Execution time Memory Grader output
1 Execution timed out 3067 ms 2648 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3021 ms 2648 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3067 ms 2648 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 8912 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 8912 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3021 ms 2648 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3067 ms 2648 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3067 ms 2648 KB Time limit exceeded
2 Halted 0 ms 0 KB -