The problem in brief
A space station is surrounded by meteorites, and exactly of them carry gold. Every minute one meteorite, chosen among those still out there, is pulled in, so the meteorites arrive in a uniformly random order and none comes back. You read and () and print the expected number of minutes until the first gold meteorite arrives. The whole task is to find a closed formula: the program itself is one line.
How you are scored
This is a classic stdin/stdout judge task (1 second, 64 MB): your printed number is compared with the reference value on hidden tests, presumably with a small floating-point tolerance. The only published test is 1 1, answer 1. Since the official tests are hidden, we validate the formula against two independent exact computations on all 820 pairs with , plus a Monte Carlo simulation.
Answer:
First look: what is random here?
Let be the minute at which the first gold meteorite arrives. is at least 1 (the very first pull may be gold) and at most (in the worst order, all empty rocks come first). Two quick sanity cases:
- : every meteorite is gold, . The formula gives .
- : the single gold meteorite is equally likely to be at any of the positions, so . The formula agrees.
The tempting wrong answer is . That is the expectation of a geometric law, the waiting time when each pull is gold with probability independently, which would mean throwing meteorites back after looking at them. Without replacement, every empty rock you pull makes the next pull more likely to be gold, so the wait is shorter. The right panel of the figure shows the gap: for it is against the true .
![Left: the law of T for x = 20 and g = 1, 3, 8, with its mean as a dashed line. For g = 1 the law is flat (uniform position), for larger g it decays fast. Right: E[T] as a function of g for x = 20: the formula (x+1)/(g+1) passes exactly through the simulated points, while x/g, the answer with replacement, is too large for small g.](/corrections/kz-faio-2024-qual-h-meteorites-with-gold/figures/distribution.png)
The key idea
Look at the gaps, not at the minutes. Lay the meteorites out in their arrival order. The gold ones cut the line of empty rocks into gaps:
(here , , gaps of sizes ). The first gold meteorite arrives right after the first gap, so
Now the symmetry argument. Take one particular empty rock. Together with the gold meteorites it forms a group of objects whose relative order is uniformly random, so the rock is equally likely to be in any of the slots "before the 1st gold", "between the 1st and 2nd", ..., "after the last". In particular it falls into gap with probability . By linearity of expectation (no independence needed), summing over the empty rocks:
All gaps have the same expected size, so the average gap is the total shared equally among gaps. This "indicator per object + linearity" trick is the single most useful tool for expectation problems: it replaces a messy distribution by one probability per object.
A second derivation, by brute force
If you do not trust symmetry, compute the law of directly. means that the first pulls are empty, the -th is gold, and the remaining gold ones are somewhere among the last positions. Counting sets of gold positions:
Then . Using the tail-sum formula with (the first pulls are all empty) gives a third route. solution.py evaluates both sums with exact fractions and checks they equal in all 820 cases with . (Proving the identity algebraically uses the hockey-stick identity ; the gap argument gets there without any algebra.)
The full solution
x, g = map(int, input().split())
print(f"{(x + 1) / (g + 1):.10g}")
It runs in constant time, far inside the limit even for . Two practical details:
- Use floating-point division and print enough digits: is usually not an integer (for
10 3the answer is2.75). %.10gprints1for the sample, exactly as expected, and up to 10 significant digits otherwise. Simulating the process instead (random shuffles until the first gold) would be both too slow for and not precise enough for a numeric checker.
Results
| approach | value for x=10, g=3 | verdict |
|---|---|---|
| closed form | 2.75 | equals both exact sums on all 820 pairs with |
| exact sum (fractions) | 11/4 | agrees |
| tail sum (fractions) | 11/4 | agrees |
| Monte Carlo, 200 000 shuffles | 2.7507 | agrees within noise |
| geometric law (with replacement) | 3.333 | wrong: ignores that rocks are not put back |
| or (off-by-one variants) | 2.333, 2.5 | wrong, fails the sample or g = x |
What to remember
- Check the extreme cases first (, ): they kill wrong formulas in seconds and suggest the right one.
- Without replacement is not geometric. Draws without replacement are dependent; the answer is always smaller than the independent-draws answer .
- Indicators plus linearity of expectation: write the quantity as a sum of 0/1 variables, one per object, and add their probabilities. No independence is required.
- Symmetry of random orders: in a uniformly random permutation, any object is equally likely to sit in any of the gaps defined by others.
- Verify a formula three ways (exact sum, independent identity, simulation) before submitting: it is cheap and catches off-by-one mistakes.