Submission #853102

# Submission time Handle Problem Language Result Execution time Memory
853102 2023-09-23T12:24:24 Z willychan Festivals in JOI Kingdom 2 (JOI23_festival2) C++14
87 / 100
9000 ms 1368 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 20005;

int n;
int MOD;
ll fac[2*N];
ll invfac[2*N];

int fastpow(int a,ll p){
	int r = 1;	
	while(p){
		if(p&1)	r = (1LL*r*a)%MOD;
		a = (1LL*a*a)%MOD;
		p>>=1;
	}
	return r;
}

ll dp[N+2][2];
int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>MOD;
	fac[0]=1;
	for(int i=1;i<=2*n+2;i++){
		fac[i] = (1LL*fac[i-1]*i)%MOD;
	}
	invfac[2*n+2] = fastpow(fac[2*n+2],MOD-2);
	invfac[0]=1;
	for(int i=2*n+1;i>=1;i--){
		invfac[i] = (1LL*invfac[i+1]*(i+1))%MOD;
	}
	dp[1][1]=1;
	dp[2][0]=1;
	for(int i=1;i<=n;i++){
		for(int f=0;f<2;f++){
		//	cout<<dp[i][f]<<" ";
			if(!dp[i][f]) continue;
			for(int t=0;t<2;t++){
				for(int j=0;i+j+1+!t<=n;j++){
					int k = dp[i][f];
					/*
					if(f==0 && t==0) k = 1LL*(1LL*k*(1LL*(j+1)*(j+2)%MOD)%MOD)*(fac[2*i-3+j]*invfac[2*i-3]%MOD)%MOD;
					if(f==0 && t==1) k = 1LL*(1LL*k*(j+1)%MOD)*(fac[2*i-3+j]*invfac[2*i-3]%MOD)%MOD;
					if(f==1 && t==0) k = 1LL*(1LL*k*(1LL*(j+1)%MOD)%MOD)*(fac[2*i-2+j]*invfac[2*i-2]%MOD)%MOD;
					if(f==1 && t==1) k = 1LL*(1LL*k)*(fac[2*i-2+j]*invfac[2*i-2]%MOD)%MOD;
					*/
					if(!f || !t) k = 1LL*k*(j+1)%MOD;
					if(!f && !t) k = 1LL*k*(j+2)%MOD;
					k = (1LL*k*(fac[2*i-3+j+f]*invfac[2*i-3+f]%MOD))%MOD;
					dp[i+j+1+(!t)][t] +=k;
					dp[i+j+1+(!t)][t] -=(dp[i+j+1+(!t)][t]>=MOD)?(MOD):(0);
				}
			}
		}
		//cout<<"\n";
	}
	
	ll total = (1LL*fac[2*n]*invfac[n])%MOD;
	total = (1LL*total*fastpow(invfac[2],n))%MOD;
	
	//ll total=1;
	//for(int i=1;i<=n;i++) total = 1LL*total*(2*n-2*i+1)%MOD;
	for(int i=1;i<=n;i++){
		
		if(dp[i][0]) total = (total-(1LL*dp[i][0]*(n-i+1)%MOD*fac[i+n-3]%MOD*invfac[2*i-3]%MOD))%MOD;
		if(dp[i][1]) total = (total-(1LL*dp[i][1]*fac[i+n-2]%MOD*invfac[2*i-2]%MOD))%MOD;
		//cout<<total<<"\n";
	}
	if(total<0) total+=MOD;
	cout<<total<<"\n";
	return 0;
}



/*
dp[i][0]*fac[2*i+j-3]*invfac[2*i-3]*(j+2)*(j+1)*invfac[2] =+> dp[i+j+2][0]
dp[i][0]* =+> dp[i+j+1][1]
dp[i][1] =+> dp[i+j+2][0]
dp[i][1] =+>. dp[i+j+1][1]



*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 4 ms 348 KB Output is correct
20 Correct 4 ms 600 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 4 ms 348 KB Output is correct
20 Correct 4 ms 600 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 394 ms 604 KB Output is correct
25 Correct 393 ms 604 KB Output is correct
26 Correct 395 ms 604 KB Output is correct
27 Correct 5 ms 348 KB Output is correct
28 Correct 67 ms 508 KB Output is correct
29 Correct 53 ms 496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 4 ms 348 KB Output is correct
20 Correct 4 ms 600 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 394 ms 604 KB Output is correct
25 Correct 393 ms 604 KB Output is correct
26 Correct 395 ms 604 KB Output is correct
27 Correct 5 ms 348 KB Output is correct
28 Correct 67 ms 508 KB Output is correct
29 Correct 53 ms 496 KB Output is correct
30 Execution timed out 9026 ms 1368 KB Time limit exceeded
31 Halted 0 ms 0 KB -