//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") //~ #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (1e3)+7; const int maxk = 11; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; // ***************************** CODE ***************************** // LL dp[maxn][maxk + 3][3]; LL pref[maxn][maxk + 3][3]; LL net[maxn][maxn]; int main() { _upgrade int n, K, mod; cin>>n>>K>>mod; if(K > 10) { cout<<"0"; return 0; } net[0][0] = 1; for(int i = 1;i < maxn;i++) for(int j = 0;j < maxn;j++) net[i][j] = (net[i - 1][j] + (j == 0 ? 0 : net[i - 1][j - 1])) % mod; dp[2][0][2] = dp[1][0][2] = dp[0][0][2] = 1; dp[0][0][1] = dp[1][0][1] = 1; for(int i = 1;i <= n;i++) { for(int j = 2 ;j < i;j++) for(int k = 0;k < maxk;k++) { dp[i][k][2] = (dp[i][k][2] + (((dp[j][k][2] * pref[i - j + 1][k - 1][2]) % mod ) * net[i - 3][j - 2])) % mod; dp[i][k][2] = (dp[i][k][2] + (((pref[j][k - 1][2] * dp[i - j + 1][k][2]) % mod ) * net[i - 3][j - 2])) % mod; dp[i][k + 1][2] = (dp[i][k + 1][2] + (((dp[j][k][2] * dp[i - j + 1][k][2]) % mod ) * net[i - 3][j - 2])) % mod; } for(int j = 1;j < i;j++) for(int k = 0;k < maxk;k++) { if(k > 1) dp[i][k][1] = (dp[i][k][1] + (((dp[j][k][1] * pref[i - j + 1][k - 2][2]) % mod) * net[i - 2][j - 1])) % mod; if(k > 0) { dp[i][k][1] = (dp[i][k][1] + (((pref[j][k - 1][1] * dp[i - j + 1][k - 1][2]) % mod) * net[i - 2][j - 1])) % mod; dp[i][k][1] = (dp[i][k][1] + (((dp[j][k][1] * dp[i - j + 1][k - 1][2]) % mod) * net[i - 2][j - 1])) % mod; } } for(int j = 1;j <= i;j++) for(int k = 0;k < maxk;k++) { dp[i][k][0] = (dp[i][k][0] + (((dp[j][k][1] * pref[i - j + 1][k - 1][1]) % mod ) * net[i - 1][j - 1])) % mod; dp[i][k][0] = (dp[i][k][0] + (((pref[j][k - 1][1] * dp[i - j + 1][k][1]) % mod ) * net[i - 1][j - 1])) % mod; dp[i][k][0] = (dp[i][k][0] + (((dp[j][k][1] * dp[i - j + 1][k][1]) % mod ) * net[i - 1][j - 1])) % mod; } for(int k = 0;k < maxk;k++) for(int trzy = 0;trzy < 3;trzy++) pref[i][k][trzy] = ((k == 0 ? 0 : pref[i][k - 1][trzy]) + dp[i][k][trzy]) % mod; } // LL res = 0LL; // for(int i = 0;i <= n;i++) // for(int k = 0;k <= 10;k++) // { // res += (i * k * (LL) dp[i][k][0]); // res %= mod; // } // cout<<res<<endl; cout<<dp[n][K][0]; return 0; }
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | //~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") //~ #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (1e3)+7; const int maxk = 11; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; // ***************************** CODE ***************************** // LL dp[maxn][maxk + 3][3]; LL pref[maxn][maxk + 3][3]; LL net[maxn][maxn]; int main() { _upgrade int n, K, mod; cin>>n>>K>>mod; if(K > 10) { cout<<"0"; return 0; } net[0][0] = 1; for(int i = 1;i < maxn;i++) for(int j = 0;j < maxn;j++) net[i][j] = (net[i - 1][j] + (j == 0 ? 0 : net[i - 1][j - 1])) % mod; dp[2][0][2] = dp[1][0][2] = dp[0][0][2] = 1; dp[0][0][1] = dp[1][0][1] = 1; for(int i = 1;i <= n;i++) { for(int j = 2 ;j < i;j++) for(int k = 0;k < maxk;k++) { dp[i][k][2] = (dp[i][k][2] + (((dp[j][k][2] * pref[i - j + 1][k - 1][2]) % mod ) * net[i - 3][j - 2])) % mod; dp[i][k][2] = (dp[i][k][2] + (((pref[j][k - 1][2] * dp[i - j + 1][k][2]) % mod ) * net[i - 3][j - 2])) % mod; dp[i][k + 1][2] = (dp[i][k + 1][2] + (((dp[j][k][2] * dp[i - j + 1][k][2]) % mod ) * net[i - 3][j - 2])) % mod; } for(int j = 1;j < i;j++) for(int k = 0;k < maxk;k++) { if(k > 1) dp[i][k][1] = (dp[i][k][1] + (((dp[j][k][1] * pref[i - j + 1][k - 2][2]) % mod) * net[i - 2][j - 1])) % mod; if(k > 0) { dp[i][k][1] = (dp[i][k][1] + (((pref[j][k - 1][1] * dp[i - j + 1][k - 1][2]) % mod) * net[i - 2][j - 1])) % mod; dp[i][k][1] = (dp[i][k][1] + (((dp[j][k][1] * dp[i - j + 1][k - 1][2]) % mod) * net[i - 2][j - 1])) % mod; } } for(int j = 1;j <= i;j++) for(int k = 0;k < maxk;k++) { dp[i][k][0] = (dp[i][k][0] + (((dp[j][k][1] * pref[i - j + 1][k - 1][1]) % mod ) * net[i - 1][j - 1])) % mod; dp[i][k][0] = (dp[i][k][0] + (((pref[j][k - 1][1] * dp[i - j + 1][k][1]) % mod ) * net[i - 1][j - 1])) % mod; dp[i][k][0] = (dp[i][k][0] + (((dp[j][k][1] * dp[i - j + 1][k][1]) % mod ) * net[i - 1][j - 1])) % mod; } for(int k = 0;k < maxk;k++) for(int trzy = 0;trzy < 3;trzy++) pref[i][k][trzy] = ((k == 0 ? 0 : pref[i][k - 1][trzy]) + dp[i][k][trzy]) % mod; } // LL res = 0LL; // for(int i = 0;i <= n;i++) // for(int k = 0;k <= 10;k++) // { // res += (i * k * (LL) dp[i][k][0]); // res %= mod; // } // cout<<res<<endl; cout<<dp[n][K][0]; return 0; } |