This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
const int N=100'000,mod=1e9+2022;
int add(int x,int y)
{
return (x+y)%mod;
}
void add_self(int& x,int y)
{
x+=y;
if(x>=mod)
{
x-=mod;
}
if(x<0)
{
x+=mod;
}
}
int prod(int x,int y)
{
return (1LL*x*y)%mod;
}
int n,m;
vector<int>g[2*N],a;
int dp[2*N][2];
void init(int nn, int mm, std::vector<int> pp, std::vector<int> init_a)
{
n=nn;
m=mm;
for(int i=1;i<n+m;i++)
{
g[pp[i]].push_back(i);
}
a=init_a;
}
void dfs(int nod)
{
for(auto &c:g[nod])
{
dfs(c);
}
int sz=(int)g[nod].size();
if(g[nod].empty())
{
dp[nod][0]=(a[nod-n]==0);
dp[nod][1]=(a[nod-n]==1);
return;
}
vector<int>choose(sz+1,0);
choose[0]=1;
for(auto &c:g[nod])
{
for(int i=sz;i>=0;i--)
{
choose[i]=prod(choose[i] , dp[c][0]);
if(i>0)
{
add_self(choose[i] , prod(choose[i-1] , dp[c][1]));
}
}
}
dp[nod][0]=dp[nod][1]=0;
for(int i=1;i<=sz;i++)
{
add_self(dp[nod][0] , prod(choose[i],sz-i));
add_self(dp[nod][1] , prod(choose[i],i));
}
add_self(dp[nod][0],prod(choose[0],sz));
}
int count_ways(int l, int r)
{
l-=n;
r-=n;
for(int i=l;i<=r;i++)
{
a[i]^=1;
}
for(auto &c:a)
{
//cout<<c<<' ';
}
dfs(0);
return dp[0][1];
}
Compilation message (stderr)
circuit.cpp: In function 'int count_ways(int, int)':
circuit.cpp:82:15: warning: unused variable 'c' [-Wunused-variable]
82 | for(auto &c:a)
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |