All Euler problems
Project Euler

Stirling Number Asymptotics

The Bell number B_n = sum_(k=0)^n S(n,k) counts the number of partitions of {1,..., n}. Find B_1000 mod (10^9+7).

Source sync Apr 19, 2026
Problem #0984
Level Level 39
Solved By 89
Languages C++, Python
Answer 465231251
Length 63 words
modular_arithmeticgeometrylinear_algebra

Problem Statement

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

In Western chess, a knight is a piece that moves either two squares horizontally and one square vertically, or one square horizontally and two squares vertically, and is capable of jumping over any intervening pieces.

Chinese chess has a similar piece called the horse, whose moves have an identical displacement as a knight's move; however, a horse, unlike a knight, is unable to jump over intervening pieces.

More specifically, a horse's move consists of two steps: An orthogonal move of one square, followed by a diagonal move by one square in the same direction as the orthogonal move. If the orthogonal square is occupied by another piece, the horse is unable to move in that direction. 0984_KnightHorsesDiag1.jpg Specifically the horse in the centre of the above board can move to the squares providing the squares are unoccupied. For example, if was occupied then it could not move to or .

A set of squares on a chessboard is called knight-connected if a knight can travel between any two squares in the set using only legal moves without using any squares not in the set. A set of squares on a chessboard is called horse-disjoint if, when a horse is placed on every square in the set (and no other square), no horse can attack any other. 0984_KnightHorsesDiag2.jpg Let be the number of knight-connected, horse-disjoint non-empty subsets of an chessboard. For example, , consisting of the nine singleton sets. You are also given that , and .

Find . Give your answer modulo .

Problem 984: Stirling Number Asymptotics

Mathematical Analysis

Bell Numbers

Theorem (Bell Triangle). BnB_n can be computed via the Bell triangle:

  • a0,0=1a_{0,0} = 1.
  • an,0=an1,n1a_{n,0} = a_{n-1,n-1}.
  • an,k=an,k1+an1,k1a_{n,k} = a_{n,k-1} + a_{n-1,k-1}.
  • Bn=an,0B_n = a_{n,0}.

Theorem (Dobinski’s Formula). Bn=1ek=0knk!B_n = \frac{1}{e} \sum_{k=0}^{\infty} \frac{k^n}{k!}.

Growth Rate

Theorem. lnBnnlnnnlnlnnn\ln B_n \sim n \ln n - n \ln \ln n - n as nn \to \infty.

For n=1000n = 1000: B1000B_{1000} has approximately 2000 digits.

Derivation

Editorial

Use the Bell triangle with two-row rolling array, reducing modulo 109+710^9 + 7 at each step.

Complexity Analysis

O(N2)O(N^2) time, O(N)O(N) space.

Answer

465231251\boxed{465231251}

Code

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

C++ project_euler/problem_984/solution.cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9+7;
int main() {
    int n = 1000;
    vector<long long> row(n+1, 0);
    row[0] = 1;
    for (int i = 1; i <= n; i++) {
        vector<long long> nr(n+1, 0);
        nr[0] = row[i-1];
        for (int j = 1; j <= i; j++)
            nr[j] = (nr[j-1] + row[j-1]) % MOD;
        row = nr;
    }
    cout << row[0] << endl;
    return 0;
}