Advertisement
Guest User

Untitled

a guest
Oct 14th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. ll n, nar;
  8.  
  9. ll dp[1000][100];
  10.  
  11. ll MOD = 1e9 + 7;
  12.  
  13. vector<ll> ar;
  14.  
  15.  
  16. /*
  17. See main function first.
  18. */
  19. ll go(ll start, ll k) {
  20. //If it already exists, return it.
  21. if (dp[start][k] != -1) {
  22. return dp[start][k];
  23. }
  24.  
  25. /*
  26. If we are selecting NO people, then there is exactly
  27. one way to play - select no people.
  28. */
  29. if (k == 0) {
  30. return dp[start][k] = 1;
  31. }
  32.  
  33. /*
  34. It is impossible to play if start exceeds ar.
  35. */
  36. if (start >= nar) {
  37. return dp[start][k] = 0;
  38. }
  39.  
  40. /*
  41. It is impossible to play if we are trying to select more
  42. people than there are from start...end of ar.
  43. */
  44. if ((nar - start) < k) {
  45. return dp[start][k] = 0;
  46. }
  47.  
  48. /*
  49. To calculate a k-wise product, we can either select the current
  50. element in ar, and then move to the rest of ar but with k decreased
  51. by 1*/
  52. ll x = (ar[start] * go(start + 1, k - 1)) % MOD;
  53.  
  54. /*
  55. Or we can not select the current element, move forward with k unchanged.
  56. */
  57. ll y = go(start + 1, k) % MOD;
  58.  
  59. return dp[start][k] = (x + y) % MOD;
  60. }
  61.  
  62.  
  63. int main() {
  64. ar.push_back(1);
  65.  
  66. ll k, t;
  67. scanf("%lld %lld", &n, &k);
  68.  
  69. memset(dp, -1, 100000 * sizeof(ll));
  70.  
  71. /*
  72. This generates ar such that every element
  73. in ar is now the number of brothers on a single "layer".
  74. So, ar[0] are all brothers, ar[1] are all brothers and
  75. so on.
  76. */
  77. for (ll i = 0; i < n - 1; ++i) {
  78. scanf("%lld", &t);
  79. if (ar.size() > t) ar[t] += 1;
  80. else ar.push_back(1);
  81. }
  82.  
  83.  
  84. nar = ar.size();
  85.  
  86. /*
  87. The logic is to now take the product of the elements
  88. of ar, k at a time. For example, if k = 2 and ar is 1 2 3 4,
  89. we want the result to be = 1*2 + 1*3 + 1*4 + 2*3 + 2*4 + 3*4.
  90. dp[start][k] indicates the number of ways
  91. to select k people to play, assuming we start from
  92. index "start" in ar. So, our final answer is dp[0][k].
  93. */
  94. printf("%lld\n", go(0, k));
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement