Submission #853102

#TimeUsernameProblemLanguageResultExecution timeMemory
853102willychanFestivals in JOI Kingdom 2 (JOI23_festival2)C++14
87 / 100
9026 ms1368 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...