QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#155697#6410. Classical DP ProblemForever_Young#WA 20ms105480kbC++143.9kb2023-09-02 01:08:162023-09-02 01:08:17

Judging History

你现在查看的是最新测评结果

  • [2023-09-02 01:08:17]
  • 评测
  • 测评结果:WA
  • 用时:20ms
  • 内存:105480kb
  • [2023-09-02 01:08:16]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,s,t) for(int i = s;i <= t;i ++)
using namespace std;
int n,a[5050],r,dp[5050][5050],res,fac[5050],ifac[5050],g[5050][5050],nt[10100],ant[10100];
const int mod=998244353;
const int NN=5050;
typedef long long ll;

int qpow(int x,int y){
	int res=1;
	while (y){
		if (y&1) res=1ll*res*x%mod;
		x=1ll*x*x%mod;
		y/=2;
	}
	return res;
}

//NTT
namespace NTT{
    const int g=3;
 
 
    int x[NN<<2],y[NN<<2],wn[NN<<2];
    void init()
    {
        rep(i,0,20)wn[i]=qpow(g,(mod-1)/(1<<i));
    }
 
    void brc(int *F,int len)
    {
        int j=len/2;
        rep(i,1,len-2){
            if(i<j)swap(F[i],F[j]);
            int k=len/2;
            while(j>=k) j-=k,k>>=1;
            if(j<k)j+=k;
        }
    }
 
    void NTT(int *F,int len,int t)
    {
        int id=0; brc(F,len);
        for(int h=2;h<=len;h<<=1)
        {
            id++;
            for(int j=0;j<len;j+=h)
            {
                int E=1;
                for(int k=j;k<j+h/2;k++)
                {
                    int u=F[k],v=(ll)E*F[k+h/2]%mod;
                    F[k]=(u+v)%mod,F[k+h/2]=((u-v)%mod+mod)%mod;
                    E=(ll)E*wn[id]%mod;
                }
            }
        }
        if(t==-1)
        {
            rep(i,1,len/2-1)swap(F[i],F[len-i]);
            ll inv=qpow(len,mod-2);
            rep(i,0,len-1)F[i]=(ll)F[i]%mod*inv%mod;
        }
    }
    void multiply(int *a,int len1,int *b,int len2)
    {
        int len=1;
        while(len<len1+len2)len<<=1;
        rep(i,len1,len-1)a[i]=0;
        rep(i,len2,len-1)b[i]=0;
        NTT(a,len,1); NTT(b,len,1);
        rep(i,0,len-1)a[i]=(ll)a[i]*b[i]%mod;
        NTT(a,len,-1);
    }
    void mpow(int *a,int len1,int *b,int len2,int y)
    {
        int len=1;
        while(len<len1+len2)len<<=1;
        rep(i,len1,len-1)a[i]=0;
        rep(i,len2,len-1)b[i]=0;
        NTT(a,len,1); NTT(b,len,1);
        rep(i,0,len-1)a[i]=(ll)a[i]*qpow(b[i],y)%mod;
        NTT(a,len,-1);
    }
}

int c(int x,int y){//x>=y
	if (x<y) return 0;
	return 1ll*fac[x]*ifac[y]%mod*ifac[x-y]%mod;
}

int main(){
	//freopen("D.in","r",stdin);
	NTT::init();
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (int i=n;i;i--){
		if (r>=a[i]) break;
		r++;
	}
	
	dp[n][a[n-r]]=1;
	for (int i=n;i>n-r;i--)
		for (int j=0;j<=a[n-r];j++){
			//printf("%d %d %d\n",i,j,dp[0][i][j]);
			dp[i-1][j]=(dp[i-1][j]+1ll*dp[i][j]*(a[i]-j))%mod;
			if (j)
				dp[i-1][j-1]=(dp[i-1][j-1]+1ll*dp[i][j]*j)%mod;
	}
	res=dp[n-r][0];
	memset(dp,0,sizeof(dp));
	//printf("%d\n",res);
	
	dp[n][a[n-r+1]]=1;
	for (int i=n;i>n-r;i--)
		for (int j=0;j<=a[n-r+1];j++){
			//printf("%d %d %d\n",i,j,dp[0][i][j]);
			dp[i-1][j]=(dp[i-1][j]+1ll*dp[i][j]*(a[i]-j))%mod;
			if (j)
				dp[i-1][j-1]=(dp[i-1][j-1]+1ll*dp[i][j]*j)%mod;
	}
	res=(res-dp[n-r][0]+mod)%mod;
	memset(dp,0,sizeof(dp));
	//printf("%d\n",res);
	
	if (r==a[n-r+1]){
		fac[0]=1;
		for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
		ifac[n]=qpow(fac[n],mod-2);
		for (int i=n;i;i--) ifac[i-1]=1ll*ifac[i]*i%mod;
		g[1][0]=1;
		int i=1;
		while (i<=n){
			int z=min(a[i],r);
			for (int j=0;j<=z;j++){
				nt[j]=1ll*g[i][j]*fac[z-j]%mod;
				ant[j]=ifac[j];
			}
			int tmp=i;
			while (a[tmp]==a[i]) tmp++;
			if (a[i]>r){
				ant[0]=0;
				tmp=n+1;
			}
			NTT::mpow(nt,z+1,ant,z+1,tmp-i);
			for (int j=0;j<=z;j++) g[tmp][j]=1ll*nt[j]*ifac[z-j]%mod;
			i=tmp;
			//for (int j=0;j<=z;j++) printf("%d %d %d\n",i,j,g[i][j]);
			
			/*
			if (a[i]<=r){
				for (int j=0;j<=r;j++) g[i+1][j]=(g[i+1][j]+g[i][j])%mod;
			}
			for (int j=0;j<r;j++){
				if (g[i][j]==0) continue;
				for (int k=j+1;k<=r&&k<=a[i];k++){
					g[i+1][k]=(g[i+1][k]+1ll*g[i][j]*c(min(a[i],r)-j,k-j))%mod;
				}
			}
			*/
		}
		//printf("%d\n",g[n+1][r]);
		res=(res+g[n+1][r])%mod;
	}
	printf("%d %d\n",r,res);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 105356kb

input:

3
1 2 3

output:

2 6

result:

ok 2 number(s): "2 6"

Test #2:

score: 0
Accepted
time: 16ms
memory: 104368kb

input:

1
1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #3:

score: 0
Accepted
time: 8ms
memory: 105308kb

input:

2
1 1

output:

1 2

result:

ok 2 number(s): "1 2"

Test #4:

score: 0
Accepted
time: 20ms
memory: 104772kb

input:

2
2 2

output:

2 6

result:

ok 2 number(s): "2 6"

Test #5:

score: 0
Accepted
time: 18ms
memory: 104820kb

input:

3
1 1 1

output:

1 3

result:

ok 2 number(s): "1 3"

Test #6:

score: 0
Accepted
time: 12ms
memory: 105476kb

input:

3
2 2 2

output:

2 9

result:

ok 2 number(s): "2 9"

Test #7:

score: 0
Accepted
time: 12ms
memory: 104528kb

input:

3
3 3 3

output:

3 48

result:

ok 2 number(s): "3 48"

Test #8:

score: 0
Accepted
time: 9ms
memory: 105480kb

input:

5
1 1 3 3 4

output:

3 47

result:

ok 2 number(s): "3 47"

Test #9:

score: -100
Wrong Answer
time: 12ms
memory: 105368kb

input:

10
2 4 5 5 5 5 6 8 8 10

output:

5 267546521

result:

wrong answer 2nd numbers differ - expected: '864', found: '267546521'