#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
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
764 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
764 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
764 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
764 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
3 ms |
600 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
764 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
3 ms |
600 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
191 ms |
604 KB |
Output is correct |
25 |
Correct |
168 ms |
860 KB |
Output is correct |
26 |
Correct |
187 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
31 ms |
604 KB |
Output is correct |
29 |
Correct |
26 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
764 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
3 ms |
600 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
191 ms |
604 KB |
Output is correct |
25 |
Correct |
168 ms |
860 KB |
Output is correct |
26 |
Correct |
187 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
31 ms |
604 KB |
Output is correct |
29 |
Correct |
26 ms |
600 KB |
Output is correct |
30 |
Correct |
7846 ms |
900 KB |
Output is correct |
31 |
Correct |
7849 ms |
904 KB |
Output is correct |
32 |
Correct |
7683 ms |
908 KB |
Output is correct |
33 |
Correct |
4046 ms |
1112 KB |
Output is correct |
34 |
Correct |
770 ms |
804 KB |
Output is correct |
35 |
Correct |
619 ms |
792 KB |
Output is correct |