萌新求助

P7077 [CSP-S2020] 函数调用

7KByte @ 2020-11-26 16:36:45

85分,WA了三个点,未知原因。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define int long long
#define N 1000005
#define P 998244353
using namespace std;
int n,m,a[N];
struct node{
    int op,p,v,c,mul;
    node(){mul=~0;}
    vector<int>e,f;
}u[N];
void calc(int x){
    if(~u[x].mul)return;
    u[x].mul=1;
    if(u[x].op==2)u[x].mul=u[x].v;
    if(u[x].op!=3)return;
    rep(i,0,u[x].c-1)calc(u[x].e[i]);
    int now=1;u[x].f.resize(u[x].c+1);
    pre(i,u[x].c-1,0)u[x].f[i]=now,now=now*u[u[x].e[i]].mul%P;
    u[x].mul=now;
}
queue<int>q;
int Pow(int x,int y){
    int now=1; 
    for(;y;y>>=1,x=x*x%P)if(y&1)now=now*x%P;
    return now;
}
int in[N],ed[N],cnt[N],mt=1,ad[N];
void topo(){
    ed[m+1]=cnt[m+1]=1;
    rep(i,1,m+1)if(!in[i])q.push(i);
    while(!q.empty()){
        int x=q.front();q.pop();
        if(u[x].op==1)ad[u[x].p]=(ad[u[x].p]+u[x].v*ed[x])%P;
        if(u[x].op==2)mt=mt*Pow(u[x].v,cnt[x])%P;
        rep(i,0,u[x].c-1){
            in[u[x].e[i]]--;
            if(!in[u[x].e[i]])q.push(u[x].e[i]);
            ed[u[x].e[i]]=(ed[u[x].e[i]]+ed[x]*u[x].f[i])%P;
            cnt[u[x].e[i]]=(cnt[u[x].e[i]]+cnt[x])%P;
        }
    }
}
signed main(){
    freopen("P7077_11.in","r",stdin);
    //freopen("aa.out","w",stdout);
    scanf("%lld",&n);
    rep(i,1,n)scanf("%lld",&a[i]);
    scanf("%lld",&m);
    rep(i,1,m){
        scanf("%lld",&u[i].op);
        if(u[i].op==1)scanf("%lld%lld",&u[i].p,&u[i].v);
        else if(u[i].op==2)scanf("%lld",&u[i].v);
        else {
            scanf("%lld",&u[i].c);
            rep(j,1,u[i].c){
                int x;scanf("%lld",&x);in[x]++;
                u[i].e.push_back(x);
            }
        }
    }
    scanf("%lld",&u[m+1].c);u[m+1].op=3;
    rep(i,1,u[m+1].c){
        int x;scanf("%lld",&x);in[x]++;
        u[m+1].e.push_back(x);
    }
    rep(i,1,m+1)calc(i);
    topo();
    rep(i,1,n)printf("%lld ",(ad[i]+mt*a[i])%P);
    //cout<<mt<<endl;
    //rep(i,1,m)printf("%lld ",ad[i]);
    return 0;
}

by AK_Dream @ 2020-11-26 16:38:30

操作2的乘数可能是0,你求0的逆元是求不出来的


by 7KByte @ 2020-11-26 16:39:36

@LK_Luogu 我没有求逆元,不是这个问题/kk


by 小粉兔 @ 2020-11-26 16:45:04

官方数据都能 WA


by 7KByte @ 2020-11-26 16:47:39

@小粉兔 官方数据不能WA?


by 7KByte @ 2020-11-26 16:48:57

好吧\texttt{cnt}应该\bmod \varphi(P)


|