#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define fi first
#define se second
#define endl '\n'
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,MOD;
int dp[2][20005]; //m-1
int dp2[2][20005]; //m
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n>>MOD;
int a=0,b=1;
dp[a][2]=1;
rep(x,2,n+1){
memset(dp[b],0,sizeof(dp[b]));
rep(y,0,20005) if (dp[a][y]){
dp[b][2]=(dp[b][2]+dp[a][y])%MOD;
dp[b][y+1]=(dp[b][y+1]+dp[a][y]*(2*(x-1)-y))%MOD;
dp2[b][1]=(dp2[b][1]+dp[a][y]*y)%MOD;
}
rep(y,0,20005) if (dp2[a][y]){
dp[b][2+y]=(dp[b][2+y]+dp2[a][y])%MOD;
dp2[b][y+1]=(dp2[b][y+1]+dp2[a][y]*(2*(x-1)-y))%MOD;
}
swap(a,b);
}
int ans=0;
rep(y,0,20005) ans=(ans+dp[a][y]+dp2[a][y])%MOD;
// cout<<ans<<endl;
int tot=1;
rep(x,1,n+1) tot=tot*(2*x-1)%MOD;
cout<<(tot-ans+MOD)%MOD<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |