#include <bits/stdc++.h>
#define LL long long
#define int LL
#define INF 0x3f3f3f3f
#define PII pair<int,int>
#define kanm7 ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
using namespace std;
const int N = 310;
int dp[N][N][2];
int inv[N * 2], c[N][N];
int n, mod;
void solve() {
cin >> n >> mod;
for(int i = 0; i <= n; i ++) {
for(int j = 0; j <= n; j ++)
{
dp[i][j][0] = dp[i][j][1] = 0;
}
}
memset(inv, 0, (n * 2 + 5) * 4);
memset(c, 0, ((n + 3) * (n + 3)) * 4);
inv[0] = 1;
for(int i = 1; i <= n * 2; i ++) inv[i] = inv[i - 1] * i % mod;
for(int i = 0; i <= n; i ++) {
for(int j = 0; j <= i; j ++) {
if(!j) c[i][j] = 1;
else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
}
int ans = inv[n * 2];
dp[0][0][0] = 1, dp[0][0][1] = 1;
for(int i = 0; i <= n; i ++) {
for(int j = 0; j <= n; j ++) {
for(int k = 1; k <= i; k ++) {
dp[i][j][0] += dp[i - k][j][1] * c[n - (i - k)][k];
dp[i][j][0] %= mod;
}
for(int k = 1; k <= j; k ++) {
dp[i][j][1] += dp[i][j - k][0] * c[n - (j - k)][k];
dp[i][j][1] %= mod;
}
if(i == j && (i == 0 || i == n)) continue;
// 这里dp[i][j][0] + dp[i][j][1]记录的是可以拿第i + j + 1张
牌的所有情况,但是
当i = j = 0的情况,在初始的时候
已经算上了,当 i = j = n的时候,已经拿完所有的牌了。
ans += (dp[i][j][0] + dp[i][j][1]) * inv[n * 2 - i - j];
ans %= mod;
}
}
cout << ans << endl;
}
signed main() {
kanm7;
int T; cin >> T;
while(T --) {
solve();
}
return 0;
}
2023牛客暑期多校训练营3 B Auspiciousness
于 2023-07-26 10:58:41 首次发布