All Euler problems
Project Euler

Exploring Pascal's Triangle

How many entries C(200000, i,j,k) = (200000!)/(i! j! k!) with i + j + k = 200000 are divisible by 10^12?

Source sync Apr 19, 2026
Problem #0154
Level Level 08
Solved By 3,106
Languages C++, Python
Answer 479742450
Length 246 words
modular_arithmeticnumber_theorygeometry

Problem Statement

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

A triangular pyramid is constructed using spherical balls so that each ball rests on exactly three balls of the next lower level.

PIC

Then, we calculate the number of paths leading from the apex to each position:

A path starts at the apex and progresses downwards to any of the three spheres directly below the current position.

Consequently, the number of paths to reach a certain position is the sum of the numbers immediately above it (depending on the position, there are up to three numbers above it).

The result is Pascal’s pyramid and the numbers at each level \(n\) are the coefficients of the trinomial expansion \((x + y + z)^n\).

How many coefficients in the expansion of \((x + y + z)^{200000}\) are multiples of \(10^{12}\)?

Problem 154: Exploring Pascal’s Triangle

Mathematical Foundation

Theorem 1 (Legendre’s Formula). For a prime pp and non-negative integer nn:

vp(n!)=m=1npmv_p(n!) = \sum_{m=1}^{\infty} \left\lfloor \frac{n}{p^m} \right\rfloor

where vpv_p denotes the pp-adic valuation.

Proof. Each integer k{1,,n}k \in \{1, \ldots, n\} contributes vp(k)v_p(k) to vp(n!)v_p(n!). We have vp(k)=m=11[pmk]v_p(k) = \sum_{m=1}^{\infty} \mathbb{1}[p^m \mid k]. Swapping summation: vp(n!)=m=1{kn:pmk}=m=1n/pmv_p(n!) = \sum_{m=1}^{\infty} |\{k \le n : p^m \mid k\}| = \sum_{m=1}^{\infty} \lfloor n/p^m \rfloor. \square

Theorem 2 (Trinomial Valuation). For the trinomial coefficient (ni,j,k)\binom{n}{i,j,k} with i+j+k=ni + j + k = n:

vp ⁣((ni,j,k))=vp(n!)vp(i!)vp(j!)vp(k!)v_p\!\left(\binom{n}{i,j,k}\right) = v_p(n!) - v_p(i!) - v_p(j!) - v_p(k!)

This equals the total number of carries when adding ii, jj, kk in base pp (Kummer’s theorem generalization).

Proof. Direct from (ni,j,k)=n!/(i!j!k!)\binom{n}{i,j,k} = n! / (i!\,j!\,k!) and the additive property of vpv_p. \square

Lemma 1 (Divisibility Condition). 1012(ni,j,k)10^{12} \mid \binom{n}{i,j,k} if and only if v2 ⁣((ni,j,k))12v_2\!\left(\binom{n}{i,j,k}\right) \ge 12 and v5 ⁣((ni,j,k))12v_5\!\left(\binom{n}{i,j,k}\right) \ge 12.

Proof. Since 1012=21251210^{12} = 2^{12} \cdot 5^{12} and gcd(212,512)=1\gcd(2^{12}, 5^{12}) = 1, the divisibility holds iff both prime-power conditions hold. \square

Lemma 2 (Symmetry Reduction). The number of ordered triples (i,j,k)(i,j,k) with i+j+k=ni + j + k = n satisfying the divisibility condition can be computed by iterating over ijki \le j \le k and applying symmetry multipliers:

  • 66 if i<j<ki < j < k (all distinct),
  • 33 if exactly two of i,j,ki, j, k are equal,
  • 11 if i=j=ki = j = k.

Proof. The trinomial coefficient is symmetric in (i,j,k)(i,j,k). The multinomial coefficient for the number of distinct permutations of (i,j,k)(i,j,k) gives the multiplier. \square

Editorial

the trinomial coefficient IS divisible by 10^12. v_p(C(n; i,j,k)) = v_p(n!) - v_p(i!) - v_p(j!) - v_p(k!) We need v_2 >= 12 AND v_5 >= 12. Note: This Python solution is slow for N=200000 (O(N^2) iterations). For the actual answer, use the C++ version. This serves as a reference. We precompute p-adic valuations of factorials. We then precompute target valuations. Finally, check v_2 condition.

Pseudocode

Precompute p-adic valuations of factorials
Precompute target valuations
Check v_2 condition
Check v_5 condition
Count with symmetry
else

Complexity Analysis

  • Time: O(n)O(n) for precomputation of f2f_2 and f5f_5. The main loop iterates over O(n2/6)O(n^2 / 6) triples, each in O(1)O(1). Total: O(n2)O(n^2) where n=200000n = 200000, giving approximately 6.67×1096.67 \times 10^9 operations. (In practice, the inner loop often terminates early.)
  • Space: O(n)O(n) for the precomputed valuation arrays.

Answer

479742450\boxed{479742450}

Code

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

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

/*
 * Problem 154: Count triples (i,j,k) with i+j+k=200000 where
 * the trinomial coefficient IS divisible by 10^12.
 *
 * v_p(binom(n; i,j,k)) = v_p(n!) - v_p(i!) - v_p(j!) - v_p(k!)
 * Need v_2 >= 12 AND v_5 >= 12.
 */

int main() {
    const int N = 200000;
    const int TARGET = 12;

    // Precompute v_p(m!) for p = 2 and p = 5
    vector<int> v2(N + 1), v5(N + 1);
    v2[0] = v5[0] = 0;
    for (int m = 1; m <= N; m++) {
        int x = m, c2 = 0, c5 = 0;
        while (x % 2 == 0) { c2++; x /= 2; }
        x = m;
        while (x % 5 == 0) { c5++; x /= 5; }
        v2[m] = v2[m - 1] + c2;
        v5[m] = v5[m - 1] + c5;
    }

    int V2N = v2[N];
    int V5N = v5[N];

    long long count = 0;

    // Iterate i <= j <= k, i+j+k = N
    // So i <= N/3, j >= i, k = N-i-j >= j => j <= (N-i)/2
    for (int i = 0; i <= N / 3; i++) {
        // For v_p: need V_pN - v_p[i] - v_p[j] - v_p[N-i-j] < TARGET
        int r2i = V2N - v2[i]; // v2 of binom without j,k parts
        int r5i = V5N - v5[i];

        for (int j = i; j <= (N - i) / 2; j++) {
            int k = N - i - j;
            int val2 = r2i - v2[j] - v2[k];
            int val5 = r5i - v5[j] - v5[k];

            if (val2 >= TARGET && val5 >= TARGET) {
                // Count with multiplicity
                if (i == j && j == k) count += 1;
                else if (i == j || j == k) count += 3;
                else count += 6;
            }
        }
    }

    printf("%lld\n", count);
    return 0;
}