fork download
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define PB push_back
  4. #define int long long
  5. #define st first
  6. #define nd second
  7. #define rd third
  8. #define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
  9. #define RE(i, n) FOR(i, 1, n)
  10. #define FORD(i, a, b) for(int i = (a); i >= (b); --i)
  11. #define REP(i, n) for(int i = 0;i <(n); ++i)
  12. #define VAR(v, i) __typeof(i) v=(i)
  13. #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
  14. #define ALL(x) (x).begin(), (x).end()
  15. #define SZ(x) ((int)(x).size())
  16. #define __builtin_ctz __builtin_ctzll
  17. #define __builtin_clz __builtin_clzll
  18. #define __builtin_popcount __builtin_popcountll
  19. using namespace std;
  20. template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
  21. template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  22. while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
  23. }
  24. #ifdef LOCAL
  25. #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
  26. #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
  27. #else
  28. #define debug(...) (__VA_ARGS__)
  29. #define debugv(x)
  30. #define cerr if(0)cout
  31. #endif
  32. #define next ____next
  33. #define prev ____prev
  34. #define left ____left
  35. #define hash ____hash
  36. typedef long long ll;
  37. typedef long double LD;
  38. typedef pair<int, int> PII;
  39. typedef pair<ll, ll> PLL;
  40. typedef vector<int> VI;
  41. typedef vector<VI> VVI;
  42. typedef vector<ll> VLL;
  43. typedef vector<pair<int, int> > VPII;
  44. typedef vector<pair<ll, ll> > VPLL;
  45.  
  46. template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
  47. template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
  48. template<class T1, class T2>
  49. ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
  50. template<class A, class B, class C> struct Triple { A first; B second; C third;
  51. bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
  52. template<class T> void ResizeVec(T&, vector<int>) {}
  53. template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  54. vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  55. for (T& v : vec) { ResizeVec(v, sz); }
  56. }
  57. typedef Triple<int, int, int> TIII;
  58. template<class A, class B, class C>
  59. ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
  60. template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  61. template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  62. template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  63.  
  64. const int K = 52;
  65. int dp[2][K][K][K * K / 2];
  66. int P;
  67. void Add(int& a, int b) {
  68. a += b;
  69. if (a >= P) {
  70. a -= P;
  71. }
  72. }
  73. void Sub(int& a, int b) {
  74. a -= b;
  75. if (a < 0) {
  76. a += P;
  77. }
  78. }
  79. const int N = 3000;
  80. int newt[N][N];
  81. int32_t main() {
  82.  
  83. ios_base::sync_with_stdio(0);
  84. cout << fixed << setprecision(10);
  85. cerr << fixed << setprecision(10);
  86. cin.tie(0);
  87. //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  88.  
  89. int n, m, k;
  90. cin>>n>>m>>k>>P;
  91.  
  92. dp[0][0][0][0] = 1;
  93. int nxt = 1;
  94. RE (i, n) {
  95. FOR (wyn, 0, k) {
  96. FOR (last_end, 0, i - 1) {
  97. FOR (luzne, 0, i * (i - 1) / 2 + 5) {
  98. dp[nxt][wyn][last_end][luzne] = 0;
  99. }
  100. }
  101. }
  102. FOR (wyn, 0, k) {
  103. FOR (last_end, 0, i - 1) {
  104. FOR (luzne, 0, i * (i - 1) / 2) {
  105. // 1 case - wyn sie nie zwieksza
  106. // wszystkie poczatki sa <= last_end,
  107. // zatem luzne zwieksza sie o last_end
  108. Add(dp[nxt][wyn][last_end][luzne + last_end], dp[nxt ^ 1][wyn][last_end][luzne]);
  109. if (luzne + last_end > 0) {
  110. Sub(dp[nxt][wyn][last_end][luzne + last_end - 1], dp[nxt ^ 1][wyn][last_end][luzne]);
  111. }
  112. // 2 case - wyn sie zwieksza
  113. // mam kogos z poczatkiem > last_end
  114. // jak koniec to x, to luzne zwieksza sie o x - 1
  115. // zatem na przedziale od luzne+last_end do luzne+i-1 zwiekszam od tyle
  116. Add(dp[nxt][wyn + 1][i][luzne + i - 1], dp[nxt ^ 1][wyn][last_end][luzne]);
  117. if(luzne + last_end > 0) {
  118. Sub(dp[nxt][wyn + 1][i][luzne + last_end - 1], dp[nxt ^ 1][wyn][last_end][luzne]);
  119. }
  120. }
  121. }
  122. }
  123. // robie sumy sufiksowe po luzne
  124. FOR (wyn, 0, k) {
  125. FOR (last_end, 0, i) {
  126. FORD (luzne, n * (n + 1) / 2, 0) {
  127. Add(dp[nxt][wyn][last_end][luzne], dp[nxt][wyn][last_end][luzne + 1]);
  128. }
  129. }
  130. }
  131. nxt ^= 1;
  132. }
  133. REP (i, N) {
  134. newt[i][0] = newt[i][i] = 1;
  135. }
  136. REP (i, N) {
  137. RE (j, i - 1) {
  138. newt[i][j] = (newt[i - 1][j] + newt[i - 1][j - 1]) % P;
  139. }
  140. }
  141. int res = 0;
  142. FOR (last_end, 0, n) {
  143. FOR (luzne, 0, n * (n + 1) / 2) {
  144. int to_take = m - k;
  145. debug(to_take, dp[nxt ^ 1][k][last_end][luzne], last_end, luzne);
  146. Add(res, dp[nxt ^ 1][k][last_end][luzne] * newt[luzne][to_take] % P);
  147. }
  148. }
  149. cout<<res<<endl;
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. return 0;
  177. }
Runtime error #stdin #stdout 0s 142656KB
stdin
Standard input is empty
stdout
Standard output is empty