Submission #1056356

#TimeUsernameProblemLanguageResultExecution timeMemory
1056356pawnedDigital Circuit (IOI22_circuit)C++17
18 / 100
53 ms8732 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...