Submission #1012747

#TimeUsernameProblemLanguageResultExecution timeMemory
1012747snpmrnhlolFestivals in JOI Kingdom 2 (JOI23_festival2)C++17
100 / 100
7849 ms1112 KiB
#include<bits/stdc++.h> using namespace std; const int N = 2e4; const int NR = 2*N; int n,mod; int dp[N + 1][2]; int fact[NR + 1], inv[NR + 1]; int fastexp(int a,int b){ int r = 1; while(b){ if(b&1)r = 1ll*r*a%mod; a = 1ll*a*a%mod; b/=2; } return r; } void precalc(){ fact[0] = 1; for(int i = 1;i <= NR;i++){ fact[i] = 1ll*fact[i - 1]*i%mod; } inv[NR] = fastexp(fact[NR],mod - 2); for(int i = NR - 1;i >= 0;i--){ inv[i] = 1ll*inv[i + 1]*(i + 1)%mod; } } int comb(int n, int k){ if(n < 0 || k < 0 || k > n)return 0; return 1ll*fact[n]*inv[k]%mod*inv[n - k]%mod; } ///assume all are seperate int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>mod; precalc(); dp[2][1] = 1; dp[1][0] = 1; for(int i = 0;i <= n;i++){ for(int j = 0;i + j <= n;j++){ if(i >= 2 && i + j + 2 <= n){ //cout<<i<<' '<<j<<' '<<2<<' '<<1ll*dp[i][1]*comb(j + 2,2)%mod<<' '<<1ll*inv[2*i - 3]%mod*fact[2*i - 3 + j]%mod*2%mod<<'\n'; dp[i + j + 2][1] = (dp[i + j + 2][1] + 1ll*dp[i][1]*comb(j + 2,2)%mod*inv[2*i - 3]%mod*fact[2*i - 3 + j]%mod*2%mod)%mod; } if(i >= 1 && i + j + 2 <= n){ dp[i + j + 2][1] = (dp[i + j + 2][1] + 1ll*dp[i][0]*(j + 1)%mod*inv[2*i - 2]%mod*fact[2*i - 2 + j]%mod)%mod; } if(i >= 1 && i + j + 1 <= n){ dp[i + j + 1][0] = (dp[i + j + 1][0] + 1ll*dp[i][1]*(j + 1)%mod*inv[2*i - 3]%mod*fact[2*i - 3 + j]%mod)%mod; } if(i >= 1 && i + j + 1 <= n){ dp[i + j + 1][0] = (dp[i + j + 1][0] + 1ll*dp[i][0]*inv[2*i - 2]%mod*fact[2*i - 2 + j]%mod)%mod; } } } int tot = 0; int all = 1; for(int i = 0;i <= n;i++){ //cout<<dp[i][0]<<' '<<dp[i][1]<<' '<<1ll*dp[i][1]*(n - i + 1)%mod*fact[2*i - 3 + (n - i)]*inv[2*i - 3]%mod<<'\n'; tot = (tot + 1ll*dp[i][1]*(n - i + 1)%mod*fact[2*i - 3 + (n - i)]%mod*inv[2*i - 3]%mod)%mod; tot = (tot + 1ll*dp[i][0]*fact[2*i - 2 + (n - i)]%mod*inv[2*i - 2]%mod)%mod; if(i)all = 1ll*all*(2*i - 1)%mod; } cout<<(all - tot + mod)%mod<<'\n'; return 0; } /** 4 100000007 **/
#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...