Submission #728658

#TimeUsernameProblemLanguageResultExecution timeMemory
728658groguDigital Circuit (IOI22_circuit)C++17
2 / 100
9 ms1480 KiB
#include "circuit.h" #include <bits/stdc++.h> #define here cerr<<"==============================\n" #define dbg(x) cerr<<#x<<": "<<x<<endl #define ll long long #define pll pair<ll,ll> #define pb push_back #define popb pop_back #define all(x_) x_.begin(), x_.end() using namespace std; #define mod 1000002022 ll add(ll x,ll y){ x+=y; x%=mod; if(x<0) x+=mod; return x; } ll mul(ll x,ll y){ x*=y; x%=mod; if(x<0) x+=mod; return x; } ll po(ll x,ll y){ if(y==0) return 1; ll ans = po(x,y/2); ans = mul(ans,ans); if(y&1) ans = mul(ans,x); return ans; } ll inv(ll x){return po(x,mod-2);} #define maxn 1005 int n,m; int par[maxn]; ll a[maxn]; int deg[maxn]; ll sub[maxn]; vector<int> g[maxn]; vector<int> v[maxn]; vector<int> topo; bool cmp(int x,int y){ if(x<n) return 1; if(y<n) return 0; return 0; } void init(int N, int M, vector<int> P, vector<int> A) { n = N; m = M; for(int i = 1;i<n+m;i++){ par[i+1] = P[i]+1; g[i+1].pb(P[i]+1); v[P[i]+1].pb(i+1); deg[P[i]+1]++; } for(int i = n;i<n+m;i++) a[i+1] = A[i-n]; queue<int> q; for(int i = 1;i<=n+m;i++) if(!deg[i]){ q.push(i); } for(ll i = 1;i<=n+m;i++) sub[i] = 1; for(ll i = 1;i<=n;i++) sub[i] = deg[i]; while(q.size()){ int u = q.front(); q.pop(); if(u<=n) topo.pb(u); for(int s : g[u]){ deg[s]--; sub[s]=mul(sub[u],sub[u]); if(!deg[s]) q.push(s); } } for(ll i = 1;i<=n+m;i++) sort(all(v[i]),cmp); /** for(ll i = 1;i<=n+m;i++){ dbg(i); for(ll x : v[i]) cerr<<x<< " "; cerr<<endl; } **/ } ll dp[maxn]; ll sum[maxn][maxn]; int count_ways(int L, int R){ L++; R++; for(int i = L;i<=R;i++) a[i]^=1; for(int i = n+1;i<=n+m;i++) dp[i] = 1; for(int u : topo){ int sz = v[u].size(); ll cnt = 0; while(sz>=1&&v[u][sz-1]>n){ cnt+=a[v[u][sz-1]]; sz--; } //dbg(u); //dbg(cnt); ll f = 1; for(int j = 1;j<=sz;j++) f = mul(f,sub[v[u][j-1]]); //dbg(cnt); for(int i = 0;i<=sz;i++) for(int j = 1;j<=sz;j++) sum[i][j] = 0; for(int i = 0;i<=sz;i++) sum[i][0] = f; dp[u] = mul(cnt,f); for(int k = 1;k<=sz;k++){ for(int j = 1;j<=sz;j++){ ll val = dp[v[u][j-1]]; sum[j][k] = add(sum[j-1][k],mul(sum[j-1][k-1],mul(val,inv(sub[v[u][j-1]])))); } dp[u] = add(dp[u],sum[sz][k]); } } //cerr<<"dp: "<<endl; //for(int i = 1;i<=n;i++) cerr<<dp[i]<< " "; //cerr<<endl; return dp[1]; } /** 3 4 3 -1 0 1 2 1 1 0 1 0 1 0 3 4 4 5 3 6 **/
#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...