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<bits/stdc++.h>
#include "circuit.h"
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
const int mod=1000002022;
const int maxn=2e5;
vector<int>adj[maxn];
vector<int>a;
int n,m;
void init(int N,int M,vector<int>p,vector<int>A){
n=N;
m=M;
a=A;
for(int i=1;i<n+m;i++){
adj[p[i]].pb(i);
}
}
int count_ways(int l,int r){
if(n==1){
l-=n;
r-=n;
int cnt=0;
for(int i=0;i<m;i++){
if(l<=i&&i<=r){
a[i]=1-a[i];
}
cnt+=a[i];
}
return cnt;
}
l-=n;
r-=n;
for(int i=l;i<=r;i++){
a[i]=1-a[i];
}
int ans=0;
int cnt[n+m+5][2];//number of ways end with 0/1
for(int j=n;j<n+m;j++){
cnt[j][a[j-n]]=1;
cnt[j][1-a[j-n]]=0;
}
for(int i=n-1;i>=0;i--){
int sz=adj[i].size();
int dp[sz+1];
fill(dp,dp+sz+1,0);
dp[0]=1;
int cur=0;
for(int x:adj[i]){
cur++;
for(int j=cur-1;j>=0;j--){
dp[j+1]+=1ll*dp[j]*cnt[x][1];
dp[j+1]%=mod;
dp[j]+=1ll*dp[j]*cnt[x][0];
dp[j]%=mod;
}
}
int c1=0,c0=0;
for(int j=0;j<=cur;j++){
c1+=dp[j];
c1%=mod;
}
for(int j=cur;j>=1;j--){
cnt[i][1]+=c1;
cnt[i][1]%=mod;
cnt[i][0]+=c0;
cnt[i][0]%=mod;
c0=(c0+dp[j])%mod;
c1=(c1-dp[j]+mod)%mod;
}
}
return cnt[0][1];
}
Compilation message (stderr)
circuit.cpp: In function 'int count_ways(int, int)':
circuit.cpp:47:9: warning: unused variable 'ans' [-Wunused-variable]
47 | int ans=0;
| ^~~
# | 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... |