#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
const int MAX_N = 20000;
const int MAX_FACT = 2 * MAX_N;
int mod;
int fact[MAX_FACT + 1], invFact[MAX_FACT + 1], countB[MAX_N + 1], countBU[MAX_N + 1];
int lgPut( int x, int n ) {
if ( n == 0 )
return 1;
int p = lgPut( x, n / 2 );
p = (long long)p * p % mod;
if ( n % 2 == 1 )
p = (long long)p * x % mod;
return p;
}
void initFact( int n ) {
fact[0] = 1;
for ( int i = 1; i <= n; i++ )
fact[i] = (long long)fact[i - 1] * i % mod;
invFact[n] = lgPut( fact[n], mod - 2 );
for ( int i = n - 1; i >= 0; i-- )
invFact[i] = (long long)invFact[i + 1] * (i + 1) % mod;
}
inline int prod( int a, int b ) {
int ret = (long long)a * b - (long long)mod * ((long long)(1.0 / mod * a * b));
return ret + mod * (ret < 0) - mod * (ret >= (long long)mod);
}
inline int prodCons( int x, int k ) {
if ( x < 0 )
return 1;
if ( x == 0 )
return fact[x + k - 1];
return prod( fact[x + k - 1], invFact[x - 1] );
}
int main() {
int n;
cin >> n >> mod;
initFact( MAX_FACT );
countB[1] = 0, countBU[1] = 1;
countB[2] = 1, countBU[2] = 1;
for ( int i = 3; i <= n; i++ ) {
for ( int k = 0; i - k - 1 >= 1; k++ ) {
//B<--B
countB[i] += prod( prod( countB[i - k - 2], (k + 1) * (k + 2) / 2 ), prodCons( 2 * (i - k - 2) - 2, k ) ) * 2 % mod;
countB[i] = (countB[i] >= mod ? countB[i] - mod : countB[i]);
//B<--BU
countB[i] += prod( prod( countBU[i - k - 2], k + 1 ), prodCons( 2 * (i - k - 2) - 1, k ) );
countB[i] = (countB[i] >= mod ? countB[i] - mod : countB[i]);
//BU<--B
countBU[i] += prod( prod( countB[i - k - 1], k + 1 ), prodCons( 2 * (i - k - 1) - 2, k ) );
countBU[i] = (countBU[i] >= mod ? countBU[i] - mod : countBU[i]);
//BU<--BU
countBU[i] += prod( countBU[i - k - 1], prodCons( 2 * (i - k - 1) - 1, k ) );
countBU[i] = (countBU[i] >= mod ? countBU[i] - mod : countBU[i]);
}
}
int tot = 1;
for ( int i = 3; i < 2 * n; i += 2 )
tot = (long long)tot * i % mod;
for ( int i = 1; i <= n; i++ ) {
int k = n - i;
tot = (tot - (long long)countB[i] * (k + 1) % mod * prodCons( 2 * i - 2, k ) % mod + mod) % mod;
tot = (tot - (long long)countBU[i] * prodCons( 2 * i - 1, k ) % mod + mod) % mod;
}
cout << tot;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
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 |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 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 |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 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 |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 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 |
2 ms |
616 KB |
Output is correct |
21 |
Correct |
2 ms |
604 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 |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 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 |
2 ms |
616 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
143 ms |
604 KB |
Output is correct |
25 |
Correct |
143 ms |
744 KB |
Output is correct |
26 |
Correct |
144 ms |
744 KB |
Output is correct |
27 |
Correct |
3 ms |
600 KB |
Output is correct |
28 |
Correct |
25 ms |
760 KB |
Output is correct |
29 |
Correct |
21 ms |
764 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 |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 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 |
2 ms |
616 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
143 ms |
604 KB |
Output is correct |
25 |
Correct |
143 ms |
744 KB |
Output is correct |
26 |
Correct |
144 ms |
744 KB |
Output is correct |
27 |
Correct |
3 ms |
600 KB |
Output is correct |
28 |
Correct |
25 ms |
760 KB |
Output is correct |
29 |
Correct |
21 ms |
764 KB |
Output is correct |
30 |
Correct |
6437 ms |
852 KB |
Output is correct |
31 |
Correct |
6435 ms |
1364 KB |
Output is correct |
32 |
Correct |
6612 ms |
1188 KB |
Output is correct |
33 |
Correct |
3370 ms |
832 KB |
Output is correct |
34 |
Correct |
589 ms |
760 KB |
Output is correct |
35 |
Correct |
497 ms |
1024 KB |
Output is correct |