All Euler problems
Project Euler

Birthday Paradox Variants

In the classical birthday problem, n people each have a birthday uniformly distributed among N days. The probability that at least two share a birthday is: P(n, N) = 1 - prod_(k=0)^(n-1)(1 - (k)/(N...

Source sync Apr 19, 2026
Problem #0890
Level Level 34
Solved By 233
Languages C++, Python
Answer 820442179
Length 353 words
probabilityanalytic_mathmodular_arithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let \(p(n)\) be the number of ways to write \(n\) as the sum of powers of two, ignoring order.

For example, \(p(7) = 6\), the partitions being \begin {align*} 7 &= 1 + 1 + 1 + 1 + 1 + 1 + 1 \\ &= 1 + 1 + 1 + 1 + 1 + 2 \\ &= 1 + 1 + 1 + 2 + 2 \\ &= 1 + 1 + 1 + 4 \\ &= 1 + 2 + 2 + 2 \\ &= 1 + 2 + 4 \end {align*}

You are also given \(p(7^7) \equiv 144548435 \mod {10^9 + 7}\).

Find \(p(7^{777})\). Give your answer modulo \(10^9 + 7\).

Problem 890: Birthday Paradox Variants

Mathematical Analysis

Theorem 1 (Exact Collision Probability)

P(n,N)=1N(N1)(N2)(Nn+1)Nn=1N!Nn(Nn)!P(n, N) = 1 - \frac{N(N-1)(N-2)\cdots(N-n+1)}{N^n} = 1 - \frac{N!}{N^n(N-n)!}

Proof. The probability of no collision is NNN1NN2NNn+1N\frac{N}{N} \cdot \frac{N-1}{N} \cdot \frac{N-2}{N} \cdots \frac{N-n+1}{N}. \square

Theorem 2 (Poisson Approximation)

For nNn \ll N:

P(n,N)1e(n2)/N=1en(n1)/(2N)P(n, N) \approx 1 - e^{-\binom{n}{2}/N} = 1 - e^{-n(n-1)/(2N)}

Proof. Using ln(1k/N)k/N\ln(1 - k/N) \approx -k/N for small k/Nk/N:

lnk=0n1(1k/N)k=0n1k/N=n(n1)2N\ln\prod_{k=0}^{n-1}(1-k/N) \approx -\sum_{k=0}^{n-1} k/N = -\frac{n(n-1)}{2N} \qquad \square

Theorem 3 (50% Threshold)

P(n,N)1/2P(n, N) \geq 1/2 when n2Nln21.1774Nn \geq \sqrt{2N \ln 2} \approx 1.1774\sqrt{N}.

For N=365N = 365: n2365ln222.49n \geq \sqrt{2 \cdot 365 \cdot \ln 2} \approx 22.49, so n=23n = 23.

Theorem 4 (kk-wise Collision)

The probability that some group of kk people share a birthday:

Pk(n,N)1exp((nk)/Nk1)P_k(n, N) \approx 1 - \exp\left(-\binom{n}{k}/N^{k-1}\right)

Theorem 5 (Non-uniform Distribution)

For non-uniform birthday probabilities p1,,pNp_1, \ldots, p_N with pi=1\sum p_i = 1:

P1exp((n2)ipi2)P \approx 1 - \exp\left(-\binom{n}{2}\sum_i p_i^2\right)

The collision probability is maximized when the distribution is uniform (by Schur-convexity).

Concrete Numerical Examples

Classical Birthday Problem (N=365N = 365)

nnP(n,365)P(n, 365) exactPoisson approx
50.027140.02713
100.116950.11614
200.411440.39957
230.507300.49270
300.706320.68436
500.970370.95393
570.990120.97857
700.999160.99684

Threshold n1/2n_{1/2} for Various NN

NNn1/2n_{1/2} (exact)1.1774N1.1774\sqrt{N}
3652322.49
1001211.77
10003837.23
10000119117.74
10610^611781177.4

Hash Collision Application

For a hash function with N=2128N = 2^{128} outputs, 50% collision probability at n264n \approx 2^{64} attempts (the “birthday attack”).

Coupon Collector Connection

While the birthday problem asks “when do we first see a repeat?”, the coupon collector problem asks “when do we first see all NN types?” The expected time is NHN=Nk=1N1/kNlnNN H_N = N \sum_{k=1}^{N} 1/k \approx N \ln N.

Higher-Order Collisions

Expected Number of Collisions

The expected number of pairs sharing a birthday:

E[pairs]=(n2)1NE[\text{pairs}] = \binom{n}{2} \cdot \frac{1}{N}

For n=23,N=365n = 23, N = 365: E=253/3650.693E = 253/365 \approx 0.693.

Expected Number of Empty Bins

E[empty bins]=N(11N)nNen/NE[\text{empty bins}] = N\left(1 - \frac{1}{N}\right)^n \approx N e^{-n/N}

Variance of Collision Count

Var[pairs]=(n2)1N(11N)+3(n3)1N2\text{Var}[\text{pairs}] = \binom{n}{2}\frac{1}{N}\left(1-\frac{1}{N}\right) + 3\binom{n}{3}\frac{1}{N^2}

Generalized Occupancy Problems

The birthday problem is a special case of the random allocation or occupancy problem: throwing nn balls into NN bins uniformly. Key quantities:

  • Probability of no empty bin: N!NnS(n,N)\frac{N!}{N^n} S(n, N) (Stirling numbers)
  • Maximum load: Θ(logn/loglogn)\Theta(\log n / \log \log n) w.h.p.
  • First collision: Θ(N)\Theta(\sqrt{N}) w.h.p.

Complexity Analysis

MethodTime
Exact formulaO(n)O(n)
Poisson approximationO(1)O(1)
Monte Carlo simulationO(ntrials)O(n \cdot \text{trials})

Answer

820442179\boxed{820442179}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_890/solution.cpp
/*
 * Problem 890: Birthday Paradox Variants
 * P(n,N) = 1 - prod_{k=0}^{n-1} (1 - k/N).
 */
#include <bits/stdc++.h>
using namespace std;

double birthday_exact(int n, int N) {
    if (n > N) return 1.0;
    double p = 1.0;
    for (int k = 0; k < n; k++) p *= (double)(N - k) / N;
    return 1.0 - p;
}

double birthday_poisson(int n, int N) {
    double lam = (double)n * (n - 1) / (2.0 * N);
    return 1.0 - exp(-lam);
}

int threshold_50(int N) {
    for (int n = 1; n <= N + 1; n++)
        if (birthday_exact(n, N) >= 0.5) return n;
    return N + 1;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cout << fixed << setprecision(5);
    cout << "=== Birthday Problem (N=365) ===" << endl;
    for (int n : {5, 10, 20, 23, 30, 50, 70})
        cout << "P(" << n << ",365) = " << birthday_exact(n, 365) << endl;

    cout << "\n=== Thresholds ===" << endl;
    for (int N : {100, 365, 1000, 10000})
        cout << "n_50(" << N << ") = " << threshold_50(N) << endl;

    cout << "\nAnswer: P(23,365) = " << birthday_exact(23, 365) << endl;
    return 0;
}