愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒
  • 板块学术版
  • 楼主Nesraychan
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/2/5 23:24
  • 上次更新2023/10/24 01:34:48
查看原帖
愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒愤怒
254629
Nesraychan楼主2023/2/5 23:24

div1E 为什么 dp of dp 5e5 个点跑不过去!

#include<bits/stdc++.h>
#define IL inline
#define reg register
#define mod 998244353
#define N 220
IL int read()
{
    reg int x=0; reg char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x;
}

IL int Add(reg int x,reg int y){return x+y<mod?x+y:x+y-mod;}
IL int Sub(reg int x,reg int y){return x<y?x-y+mod:x-y;}
IL void Pls(reg int &x,reg int y){x=Add(x,y);}
IL void Dec(reg int &x,reg int y){x=Sub(x,y);}
IL int Mul(reg int x,reg int y){reg long long r=1ll*x*y; return r<mod?r:r%mod;}

int n;
char s[N];
bool ban[4];

#define ull unsigned long long
const ull B1=533,B2=20;

struct Node{int x[4],y[4];}p,q;
std::unordered_map<ull,int>f[2];

IL Node back(reg ull h)
{
    reg Node tp;
    for(reg int i=4;i--;)if(!ban[i])tp.y[i]=h%B1-n,h/=B1;
    for(reg int i=4;i--;)if(!ban[i])tp.x[i]=h%B2,h/=B2;
    return tp;
}

IL ull hash(reg Node &p)
{
    reg ull h=0;
    for(reg int i=0;i<4;++i)if(!ban[i])h=h*B2+p.x[i];
    for(reg int i=0;i<4;++i)if(!ban[i])h=h*B1+n+p.y[i];
    return h;
}

int ch[4][2],de[4][2],ans1,ans2,ans3;
bool vis[4];

IL void clear(reg std::unordered_map<ull,int>&a){reg std::unordered_map<ull,int>b; std::swap(a,b);}

main()
{
    scanf("%s",s+1),n=strlen(s+1);
    ch[0][0]=1,ch[0][1]=2,de[1][0]=1,ch[1][1]=3,ch[2][0]=3,de[2][1]=-1,de[3][0]=1,de[3][1]=-1;
    
    reg int a=0,b=1;
    for(reg int i=4;i--;)p.x[i]=i,p.y[i]=0; ban[1]=1;
    f[a][hash(p)]=1;
    for(reg int i=1,j,w;i<=n;++i,a^=1,b^=1)
    {
        clear(f[b]);
        for(reg auto t:f[a])
        {
            p=back(t.first),w=t.second;
            if(!w)continue;
            if(s[i]!='b')
            {
                for(j=4;j--;)q.x[j]=ch[p.x[j]][0],q.y[j]=p.y[j]+de[p.x[j]][0];
                Pls(f[b][hash(q)],w);
            }
            if(s[i]!='a')
            {
                for(j=4;j--;)q.x[j]=ch[p.x[j]][1],q.y[j]=p.y[j]+de[p.x[j]][1];
                Pls(f[b][hash(q)],w);
            }
        }
    }
    for(reg auto t:f[a])
    {
        p=back(t.first),memset(vis,0,sizeof(vis));
        reg int u=0,s=0,k=0;
        while(!vis[u])vis[u]=1,s+=p.y[u],u=p.x[u]; if(vis[1])continue;
        vis[u]=0,k=p.y[u],u=p.x[u];
        while(vis[u])k+=p.y[u],u=p.x[u];
        if(k>0)Pls(ans1,t.second);
        else if(k<0)Pls(ans3,t.second);
        else Pls(ans2,t.second);
    }
    clear(f[a]),ban[1]=0;

    for(reg int i=4;i--;)p.x[i]=i,p.y[i]=0; ban[2]=1;
    f[a][hash(p)]=1;
    for(reg int i=1,j,w;i<=n;++i,a^=1,b^=1)
    {
        clear(f[b]);
        for(reg auto t:f[a])
        {
            p=back(t.first),w=t.second;
            if(!w)continue;
            if(s[i]!='b')
            {
                for(j=4;j--;)q.x[j]=ch[p.x[j]][0],q.y[j]=p.y[j]+de[p.x[j]][0];
                Pls(f[b][hash(q)],w);
            }
            if(s[i]!='a')
            {
                for(j=4;j--;)q.x[j]=ch[p.x[j]][1],q.y[j]=p.y[j]+de[p.x[j]][1];
                Pls(f[b][hash(q)],w);
            }
        }
    }
    for(reg auto t:f[a])
    {
        p=back(t.first),memset(vis,0,sizeof(vis));
        reg int u=0,s=0,k=0;
        while(!vis[u])vis[u]=1,s+=p.y[u],u=p.x[u]; if(vis[2])continue;
        vis[u]=0,k=p.y[u],u=p.x[u];
        while(vis[u])k+=p.y[u],u=p.x[u];
        if(k>0)Pls(ans1,t.second);
        else if(k<0)Pls(ans3,t.second);
        else Pls(ans2,t.second);
    }
    clear(f[a]),ban[2]=0;

    for(reg int i=4;i--;)p.x[i]=i,p.y[i]=0; ban[1]=ban[2]=1;
    f[a][hash(p)]=1;
    for(reg int i=1,j,w;i<=n;++i,a^=1,b^=1)
    {
        clear(f[b]);
        for(reg auto t:f[a])
        {
            p=back(t.first),w=t.second;
            if(!w)continue;
            if(s[i]!='b')
            {
                for(j=4;j--;)q.x[j]=ch[p.x[j]][0],q.y[j]=p.y[j]+de[p.x[j]][0];
                Pls(f[b][hash(q)],w);
            }
            if(s[i]!='a')
            {
                for(j=4;j--;)q.x[j]=ch[p.x[j]][1],q.y[j]=p.y[j]+de[p.x[j]][1];
                Pls(f[b][hash(q)],w);
            }
        }
    }
    for(reg auto t:f[a])
    {
        p=back(t.first),memset(vis,0,sizeof(vis));
        reg int u=0,s=0,k=0;
        while(!vis[u])vis[u]=1,s+=p.y[u],u=p.x[u]; if(vis[1])continue;
        vis[u]=0,k=p.y[u],u=p.x[u];
        while(vis[u])k+=p.y[u],u=p.x[u];
        if(k>0)Dec(ans1,t.second);
        else if(k<0)Dec(ans3,t.second);
        else Dec(ans2,t.second);
    }

    printf("%d\n%d\n%d\n",ans1,ans2,ans3);
}
2023/2/5 23:24
加载中...