#include <bits/stdc++.h> using namespace std; const int N = 1005; const int K = 13; int dp[N][K][4][2]; int bin[N+5][N+5]; int P = 101; int add(int a, int b){ return (a+b)%P; } void addr(int &a, int b){ a += b; a %= P; } int mul(int a, int b){ return (int)(((long long)a * b)%P); } int main(){ ios_base::sync_with_stdio(0); for(int i = 0; i < N; ++i){ bin[i][0] = 1; } int n,ki,p; cin >> n >> ki >> p; P = p; for(int i = 1; i < N; ++i){ for(int j = 1; j <= i; ++j){ bin[i][j] = add(bin[i-1][j], bin[i-1][j-1]); } } for(int a = 0; a < 4; ++a){ dp[0][0][a][0] = 1; dp[0][0][a][1] = 1; } dp[1][0][0][0] = 1; for(int i = 0; i < N; ++i){ for(int j = 0; j < i; ++j){ for(int k = 1; k < K; ++k){ addr(dp[i][k][0][0], mul(mul(dp[j][k][1][1], dp[i-j-1][k][2][0]),bin[i-1][j])); addr(dp[i][k][0][0], mul(mul(dp[j][k][1][0], dp[i-j-1][k-1][2][1]),bin[i-1][j])); addr(dp[i][k][1][0], mul(mul(dp[j][k-1][1][1], dp[i-j-1][k-1][3][0]),bin[i-1][j])); addr(dp[i][k][1][0], mul(mul(dp[j][k][1][0], dp[i-j-1][k-1][3][1]),bin[i-1][j])); addr(dp[i][k][2][0], mul(mul(dp[j][k-1][3][1], dp[i-j-1][k][2][0]),bin[i-1][j])); addr(dp[i][k][2][0], mul(mul(dp[j][k-1][3][0], dp[i-j-1][k-1][2][1]),bin[i-1][j])); addr(dp[i][k][3][0], mul(mul(dp[j][k-1][3][1], dp[i-j-1][k][3][0]),bin[i-1][j])); addr(dp[i][k][3][0], mul(mul(dp[j][k][3][0], dp[i-j-1][k-1][3][1]),bin[i-1][j])); addr(dp[i][k][3][0], mul(mul(dp[j][k-1][3][0], dp[i-j-1][k-1][3][0]),bin[i-1][j])); } } for(int a = 0; a < 4; ++a){ dp[i][0][a][1] = dp[i][0][a][0]; } for(int k = 1; k < K; ++k) for(int a = 0; a < 4; ++a){ dp[i][k][a][1] = add(dp[i][k-1][a][1], dp[i][k][a][0]); } } if(ki >= K)cout << 0 << '\n'; else cout << dp[n][ki][0][0] << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <bits/stdc++.h> using namespace std; const int N = 1005; const int K = 13; int dp[N][K][4][2]; int bin[N+5][N+5]; int P = 101; int add(int a, int b){ return (a+b)%P; } void addr(int &a, int b){ a += b; a %= P; } int mul(int a, int b){ return (int)(((long long)a * b)%P); } int main(){ ios_base::sync_with_stdio(0); for(int i = 0; i < N; ++i){ bin[i][0] = 1; } int n,ki,p; cin >> n >> ki >> p; P = p; for(int i = 1; i < N; ++i){ for(int j = 1; j <= i; ++j){ bin[i][j] = add(bin[i-1][j], bin[i-1][j-1]); } } for(int a = 0; a < 4; ++a){ dp[0][0][a][0] = 1; dp[0][0][a][1] = 1; } dp[1][0][0][0] = 1; for(int i = 0; i < N; ++i){ for(int j = 0; j < i; ++j){ for(int k = 1; k < K; ++k){ addr(dp[i][k][0][0], mul(mul(dp[j][k][1][1], dp[i-j-1][k][2][0]),bin[i-1][j])); addr(dp[i][k][0][0], mul(mul(dp[j][k][1][0], dp[i-j-1][k-1][2][1]),bin[i-1][j])); addr(dp[i][k][1][0], mul(mul(dp[j][k-1][1][1], dp[i-j-1][k-1][3][0]),bin[i-1][j])); addr(dp[i][k][1][0], mul(mul(dp[j][k][1][0], dp[i-j-1][k-1][3][1]),bin[i-1][j])); addr(dp[i][k][2][0], mul(mul(dp[j][k-1][3][1], dp[i-j-1][k][2][0]),bin[i-1][j])); addr(dp[i][k][2][0], mul(mul(dp[j][k-1][3][0], dp[i-j-1][k-1][2][1]),bin[i-1][j])); addr(dp[i][k][3][0], mul(mul(dp[j][k-1][3][1], dp[i-j-1][k][3][0]),bin[i-1][j])); addr(dp[i][k][3][0], mul(mul(dp[j][k][3][0], dp[i-j-1][k-1][3][1]),bin[i-1][j])); addr(dp[i][k][3][0], mul(mul(dp[j][k-1][3][0], dp[i-j-1][k-1][3][0]),bin[i-1][j])); } } for(int a = 0; a < 4; ++a){ dp[i][0][a][1] = dp[i][0][a][0]; } for(int k = 1; k < K; ++k) for(int a = 0; a < 4; ++a){ dp[i][k][a][1] = add(dp[i][k-1][a][1], dp[i][k][a][0]); } } if(ki >= K)cout << 0 << '\n'; else cout << dp[n][ki][0][0] << '\n'; } |