Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- ll n, nar;
- ll dp[1000][100];
- ll MOD = 1e9 + 7;
- vector<ll> ar;
- /*
- See main function first.
- */
- ll go(ll start, ll k) {
- //If it already exists, return it.
- if (dp[start][k] != -1) {
- return dp[start][k];
- }
- /*
- If we are selecting NO people, then there is exactly
- one way to play - select no people.
- */
- if (k == 0) {
- return dp[start][k] = 1;
- }
- /*
- It is impossible to play if start exceeds ar.
- */
- if (start >= nar) {
- return dp[start][k] = 0;
- }
- /*
- It is impossible to play if we are trying to select more
- people than there are from start...end of ar.
- */
- if ((nar - start) < k) {
- return dp[start][k] = 0;
- }
- /*
- To calculate a k-wise product, we can either select the current
- element in ar, and then move to the rest of ar but with k decreased
- by 1*/
- ll x = (ar[start] * go(start + 1, k - 1)) % MOD;
- /*
- Or we can not select the current element, move forward with k unchanged.
- */
- ll y = go(start + 1, k) % MOD;
- return dp[start][k] = (x + y) % MOD;
- }
- int main() {
- ar.push_back(1);
- ll k, t;
- scanf("%lld %lld", &n, &k);
- memset(dp, -1, 100000 * sizeof(ll));
- /*
- This generates ar such that every element
- in ar is now the number of brothers on a single "layer".
- So, ar[0] are all brothers, ar[1] are all brothers and
- so on.
- */
- for (ll i = 0; i < n - 1; ++i) {
- scanf("%lld", &t);
- if (ar.size() > t) ar[t] += 1;
- else ar.push_back(1);
- }
- nar = ar.size();
- /*
- The logic is to now take the product of the elements
- of ar, k at a time. For example, if k = 2 and ar is 1 2 3 4,
- we want the result to be = 1*2 + 1*3 + 1*4 + 2*3 + 2*4 + 3*4.
- dp[start][k] indicates the number of ways
- to select k people to play, assuming we start from
- index "start" in ar. So, our final answer is dp[0][k].
- */
- printf("%lld\n", go(0, k));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement