All Euler problems
Project Euler

Catalan Number Variants

The n -th Catalan number is C_n = (1)/(n+1)C(2n, n). Find sum_(k=0)^1000 C_k (mod 10^9+7).

Source sync Apr 19, 2026
Problem #0928
Level Level 33
Solved By 244
Languages C++, Python
Answer 81108001093
Length 251 words
modular_arithmeticanalytic_mathsequence

Problem Statement

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

This problem is based on (but not identical to) the scoring for the card game Cribbage.

Consider a normal pack of \(52\) cards. A Hand is a selection of one or more of these cards.

For each Hand the Hand score is the sum of the values of the cards in the Hand where the value of Aces is \(1\) and the value of court cards (Jack, Queen, King) is \(10\).

The Cribbage score is obtained for a Hand by adding together the scores for:

  • Pairs. A pair is two cards of the same rank. Every pair is worth \(2\) points.

  • Runs. A run is a set of at least \(3\) cards whose ranks are consecutive, e.g. 9, 10, Jack. Note that Ace is never high, so Queen, King, Ace is not a valid run. The number of points for each run is the size of the run. All locally maximum runs are counted. For example, 2, 3, 4, 5, 7, 8, 9 the two runs of 2, 3, 4, 5 and 7, 8, 9 are counted but not 2, 3, 4 or 3, 4, 5.

  • Fifteens. A fifteen is a combination of cards that has value adding to \(15\). Every fifteen is worth \(2\) points. For this purpose the value of the cards is the same as in the Hand Score.

For example, \((5 \spadesuit , 5 \clubsuit , 5 \diamondsuit , K \heartsuit )\) has a Cribbage score of \(14\) as there are four ways that fifteen can be made and also three pairs can be made.

The example \(( A \diamondsuit , A \heartsuit , 2 \clubsuit , 3 \heartsuit , 4 \clubsuit , 5 \spadesuit )\) has a Cribbage score of \(16\): two runs of five worth \(10\) points, two ways of getting fifteen worth \(4\) points and one pair worth \(2\) points. In this example the Hand score is equal to the Cribbage score.

Find the number of Hands in a normal pack of cards where the Hand score is equal to the Cribbage score.

Problem 928: Catalan Number Variants

Mathematical Foundation

Theorem 1 (Ratio Recurrence). The Catalan numbers satisfy C0=1C_0 = 1 and

Cn+1=2(2n+1)n+2Cnfor n0.C_{n+1} = \frac{2(2n+1)}{n+2}\, C_n \quad \text{for } n \geq 0.

Proof. Compute the ratio directly:

Cn+1Cn=(2n+2n+1)/(n+2)(2nn)/(n+1)=(2n+2)!(n+1)!(n+1)!n!n!(2n)!n+1n+2.\frac{C_{n+1}}{C_n} = \frac{\binom{2n+2}{n+1}/(n+2)}{\binom{2n}{n}/(n+1)} = \frac{(2n+2)!}{(n+1)!(n+1)!} \cdot \frac{n! \cdot n!}{(2n)!} \cdot \frac{n+1}{n+2}.

Simplifying: (2n+2)(2n+1)(n+1)(n+1)n+1n+2=2(2n+1)n+2\frac{(2n+2)(2n+1)}{(n+1)(n+1)} \cdot \frac{n+1}{n+2} = \frac{2(2n+1)}{n+2}. \square

Theorem 2 (Segner Convolution Recurrence). For all n0n \geq 0:

Cn+1=i=0nCiCni.C_{n+1} = \sum_{i=0}^{n} C_i \, C_{n-i}.

Proof. Consider Dyck paths from (0,0)(0,0) to (2(n+1),0)(2(n+1), 0). Each such path first touches the xx-axis at some step 2(i+1)2(i+1) (the first return). The path decomposes as: an up-step, a Dyck path of semilength ii (shifted up by 1), a down-step, and a Dyck path of semilength nin - i. Since the first part has CiC_i choices and the second has CniC_{n-i} choices, summing over all first-return times i=0,,ni = 0, \ldots, n gives the recurrence. \square

Theorem 3 (Generating Function). The ordinary generating function C(x)=n=0CnxnC(x) = \sum_{n=0}^{\infty} C_n x^n satisfies the functional equation C(x)=1+xC(x)2C(x) = 1 + x\,C(x)^2, with explicit solution

C(x)=114x2x.C(x) = \frac{1 - \sqrt{1 - 4x}}{2x}.

Proof. Theorem 2 states Cn+1=i=0nCiCniC_{n+1} = \sum_{i=0}^{n} C_i C_{n-i}, which means n0Cn+1xn=C(x)2\sum_{n \geq 0} C_{n+1} x^n = C(x)^2. Multiplying by xx: C(x)1=xC(x)2C(x) - 1 = x \, C(x)^2, i.e., C(x)=1+xC(x)2C(x) = 1 + x\,C(x)^2. Solving this quadratic in C(x)C(x):

C(x)=1±14x2x.C(x) = \frac{1 \pm \sqrt{1 - 4x}}{2x}.

The minus sign is chosen to ensure C(0)=1C(0) = 1 (by L’Hopital or series expansion). \square

Theorem 4 (Asymptotic Growth). As nn \to \infty:

Cn4nn3/2π.C_n \sim \frac{4^n}{n^{3/2}\sqrt{\pi}}.

Proof. Using Stirling’s approximation n!2πn(n/e)nn! \sim \sqrt{2\pi n}\,(n/e)^n:

Cn=1n+1(2nn)=(2n)!(n+1)!n!4πn(2n/e)2n(n+1)2πn(n/e)2n=4n(n+1)πn4nn3/2π.C_n = \frac{1}{n+1}\binom{2n}{n} = \frac{(2n)!}{(n+1)!\,n!} \sim \frac{\sqrt{4\pi n}\,(2n/e)^{2n}}{(n+1)\cdot 2\pi n \cdot (n/e)^{2n}} = \frac{4^n}{(n+1)\sqrt{\pi n}} \sim \frac{4^n}{n^{3/2}\sqrt{\pi}}. \quad \square

Lemma 1 (Modular Inverse via Fermat). For prime pp and a≢0(modp)a \not\equiv 0 \pmod{p}: a1ap2(modp)a^{-1} \equiv a^{p-2} \pmod{p}.

Proof. By Fermat’s little theorem, ap11(modp)a^{p-1} \equiv 1 \pmod{p}, so aap21a \cdot a^{p-2} \equiv 1. \square

Editorial

Alternative (precompute factorials). We method 1: Using ratio recurrence with modular inverses. 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

Method 1: Using ratio recurrence with modular inverses
C_{n+1} = C_n * 2*(2n+1) * inverse(n+2) mod MOD
Precompute factorials and inverse factorials mod MOD
C_k = fact[2k] * inv_fact[k+1] * inv_fact[k] mod MOD

Complexity Analysis

  • Time (ratio recurrence): O(Nlogp)O(N \log p), where each step requires one modular inverse via binary exponentiation in O(logp)O(\log p).
  • Time (factorial precomputation): O(N+logp)O(N + \log p). Precomputing factorials is O(N)O(N); a single modular inverse costs O(logp)O(\log p); the remaining inverse factorials are O(N)O(N) via backward recurrence.
  • Space: O(1)O(1) for the ratio method; O(N)O(N) for the factorial method.

Answer

81108001093\boxed{81108001093}

Code

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

C++ project_euler/problem_928/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 928: Catalan Number Variants
 *
 * Find sum_{k=0}^{1000} C_k mod 10^9+7.
 * C_n = (2n)! / ((n+1)! * n!).
 *
 * Recurrence: C_{k+1} = C_k * 2(2k+1) / (k+2).
 * Or use factorial precomputation with modular inverses.
 *
 * Generating function: C(x) = (1 - sqrt(1-4x)) / (2x).
 * Asymptotics: C_n ~ 4^n / (n^{3/2} sqrt(pi)).
 *
 * Complexity: O(N) with factorial precomputation.
 */

long long MOD = 1000000007;

long long power(long long a, long long b, long long mod) {
    long long res = 1; a %= mod;
    while (b > 0) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int main() {
    int N = 1000;
    vector<long long> fact(2*N + 2), inv_fact(2*N + 2);
    fact[0] = 1;
    for (int i = 1; i <= 2*N + 1; i++)
        fact[i] = fact[i-1] * i % MOD;
    inv_fact[2*N+1] = power(fact[2*N+1], MOD-2, MOD);
    for (int i = 2*N; i >= 0; i--)
        inv_fact[i] = inv_fact[i+1] * (i+1) % MOD;

    long long total = 0;
    for (int k = 0; k <= N; k++) {
        // C_k = (2k)! / (k! * (k+1)!)
        long long c = fact[2*k] % MOD * inv_fact[k] % MOD * inv_fact[k+1] % MOD;
        total = (total + c) % MOD;
    }
    cout << total << endl;

    // Verify first Catalan numbers: 1, 1, 2, 5, 14, 42
    auto catalan = [&](int k) {
        return fact[2*k] % MOD * inv_fact[k] % MOD * inv_fact[k+1] % MOD;
    };
    assert(catalan(0) == 1);
    assert(catalan(1) == 1);
    assert(catalan(2) == 2);
    assert(catalan(3) == 5);
    assert(catalan(4) == 14);

    return 0;
}