Project Euler Lab - Problem 602

#602 - Product of Head Counts

● AdvancedOfficial difficulty: 48%PolynomialsTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Alice enlists the help of some friends to generate a random number, using a single unfair coin. She and her friends sit around a table and, starting with Alice, they take it in turns to toss the coin. Everyone keeps a count of how many heads they obtain individually. The process ends as soon as Alice obtains a Head. At this point, Alice multiplies all her friends' Head counts together to obtain her random number.

As an illustration, suppose Alice is assisted by Bob, Charlie, and Dawn, who are seated round the table in that order, and that they obtain the sequence of Head/Tail outcomes THHH - TTTT - THHT - H beginning and ending with Alice. Then Bob and Charlie each obtain 2 heads, and Dawn obtains 1 head. Alice's random number is therefore \(2\times 2\times 1 = 4\).

Define \(e(n, p)\) to be the expected value of Alice's random number, where \(n\) is the number of friends helping (excluding Alice herself), and \(p\) is the probability of the coin coming up Tails.

It turns out that, for any fixed \(n\), \(e(n, p)\) is always a polynomial in \(p\). For example, \(e(3, p) = p^3 + 4p^2 + p\).

Define \(c(n, k)\) to be the coefficient of \(p^k\) in the polynomial \(e(n, p)\). So \(c(3, 1) = 1\), \(c(3, 2) = 4\), and \(c(3, 3) = 1\).

You are given that \(c(100, 40) \equiv 986699437 \text{ } (\text{mod } 10^9+7)\).

Find \(c(10000000, 4000000) \mod 10^9+7\).

This problem is taken from Project Euler, Problem 602.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=602. Published Saturday, 6th May 2017, 10:00 pm. Solved by 612 members at time of mirroring.

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.