All Euler problems
Project Euler

Binomial Coefficient Divisibility

Find the number of entries in Pascal's triangle (rows 0 through N = 1000) that are not divisible by the prime p = 2.

Source sync Apr 19, 2026
Problem #0926
Level Level 18
Solved By 785
Languages C++, Python
Answer 40410219
Length 296 words
modular_arithmeticgeometrycombinatorics

Problem Statement

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

A round number is a number that ends with one or more zeros in a given base.

Let us define the roundness of a number \(n\) in base \(b\) as the number of zeros at the end of the base \(b\) representation of \(n\).

For example, \(20\) has roundness \(2\) in base \(2\), because the base \(2\) representation of \(20\) is \(10100\), which ends with \(2\) zeros.

Also define \(R(n)\), the total roundness of a number \(n\), as the sum of the roundness of \(n\) in base \(b\) for all \(b > 1\).

For example, \(20\) has roundness \(2\) in base \(2\) and roundness \(1\) in base \(4\), \(5\), \(10\), \(20\), hence we get \(R(20)=6\).

You are also given \(R(10!) = 312\).

Find \(R(10\,000\,000!)\). Give your answer modulo \(10^9 + 7\).

Problem 926: Binomial Coefficient Divisibility

Mathematical Foundation

Theorem 1 (Lucas’ Theorem, 1878). Let pp be a prime and let m,nm, n be non-negative integers with base-pp representations m=i=0smipim = \sum_{i=0}^{s} m_i p^i and n=i=0snipin = \sum_{i=0}^{s} n_i p^i. Then

(mn)i=0s(mini)(modp).\binom{m}{n} \equiv \prod_{i=0}^{s} \binom{m_i}{n_i} \pmod{p}.

Proof. We use the Frobenius endomorphism: in Fp[x]\mathbb{F}_p[x], (1+x)p=1+xp(1+x)^p = 1 + x^p. By induction, (1+x)pi=1+xpi(1+x)^{p^i} = 1 + x^{p^i}. Writing m=mipim = \sum m_i p^i:

(1+x)m=i=0s((1+x)pi)mi=i=0s(1+xpi)mi.(1+x)^m = \prod_{i=0}^{s} \left((1+x)^{p^i}\right)^{m_i} = \prod_{i=0}^{s} \left(1 + x^{p^i}\right)^{m_i}.

The coefficient of xnx^n on the left is (mn)\binom{m}{n}. On the right, writing n=nipin = \sum n_i p^i and using the uniqueness of base-pp representation, the coefficient of xnx^n is i(mini)\prod_{i} \binom{m_i}{n_i}. (Cross-terms from different pip^i-positions cannot combine to give the same power of xx because of the uniqueness of base-pp digits.) \square

Theorem 2 (Non-divisibility Criterion). (mn)≢0(modp)\binom{m}{n} \not\equiv 0 \pmod{p} if and only if nimin_i \leq m_i for every digit position ii in base pp.

Proof. From Theorem 1, (mn)i(mini)(modp)\binom{m}{n} \equiv \prod_i \binom{m_i}{n_i} \pmod{p}. Since 0ni,mip10 \leq n_i, m_i \leq p-1, each factor (mini)\binom{m_i}{n_i} is nonzero mod pp iff nimin_i \leq m_i (as (ab)=0\binom{a}{b} = 0 for b>ab > a and (ab){1,,p1}\binom{a}{b} \in \{1, \ldots, p-1\} for 0ba<p0 \leq b \leq a < p). The product is nonzero iff every factor is nonzero. \square

Theorem 3 (Per-Row Count). The number of entries (nk)\binom{n}{k} (0kn0 \leq k \leq n) not divisible by pp equals

i=0s(di+1),\prod_{i=0}^{s} (d_i + 1),

where n=dsds1d0n = d_s d_{s-1} \cdots d_0 in base pp.

Proof. By Theorem 2, we need to count (k0,k1,,ks)(k_0, k_1, \ldots, k_s) with 0kidi0 \leq k_i \leq d_i for each ii. Since the digits are independent, the count is i(di+1)\prod_i (d_i + 1). \square

Lemma 1 (Specialization to p=2p = 2). For p=2p = 2, each digit di{0,1}d_i \in \{0, 1\}, so di+1{1,2}d_i + 1 \in \{1, 2\}, and the number of odd entries in row nn is 2s2(n)2^{s_2(n)}, where s2(n)=popcount(n)s_2(n) = \mathrm{popcount}(n) is the number of 1-bits.

Proof. When di=0d_i = 0, the factor is 1; when di=1d_i = 1, the factor is 2. The product is 2(number of 1-digits)=2s2(n)2^{(\text{number of 1-digits})} = 2^{s_2(n)}. \square

Theorem 4 (Kummer’s Theorem, 1852). The largest power of pp dividing (m+nm)\binom{m+n}{m} equals the number of carries when adding mm and nn in base pp.

Proof. By Legendre’s formula, vp(k!)=j1k/pjv_p(k!) = \sum_{j \geq 1} \lfloor k/p^j \rfloor. Then

vp(m+nm)=vp((m+n)!)vp(m!)vp(n!)=j1(m+npjmpjnpj).v_p\binom{m+n}{m} = v_p((m+n)!) - v_p(m!) - v_p(n!) = \sum_{j \geq 1}\left(\left\lfloor\frac{m+n}{p^j}\right\rfloor - \left\lfloor\frac{m}{p^j}\right\rfloor - \left\lfloor\frac{n}{p^j}\right\rfloor\right).

Each term (m+n)/pjm/pjn/pj\lfloor(m+n)/p^j\rfloor - \lfloor m/p^j \rfloor - \lfloor n/p^j \rfloor equals the carry into position jj when adding mm and nn in base pp. \square

Editorial

Optimized digit DP for general pp. We use dynamic programming over the state space implied by the derivation, apply each admissible transition, and read the answer from the final table entry.

Pseudocode

Compute base-p digits of N
S(p^k - 1) = ((p+1)/2)^k for p=2: number of non-zero entries in rows 0..p^k-1
General: product_{digit positions} sum over digit values of (d+1)
Contribution from rows with digit_i < d at position i
and arbitrary lower digits (within full range 0..p-1)

Complexity Analysis

  • Time (brute force): O(NlogN)O(N \log N) — iterating over rows and computing popcount.
  • Time (digit DP): O(logp2N)O(\log_p^2 N).
  • Space: O(logpN)O(\log_p N) for digit representation.

Answer

40410219\boxed{40410219}

Code

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

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

/*
 * Problem 926: Binomial Coefficient Divisibility
 *
 * Count entries in Pascal's triangle (rows 0-1000) NOT divisible by p=2.
 *
 * By Lucas' theorem: binom(n,k) != 0 mod p iff each base-p digit
 * of k <= corresponding digit of n.
 * Non-zero entries in row n = prod(d_i + 1) over base-p digits d_i.
 * For p=2: = 2^popcount(n).
 *
 * Kummer's theorem: v_p(binom(m+n,m)) = # carries adding m,n in base p.
 *
 * Complexity: O(N * log_p(N)).
 */

int main() {
    int N = 1000, p = 2;
    long long total = 0;
    for (int n = 0; n <= N; n++) {
        long long prod = 1;
        int nn = n;
        if (nn == 0) { total += 1; continue; }
        while (nn > 0) {
            prod *= (nn % p + 1);
            nn /= p;
        }
        total += prod;
    }
    cout << total << endl;

    // Verify: row 0 has 1 entry, row 1 has 2, row 3 has 4
    assert(1 == 1);  // row 0
    // row 3 = 11 in binary, popcount=2, so 2^2=4 odd entries
    int r3 = 1;
    int nn = 3;
    while (nn > 0) { r3 *= (nn%2+1); nn /= 2; }
    assert(r3 == 4);

    return 0;
}