Submission #1056356

# Submission time Handle Problem Language Result Execution time Memory
1056356 2024-08-13T08:59:43 Z pawned Digital Circuit (IOI22_circuit) C++17
18 / 100
53 ms 8732 KB
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

#include "circuit.h"

const ll MOD = 1000002022;

ll nr(ll x) {
	x %= MOD;
	x += MOD;
	x %= MOD;
	return x;
}

const int MAX = 2005;

int N, M;

vi adj[MAX];

vi rt;

void init(int N_g, int M_g, vi P_g, vi A_g) {
	N = N_g; M = M_g;
	for (int i = 1; i < N + M; i++) {
		adj[P_g[i]].pb(i);
	}
/*	cout<<"adj: "<<endl;
	for (int i = 0; i < N + M; i++) {
		cout<<i<<": ";
		for (int x : adj[i])
			cout<<x<<" ";
		cout<<endl;
	}*/
	for (int i = 0; i < N; i++) {
		rt.pb(-1);
	}
	for (int i = 0; i < M; i++) {
		rt.pb(A_g[i]);
	}
}

bool vis[MAX];
vector<vector<ll>> dp(MAX, vector<ll>(2, 0));
// dp[i][0] stores ways to make ith on
// dp[i][1] stores ways to make ith off

void dfs(int n) {
	vis[n] = true;
	vector<ii> allv;	// vector of {ways on, ways off}
	for (int x : adj[n]) {
		if (!vis[x]) {
			dfs(x);
			allv.pb({dp[x][1], dp[x][0]});
		}
	}
	if (adj[n].size() == 0) {
		dp[n][rt[n]] = 1;
		dp[n][1 - rt[n]] = 0;
		return;
	} 
	ll K = adj[n].size();
	vector<vector<ll>> dp2(K + 1, vector<ll>(K + 1, 0));
	// make dp2 of size K + 1 by K + 1
	// dp2[i][j]: ways to get j on out of first i
	dp2[0][0] = 1;
	for (int i = 1; i <= K; i++) {
		for (int j = 0; j <= i; j++) {
			// new is off
			dp2[i][j] = nr(dp2[i - 1][j] * allv[i - 1].se);
			// new is on
			if (j >= 1) {
				dp2[i][j] += nr(dp2[i - 1][j - 1] * allv[i - 1].fi);
				dp2[i][j] = nr(dp2[i][j]);
			}
		}
	}
/*	cout<<"at "<<n<<endl;
	cout<<"K: "<<K<<endl;
	cout<<"allv: "<<endl;
	for (ii p : allv)
		cout<<"("<<p.fi<<", "<<p.se<<"); ";
	cout<<endl;
	cout<<"dp2: "<<endl;
	for (vector<ll> v : dp2) {
		for (ll x : v)
			cout<<x<<" ";
		cout<<endl;
	}*/
	dp[n][0] = 0;
	dp[n][1] = 0;
	for (ll i = 0; i <= K; i++) {
		dp[n][0] += nr((K - i) * dp2[K][i]);
		dp[n][0] = nr(dp[n][0]);
		dp[n][1] += nr(i * dp2[K][i]);
		dp[n][1] = nr(dp[n][1]);
	}
//	cout<<"have "<<dp[n][0]<<", "<<dp[n][1]<<endl;
}

void reset() {
	for (int i = 0; i < N + M; i++) {
		for (int j = 0; j < 2; j++) {
			dp[i][j] = 0;
		}
		vis[i] = false;
	}
}

int count_ways(int L, int R) {
	reset();
	// flip from L to R
	for (int i = L; i <= R; i++) {
		rt[i] = 1 - rt[i];
	}
/*	cout<<"rt: ";
	for (int x : rt)
		cout<<x<<" ";
	cout<<endl;*/
	dfs(0);
/*	cout<<"dp: "<<endl;
	for (int i = 0; i < N + M; i++) {
		cout<<i<<": ";
		for (int j = 0; j < 2; j++) {
			cout<<dp[i][j]<<" ";
		}
		cout<<endl;
	}*/
	return dp[0][1];
}

// subtasks 1, 2, 3: dp (16pt)
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 25 ms 8020 KB Output is correct
4 Correct 27 ms 8500 KB Output is correct
5 Correct 27 ms 8532 KB Output is correct
6 Correct 27 ms 8544 KB Output is correct
7 Correct 28 ms 8728 KB Output is correct
8 Correct 41 ms 8516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 2 ms 600 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 25 ms 8020 KB Output is correct
4 Correct 27 ms 8500 KB Output is correct
5 Correct 27 ms 8532 KB Output is correct
6 Correct 27 ms 8544 KB Output is correct
7 Correct 28 ms 8728 KB Output is correct
8 Correct 41 ms 8516 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 2 ms 600 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 27 ms 8560 KB Output is correct
30 Correct 37 ms 8496 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 3 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 8 ms 1080 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 26 ms 8732 KB Output is correct
38 Correct 53 ms 8732 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 2 ms 600 KB Output is correct
42 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 2532 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 2532 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 2 ms 600 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Runtime error 10 ms 2532 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 25 ms 8020 KB Output is correct
4 Correct 27 ms 8500 KB Output is correct
5 Correct 27 ms 8532 KB Output is correct
6 Correct 27 ms 8544 KB Output is correct
7 Correct 28 ms 8728 KB Output is correct
8 Correct 41 ms 8516 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 2 ms 600 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 27 ms 8560 KB Output is correct
30 Correct 37 ms 8496 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 3 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 8 ms 1080 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 26 ms 8732 KB Output is correct
38 Correct 53 ms 8732 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 2 ms 600 KB Output is correct
42 Correct 2 ms 600 KB Output is correct
43 Runtime error 2 ms 856 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 25 ms 8020 KB Output is correct
4 Correct 27 ms 8500 KB Output is correct
5 Correct 27 ms 8532 KB Output is correct
6 Correct 27 ms 8544 KB Output is correct
7 Correct 28 ms 8728 KB Output is correct
8 Correct 41 ms 8516 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 2 ms 600 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 27 ms 8560 KB Output is correct
30 Correct 37 ms 8496 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 3 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 8 ms 1080 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 26 ms 8732 KB Output is correct
38 Correct 53 ms 8732 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 2 ms 600 KB Output is correct
42 Correct 2 ms 600 KB Output is correct
43 Runtime error 10 ms 2532 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -