제출 #711269

#제출 시각아이디문제언어결과실행 시간메모리
711269Jarif_Rahman디지털 회로 (IOI22_circuit)C++17
18 / 100
3080 ms8588 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const ll md = 1000002022;

int n, m;
vector<int> P, A;
vector<vector<int>> tree;
vector<ll> dp, total;

void dfs(int nd){
    for(int x: tree[nd]) dfs(x);

    if(nd >= n){
        dp[nd] = A[nd-n];
        return;
    }
    total[nd] = tree[nd].size();
    for(ll x: tree[nd]) total[nd]*=total[x], total[nd]%=md;

    int k = tree[nd].size();
    vector<ll> a, b;
    for(int x: tree[nd]) a.pb(dp[x]), b.pb((total[x]-dp[x])%md);

    vector<vector<ll>> dp2(k, vector<ll>(k+1, 0));
    dp2[0][0] = b[0];
    dp2[0][1] = a[0];
    for(int i = 1; i < k; i++) for(int j = 0; j <= i+1; j++){
        dp2[i][j] = (dp2[i-1][j]*b[i])%md;
        if(j) dp2[i][j]+=(dp2[i-1][j-1]*a[i])%md, dp2[i][j]%=md;
    }

    dp[nd] = 0;
    for(int i = 1; i <= k; i++) dp[nd]+=(dp2[k-1][i]*i)%md, dp[nd]%=md;
}

void init(int _n, int _m, vector<int> _P, vector<int> _A){
    swap(n, _n), swap(m, _m), swap(P, _P), swap(A, _A);
    tree.resize(n+m);
    for(int i = 1; i < n+m; i++) tree[P[i]].pb(i);
}

int count_ways(int L, int R){
    for(int i = L; i <= R; i++) A[i-n] = !A[i-n];
    dp.assign(n+m, 0);
    total.assign(n+m, 1);
    dfs(0);

    if(dp[0] < 0) dp[0]+=md;
    return dp[0];
}
#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...