Advertisement
RaFiN_

Team Work cf-932E

Jun 25th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. //Team Work cf-932E
  2. Another alternative solution for E:
  3.  
  4. Let's give x^k a combinatorial meaning. x^k is the number of combinations of picking k people from the subset with x people with repetition. For each subset we count all combinations and sum those.
  5.  
  6. Why not switch sums? For each combination we count the number of subsets in which the combination appears, and sum those. This obviously computes the same result.
  7.  
  8. Assume we have a combination of size k consisting of i distinct people. From which subsets can we construct this combination? Obviously the i people have to be in the subset, but for every other person we have two choices: he is in the subset or he isn't. It doesn't matter. Therefore there are 2^(n - i) such subsets.
  9.  
  10. Let dp[j][i] be the number of combinations of size j with i distinct people, then the result is . dp[j][i] can be computed very easily with, you guess it, dynamic programming.
  11.  
  12. To generate a combination with i elements from which j are distinct we have two options.
  13.  
  14. We can start with a combination with i - 1 elements from which j are distinct, and append an element that already appears in it. => j options to choose the new element => j * dp[i - 1][j].
  15. We can start with a combination with i - 1 elements from which j - 1 are distinct, and append a new element that doesn't appear in the combination => n - (j - 1) possibilities to choose the new element => (n - j + 1) * dp[i - 1][j - 1].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement