Social feed~25 min2s · 256 MBtarget O(top + q) time, O(top) spacenumber-theory
You work on a social app that runs prize draws for its creators. In each draw, n entries are in the pool and k of them win. The order of the winners does not matter.
Your team lead asks: "For every draw, how many different sets of winners can come out?" The count is huge, so report it modulo 1,000,000,007, a prime. A request holds up to 100,000 draws, and a pool holds up to 1,000,000 entries.
Write draw_outcomes(pool_sizes, winners).
pool_sizes[i] is the number of entries in draw i.winners[i] is the number of winners in draw i.winners[i] entries out of pool_sizes[i], modulo 1,000,000,007.If a draw has more winners than entries, no set exists, so the answer is 0. A draw with no winners has exactly one set, the empty one.
Input: pool_sizes = [5, 10, 3, 0], winners = [2, 3, 4, 0]
Output: [10, 120, 0, 1]| Draw | Entries | Winners | Sets |
|---|---|---|---|
| 0 | 5 | 2 | 10 |
| 1 | 10 | 3 | 120 |
| 2 | 3 | 4 | 0: more winners than entries |
| 3 | 0 | 0 | 1: the empty set |
Input: pool_sizes = [4, 6], winners = [1, 6]
Output: [4, 1]One winner out of four entries can be chosen in 4 ways. Choosing all 6 of 6 entries can be done in one way.
Input: pool_sizes = [1000], winners = [500]
Output: [159835829]The exact count has about 300 digits, so only its remainder modulo 1,000,000,007 is reported.
1 ≤ len(pool_sizes) ≤ 100,000, and winners has the same length.0 ≤ pool_sizes[i] ≤ 1,000,0000 ≤ winners[i] ≤ 1,000,000The exact number of sets for one draw with a pool of 1,000,000 has about 301,000 digits. Building it takes far longer than the time limit for a single draw, and a request holds 100,000 draws. The factorials up to 1,000,000 are only 1,000,001 numbers, and each answer needs three of them. A table built once and read for every draw fits easily.