All Euler problems
Project Euler

Modular Combinatorics

Find sum_(k=0)^(10^6) C(10^6, k) * k^3 (mod 10^9+7).

Source sync Apr 19, 2026
Problem #0960
Level Level 37
Solved By 164
Languages C++, Python
Answer 243559751
Length 238 words
modular_arithmeticcombinatoricsbrute_force

Problem Statement

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

There are $n$ distinct piles of stones, each of size $n-1$. Starting with an initial score of $0$, the following procedure is repeated

  1. Choose any two piles and remove exactly $n$ stones in total from the two piles.

  2. If the number of stones removed from the two piles were $a$ and $b$, add $\min(a,b)$ to the score.

If all piles are eventually emptied, the current score is confirmed as final. However, if one gets "stuck" and cannot empty all piles, the current score is discarded, resulting in a final score of $0$.

Three example sequences of turns are illustrated below for $n=4$, with each tuple representing pile sizes as one proceeds, and with additions to the score indicated above the arrows. \begin{align} &(3,3,3,3)\xrightarrow{+1}(0,3,2,3)\xrightarrow{+1}(0,3,1,0)\xrightarrow{+1}(0,0,0,0)&:\quad\text{final score }=3\\ &(3,3,3,3)\xrightarrow{+1}(3,0,3,2)\xrightarrow{+2}(1,0,3,0)\xrightarrow{+1}(0,0,0,0)&:\quad\text{final score }=4\\ &(3,3,3,3)\xrightarrow{+2}(1,3,1,3)\xrightarrow{+1}(1,2,1,0)\rightarrow\text{stuck!}&:\quad\text{final score }=0 \end{align}

Define $F(n)$ to be the sum of the final scores achieved for every sequence of turns which successfully empty all piles.

You are given $F(3)=12$, $F(4)=360$, and $F(8)=16785941760$.

Find $F(100)$. Give your answer modulo $10^9+7$.

Problem 960: Modular Combinatorics

Mathematical Analysis

Stirling Numbers of the Second Kind

To evaluate k(nk)km\sum_k \binom{n}{k} k^m, we expand kmk^m in the falling factorial basis using Stirling numbers of the second kind S(m,r)S(m, r):

km=r=0mS(m,r)krk^m = \sum_{r=0}^{m} S(m, r) \cdot k^{\underline{r}}

where kr=k(k1)(k2)(kr+1)k^{\underline{r}} = k(k-1)(k-2)\cdots(k-r+1) is the falling factorial.

Theorem (Stirling Expansion). For m=3m = 3:

k3=S(3,1)k1+S(3,2)k2+S(3,3)k3=k+3k(k1)+k(k1)(k2)k^3 = S(3,1) k^{\underline{1}} + S(3,2) k^{\underline{2}} + S(3,3) k^{\underline{3}} = k + 3k(k-1) + k(k-1)(k-2)

where S(3,1)=1S(3,1) = 1, S(3,2)=3S(3,2) = 3, S(3,3)=1S(3,3) = 1.

Binomial-Falling Factorial Identity

Theorem. For rnr \le n:

k=0n(nk)kr=nr2nr\sum_{k=0}^{n} \binom{n}{k} k^{\underline{r}} = n^{\underline{r}} \cdot 2^{n-r}

Proof. Using the absorption identity (nk)kr=nr(nrkr)\binom{n}{k} k^{\underline{r}} = n^{\underline{r}} \binom{n-r}{k-r}:

k=0n(nk)kr=nrk=rn(nrkr)=nr2nr\sum_{k=0}^{n} \binom{n}{k} k^{\underline{r}} = n^{\underline{r}} \sum_{k=r}^{n} \binom{n-r}{k-r} = n^{\underline{r}} \cdot 2^{n-r} \qquad \square

Combining the Results

k=0n(nk)k3=r=13S(3,r)nr2nr\sum_{k=0}^{n} \binom{n}{k} k^3 = \sum_{r=1}^{3} S(3,r) \cdot n^{\underline{r}} \cdot 2^{n-r} =1n2n1+3n(n1)2n2+1n(n1)(n2)2n3= 1 \cdot n \cdot 2^{n-1} + 3 \cdot n(n-1) \cdot 2^{n-2} + 1 \cdot n(n-1)(n-2) \cdot 2^{n-3}

For n=106n = 10^6, all arithmetic is modulo p=109+7p = 10^9 + 7.

Concrete Examples

nn(nk)k3\sum \binom{n}{k} k^3Formula value
1111=11 \cdot 1 = 1
21022+321=102 \cdot 2 + 3 \cdot 2 \cdot 1 = 10
36634+922+61=12+36+6=543 \cdot 4 + 9 \cdot 2 \cdot 2 + 6 \cdot 1 = 12 + 36 + 6 = 54

Rechecking: n=2n=2: (20)0+(21)1+(22)8=0+2+8=10\binom{2}{0}\cdot 0 + \binom{2}{1}\cdot 1 + \binom{2}{2}\cdot 8 = 0 + 2 + 8 = 10. Formula: 22+61+0=102\cdot 2 + 6\cdot 1 + 0 = 10. Correct.

Derivation

Editorial

Compute sum_{k=0}^{n} C(n,k) * k^3 mod (10^9 + 7), n = 10^6. Using Stirling numbers: k^3 = k + 3k(k-1) + k(k-1)(k-2) sum C(n,k)k^{r_down} = n^{r_down} * 2^{n-r} Result: n2^{n-1} + 3n(n-1)2^{n-2} + n(n-1)*(n-2)*2^{n-3} mod p Complexity: O(log n) for modular exponentiation. We compute 2n1modp2^{n-1} \bmod p, 2n2modp2^{n-2} \bmod p, 2n3modp2^{n-3} \bmod p via modular exponentiation. Finally, compute S=n2n1+3n(n1)2n2+n(n1)(n2)2n3(modp)S = n \cdot 2^{n-1} + 3n(n-1) \cdot 2^{n-2} + n(n-1)(n-2) \cdot 2^{n-3} \pmod{p}.

Pseudocode

Compute $n = 10^6$, $p = 10^9 + 7$
Compute $2^{n-1} \bmod p$, $2^{n-2} \bmod p$, $2^{n-3} \bmod p$ via modular exponentiation
Compute $S = n \cdot 2^{n-1} + 3n(n-1) \cdot 2^{n-2} + n(n-1)(n-2) \cdot 2^{n-3} \pmod{p}$

Generalization to Higher Powers

The same technique works for any mm: k=0n(nk)km=r=1mS(m,r)nr2nr\sum_{k=0}^{n} \binom{n}{k} k^m = \sum_{r=1}^{m} S(m,r) \cdot n^{\underline{r}} \cdot 2^{n-r}. The Stirling numbers S(m,r)S(m, r) can be precomputed using the recurrence S(m,r)=rS(m1,r)+S(m1,r1)S(m, r) = r \cdot S(m-1, r) + S(m-1, r-1).

Proof of Correctness

Theorem (Stirling Expansion Correctness). For all non-negative integers kk and mm:

km=r=0mS(m,r)krk^m = \sum_{r=0}^{m} S(m, r) \cdot k^{\underline{r}}

Proof. Both sides are polynomials of degree mm in kk. The falling factorials {k0,k1,,km}\{k^{\underline{0}}, k^{\underline{1}}, \ldots, k^{\underline{m}}\} form a basis for the space of polynomials of degree m\le m. The Stirling numbers S(m,r)S(m, r) are the unique coefficients expressing kmk^m in this basis. \square

Theorem (Absorption Identity). (nk)kr=nr(nrkr)\binom{n}{k} k^{\underline{r}} = n^{\underline{r}} \binom{n-r}{k-r}.

Proof. (nk)kr=n!(nk)!(kr)!1(nr)!/(nr)!\binom{n}{k} k^{\underline{r}} = \frac{n!}{(n-k)! \cdot (k-r)!} \cdot \frac{1}{(n-r)!/(n-r)!} … More cleanly: both sides equal n!(kr)!(nk)!=nr(nr)!(kr)!(nk)!=nr(nrkr)\frac{n!}{(k-r)! \cdot (n-k)!} = n^{\underline{r}} \cdot \frac{(n-r)!}{(k-r)! \cdot (n-k)!} = n^{\underline{r}} \binom{n-r}{k-r}. \square

All modular arithmetic is exact since p=109+7p = 10^9 + 7 is prime and all intermediate values are well-defined modulo pp.

Complexity Analysis

  • Time: O(logn)O(\log n) for modular exponentiation; O(1)O(1) for the formula evaluation.
  • Space: O(1)O(1).

Answer

243559751\boxed{243559751}

Code

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

C++ project_euler/problem_960/solution.cpp
/*
 * Problem 960: Modular Combinatorics
 *
 * Compute sum_{k=0}^{n} C(n,k)*k^3 mod (10^9+7), n = 10^6.
 *
 * Using Stirling numbers: k^3 = k + 3k(k-1) + k(k-1)(k-2)
 * sum C(n,k)*k^{r_down} = n^{r_down} * 2^{n-r}
 *
 * Formula: n*2^{n-1} + 3*n*(n-1)*2^{n-2} + n*(n-1)*(n-2)*2^{n-3} mod p
 *
 * Complexity: O(log n) for modular exponentiation.
 */

#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1e9 + 7;

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

int main() {
    long long n = 1000000;
    long long p = MOD;

    long long pow2_n1 = power(2, n - 1, p);
    long long pow2_n2 = power(2, n - 2, p);
    long long pow2_n3 = power(2, n - 3, p);

    long long nn = n % p;
    long long nn1 = (n - 1) % p;
    long long nn2 = (n - 2) % p;

    long long term1 = nn * pow2_n1 % p;
    long long term2 = 3 * nn % p * nn1 % p * pow2_n2 % p;
    long long term3 = nn * nn1 % p * nn2 % p * pow2_n3 % p;

    long long ans = (term1 + term2 + term3) % p;
    cout << ans << endl;
    return 0;
}