Under the Rainbow
70 colored balls are placed in an urn, 10 for each of the seven rainbow colors. What is the expected number of distinct colors in 20 randomly picked balls? Give your answer with nine digits after t...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
\(70\) coloured balls are placed in an urn, \(10\) for each of the seven rainbow colours.
What is the expected number of distinct colours in \(20\) randomly picked balls?
Give your answer with nine digits after the decimal point (\(a.bcdefghij\)).
Problem 493: Under the Rainbow
Mathematical Foundation
Theorem 1 (Linearity of expectation for indicator variables). Let where each is an indicator random variable. Then , regardless of dependence among the .
Proof. By the linearity of expectation, . Since , .
Theorem 2 (Expected distinct colors). Let be the number of distinct colors among 20 balls drawn without replacement from an urn of 70 balls (10 of each of 7 colors). Then
Proof. For each color , define . Then . By Theorem 1:
by symmetry (all colors are interchangeable). The complement is:
since there are ways to choose 20 balls from the 60 non-color-1 balls, out of total ways. Hence , and the result follows.
Lemma 1 (Product formula for the ratio). The binomial ratio simplifies as:
Proof. We have . Writing this as a ratio of falling factorials:
The numerator has factors and the denominator has . The common factors cancel, leaving .
Editorial
We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.
Pseudocode
ratio = 1
For k from 0 to 9:
ratio *= (50 - k) / (70 - k)
answer = 7 * (1 - ratio)
output answer rounded to 9 decimal places
Complexity Analysis
- Time: — a fixed product of 10 terms and one subtraction/multiplication.
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main(){
// E[distinct colors] = 7 * (1 - C(60,20)/C(70,20))
// C(60,20)/C(70,20) = prod_{k=0}^{9} (50-k)/(70-k)
// Use exact rational arithmetic with long doubles or fractions
// For precision, compute with exact fractions using __int128 or just careful doubles
// Actually, let's use exact rational computation
// Numerator: 50*49*48*47*46*45*44*43*42*41
// Denominator: 70*69*68*67*66*65*64*63*62*61
// These fit in unsigned long long
// num = 50*49*48*47*46*45*44*43*42*41
// den = 70*69*68*67*66*65*64*63*62*61
// For exact decimal, use high-precision approach
// But we can use long double which has ~18 digits of precision
long double ratio = 1.0L;
for(int k = 0; k < 10; k++){
ratio *= (long double)(50 - k) / (long double)(70 - k);
}
long double answer = 7.0L * (1.0L - ratio);
cout << fixed << setprecision(9) << answer << endl;
return 0;
}
from fractions import Fraction
from math import comb
def solve():
"""
Expected number of distinct colors when drawing 20 balls from 70
(10 each of 7 colors).
E = 7 * (1 - C(60,20)/C(70,20))
"""
# Use exact rational arithmetic
ratio = Fraction(comb(60, 20), comb(70, 20))
expected = 7 * (1 - ratio)
# Convert to decimal with 9 places
# expected is a Fraction
# We need to print with exactly 9 decimal places
numerator = expected.numerator
denominator = expected.denominator
# Integer part
integer_part = numerator // denominator
remainder = numerator % denominator
# Decimal part: multiply remainder by 10^9, divide by denominator
decimal_part = (remainder * 10**9) // denominator
# Check rounding
next_digit = (remainder * 10**10) // denominator % 10
if next_digit >= 5:
decimal_part += 1
print(f"{integer_part}.{decimal_part:09d}")
solve()