QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#462115 | #7408. Determination | grass8cow | WA | 1ms | 3728kb | C++17 | 2.4kb | 2024-07-03 14:26:35 | 2024-07-03 14:26:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int qpow(int a,int b){
int c=1;for(;b;b>>=1){
if(b&1)c=1ll*a*c%mod;
a=1ll*a*a%mod;
}
return c;
}
void read(int &e){
e=0;char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')e=e*10+c-'0',c=getchar();
}
int n,X,fa[1010000],a[1010000],b[1001000],c[1010000];
namespace sol1{
int dp[1010000][2];
void sol(){
for(int i=1;i<=n;i++)dp[i][0]=1,dp[i][1]=-c[i];
for(int i=n;i>=2;i--){
int fz[2]={0,0};
for(int x=0;x<2;x++)for(int y=0;y<2;y++){
if(!x&&!y)(fz[1]-=1ll*dp[fa[i]][x]*dp[i][y]%mod*a[i]%mod*b[i]%mod)%=mod;
else if(y)(fz[x]+=1ll*dp[fa[i]][x]*dp[i][y]%mod)%=mod;
}
dp[fa[i]][0]=fz[0],dp[fa[i]][1]=fz[1];
}
printf("%d\n",((((n&1)?(-dp[1][1]):dp[1][1])+mod)%mod+mod)%mod);
}
}
void ad(int &x,int y){
(x+=y)%=mod;
}
namespace sol2{
int dp[1010000][6][2],fz[6][2];
//{是已选点吗,是特殊点吗,入了吗,出了吗},树内已存了吗
void sol(){
int I=qpow(X,mod-2);
for(int i=1;i<=n;i++)c[i]=(1ll*c[i]*I%mod+mod-1)%mod;
for(int i=2;i<=n;i++)a[i]=(1ll*a[i]*I%mod+mod-1)%mod,b[i]=(1ll*b[i]*I%mod+mod-1)%mod;
for(int i=1;i<=n;i++){
for(int x=0;x<6;x++)for(int z=0;z<2;z++)dp[i][x][z]=0;
dp[i][5][0]=mod-c[i],dp[i][4][0]=1,dp[i][0][0]=1;
}
for(int i=n;i>=2;i--){
memset(fz,0,sizeof(fz));
for(int p1=0;p1<2;p1++)for(int x=0;x<6;x++)if(dp[fa[i]][x][p1])
for(int p2=0;p1+p2<2;p2++)if(!((x<4)&&p2))for(int y=0;y<6;y++)if(dp[i][y][p2]){
int zg=1ll*dp[fa[i]][x][p1]*dp[i][y][p2]%mod;
if(x>=4&&y<4){if(!p1&&!p2)ad(fz[x][1],zg);}
if(x>=4&&y>=4){
if(y==5)ad(fz[x][p1|p2],zg);
else if(x==4&&y==4)ad(fz[5][p1|p2],mod-1ll*zg*a[i]%mod*b[i]%mod);
}
if(x<4&&y>=4){if(y==5)ad(fz[x][0],zg);}
if(x<4&&y<4){
if(!(y&2)&&!(x&1))ad(fz[x|1][0],1ll*zg*a[i]%mod);
if(!(y&1)&&!(x&2))ad(fz[x|2][0],1ll*zg*b[i]%mod);
}
}
for(int x=0;x<6;x++)for(int z=0;z<2;z++)
dp[fa[i]][x][z]=fz[x][z];
}
int ans=0;
for(int x=0;x<6;x++)if(x!=4)for(int z=0;z<2;z++)
ad(ans,(z||x<4)?-dp[1][x][z]:dp[1][x][z]);
printf("%lld\n",(1ll*ans*qpow(-X,n)%mod+mod)%mod);
}
}
void sol(){
read(n),read(X);
for(int i=1;i<=n;i++)read(c[i]);
for(int i=2;i<=n;i++)read(fa[i]),read(a[i]),read(b[i]);
if(!X){sol1::sol();return;}
sol2::sol();
}
int main(){
int T;read(T);while(T--)sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3728kb
input:
3 1 23333 233 3 1 1 1 1 1 2 3 1 4 5 3 1 2 3 4 1 4 5 2 6 7
output:
233 998244349 998244269
result:
wrong answer 2nd numbers differ - expected: '1000000003', found: '998244349'