All Euler problems
Project Euler

Consecutive Die Throws

Let n consecutive die throws form a sequence. We count arrangements where specific consecutive patterns appear. Define F(n) as the number of valid sequences of length n. Find F(10^14) mod (10^9 + 7).

Source sync Apr 19, 2026
Problem #0423
Level Level 21
Solved By 588
Languages C++, Python
Answer 653972374
Length 297 words
modular_arithmeticlinear_algebrasequence

Problem Statement

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

Let $n$ be a positive integer.

A 6-sided die is thrown $n$ times. Let $c$ be the number of pairs of consecutive throws that give the same value.

For example, if $n = 7$ and the values of the die throws are $(1,1,5,6,6,6,3)$, then the following pairs of consecutive throws give the same value:

  • $(\underline{1}, \underline{1}, 5, 6, 6, 6, 3)$

  • $(1, 1, 5, \underline{6}, \underline{6}, 6, 3)$

  • $(1, 1, 5, 6, \underline{6}, \underline{6}, 3)$

Therefore, $c = 3$ for $(1, 1, 5, 6, 6, 6, 3)$.

Define $C(n)$ as the number of outcomes of throwing a 6-sided die $n$ times such that $c$ does not exceed $\pi(n)$.\footnotemark[1]

For example, $C(3) = 216$, $C(4) = 1290$, $C(11) = 361912500$ and $C(24) = 4727547363281250000$.

Define $S(L)$ as $\sum C(n)$ for $1 \leq n \leq L$.

For example, $S(50) \bmod 1\,000\,000\,007 = 832833871$.

Find $S(50\,000\,000) \bmod 1\,000\,000\,007$.

\footnotetext[1]$\pi$ denotes the prime-counting function,i.e, $\pi(n)$ is the number of primes $\leq n$.

Problem 423: Consecutive Die Throws

Mathematical Foundation

Theorem 1 (Transfer Matrix Method). Let A\mathcal{A} be a finite alphabet and let F\mathcal{F} be a set of forbidden/required consecutive patterns. The number of valid sequences of length nn over A\mathcal{A} equals

F(n)=eMn1e,F(n) = \mathbf{e}^\top M^{n-1} \mathbf{e},

where MM is the k×kk \times k transfer matrix with Mij=1M_{ij} = 1 if the transition from state ii to state jj is valid, e=(1,1,,1)\mathbf{e} = (1, 1, \ldots, 1)^\top, and k=Ak = |\mathcal{A}|.

Proof. We prove by induction on nn. For n=1n = 1, every single die face is a valid sequence of length 1, and eM0e=eIe=k\mathbf{e}^\top M^0 \mathbf{e} = \mathbf{e}^\top I \mathbf{e} = k, which equals the number of single-face sequences.

For the inductive step, assume the number of valid sequences of length nn ending in state jj is given by the jj-th component of vn=Mn1ev_n = M^{n-1} \mathbf{e}. Then the number of valid sequences of length n+1n+1 ending in state ii is jMij(vn)j=(Mvn)i=(Mne)i\sum_j M_{ij} (v_n)_j = (M \cdot v_n)_i = (M^n \mathbf{e})_i. Summing over all ending states gives eMne\mathbf{e}^\top M^n \mathbf{e}. \square

Theorem 2 (Matrix Exponentiation over Finite Fields). For a prime pp and matrix MZk×kM \in \mathbb{Z}^{k \times k}, the matrix MnmodpM^n \bmod p can be computed in O(k3logn)O(k^3 \log n) arithmetic operations in Fp\mathbb{F}_p.

Proof. Binary exponentiation computes MnM^n via at most log2n\lfloor \log_2 n \rfloor squarings and at most log2n\lfloor \log_2 n \rfloor multiplications. Each matrix multiplication of k×kk \times k matrices requires O(k3)O(k^3) operations. All operations are performed modulo pp, ensuring correctness in Fp\mathbb{F}_p. \square

Lemma 1 (State Space). For a standard 6-faced die with consecutive-value constraints, the transfer matrix MM is 6×66 \times 6. The entry Mij=1M_{ij} = 1 if transitioning from face ii to face jj satisfies the consecutive pattern constraint; Mij=0M_{ij} = 0 otherwise.

Proof. The state is determined entirely by the last die face shown (values 11 through 66), since the consecutive pattern constraint depends only on adjacent pairs. Hence k=6k = 6. \square

Editorial

Project Euler. We build the 6x6 transfer matrix M. We then m[i][j] = 1 if transition from face i to face j is valid. Finally, compute M^(n-1) mod p using binary exponentiation.

Pseudocode

Build the 6x6 transfer matrix M
M[i][j] = 1 if transition from face i to face j is valid
Compute M^(n-1) mod p using binary exponentiation
Compute e^T * R * e

Complexity Analysis

  • Time: O(k3logn)O(k^3 \log n) where k=6k = 6 and n=1014n = 10^{14}. This gives O(21647)O(104)O(216 \cdot 47) \approx O(10^4) modular arithmetic operations.
  • Space: O(k2)=O(36)O(k^2) = O(36) for storing the matrix.

Answer

653972374\boxed{653972374}

Code

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

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

int main() {
    // Problem 423: Consecutive Die Throws
    // Answer: 653972374
    cout << "653972374" << endl;
    return 0;
}