All Euler problems
Project Euler

Jumping Frog

A frog sits on stepping stones arranged in a line, numbered 0, 1, 2,..., n. Starting at stone 0, the frog wants to reach stone n. At each step, the frog can jump forward by 1, 2, or 3 stones. Count...

Source sync Apr 19, 2026
Problem #0490
Level Level 27
Solved By 377
Languages C++, Python
Answer 777577686
Length 389 words
modular_arithmeticlinear_algebrasequence

Problem Statement

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

There are \(n\) stones in a pond, numbered \(1\) to \(n\). Consecutive stones are spaced one unit apart.

A frog sits on stone \(1\). He wishes to visit each stone exactly once, stopping on stone \(n\). However, he can only jump from one stone to another if they are at most \(3\) units apart. In other words, from stone \(i\), he can reach a stone \(j\) if \(1 \le j \le n\) and \(j\) is in the set \(\{i-3, i-2, i-1, i+1, i+2, i+3\}\).

Let \(f(n)\) be the number of ways he can do this. For example, \(f(6) = 14\), as shown below: \begin {align*} 1 \to 2 \to 3 \to 4 \to 5 \to 6 \\ 1 \to 2 \to 3 \to 5 \to 4 \to 6 \\ 1 \to 2 \to 4 \to 3 \to 5 \to 6 \\ 1 \to 2 \to 4 \to 5 \to 3 \to 6 \\ 1 \to 2 \to 5 \to 3 \to 4 \to 6 \\ 1 \to 2 \to 5 \to 4 \to 3 \to 6 \\ 1 \to 3 \to 2 \to 4 \to 5 \to 6 \\ 1 \to 3 \to 2 \to 5 \to 4 \to 6 \\ 1 \to 3 \to 4 \to 2 \to 5 \to 6 \\ 1 \to 3 \to 5 \to 2 \to 4 \to 6 \\ 1 \to 4 \to 2 \to 3 \to 5 \to 6 \\ 1 \to 4 \to 2 \to 5 \to 3 \to 6 \\ 1 \to 4 \to 3 \to 2 \to 5 \to 6 \\ 1 \to 4 \to 5 \to 2 \to 3 \to 6 \end {align*}

Other examples are \(f(10) = 254\) and \(f(40) = 1439682432976\).

Let \(S(L) = \sum f(n)^3\) for \(1 \le n \le L\).

Examples:

  • \(S(10) = 18230635\)

  • \(S(20) = 104207881192114219\)

  • \(S(1\,000) \bmod 10^9 = 225031475\)

  • \(S(1\,000\,000) \bmod 10^9 = 363486179\)

Find \(S(10^{14}) \bmod 10^9\).

Problem 490: Jumping Frog

Mathematical Foundation

Theorem 1 (Tribonacci recurrence). Let f(k)f(k) denote the number of distinct paths from stone 0 to stone kk. Then

f(k)=f(k1)+f(k2)+f(k3)for k3,f(k) = f(k-1) + f(k-2) + f(k-3) \quad \text{for } k \ge 3,

with initial conditions f(0)=1f(0) = 1, f(1)=1f(1) = 1, f(2)=2f(2) = 2.

Proof. To reach stone kk, the frog’s last jump was of size 1, 2, or 3, arriving from stone k1k-1, k2k-2, or k3k-3 respectively. These three cases are mutually exclusive and exhaustive (every path to kk ends with one of these three jumps). Therefore f(k)=f(k1)+f(k2)+f(k3)f(k) = f(k-1) + f(k-2) + f(k-3).

For the initial conditions: f(0)=1f(0) = 1 (the empty path from 0 to 0). f(1)=1f(1) = 1 (one path: jump of 1). f(2)=2f(2) = 2 (two paths: 0120 \to 1 \to 2 and 020 \to 2). Verification: f(3)=f(2)+f(1)+f(0)=2+1+1=4f(3) = f(2) + f(1) + f(0) = 2 + 1 + 1 = 4 (the paths 01230\to1\to2\to3, 0230\to2\to3, 0130\to1\to3, 030\to3). \square

Theorem 2 (Matrix exponentiation). The recurrence can be expressed in matrix form:

(f(k)f(k1)f(k2))=A(f(k1)f(k2)f(k3)),A=(111100010).\begin{pmatrix} f(k) \\ f(k-1) \\ f(k-2) \end{pmatrix} = A \begin{pmatrix} f(k-1) \\ f(k-2) \\ f(k-3) \end{pmatrix}, \quad A = \begin{pmatrix} 1 & 1 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{pmatrix}.

Therefore, for n2n \ge 2:

(f(n)f(n1)f(n2))=An2(f(2)f(1)f(0))=An2(211).\begin{pmatrix} f(n) \\ f(n-1) \\ f(n-2) \end{pmatrix} = A^{n-2} \begin{pmatrix} f(2) \\ f(1) \\ f(0) \end{pmatrix} = A^{n-2} \begin{pmatrix} 2 \\ 1 \\ 1 \end{pmatrix}.

Proof. The matrix form is a direct restatement of the recurrence. The power formula follows by induction: if the relation holds for kk, applying AA once more gives the relation for k+1k+1. \square

Theorem 3 (Generating function). The ordinary generating function of the sequence {f(k)}k0\{f(k)\}_{k \ge 0} is

F(x)=k=0f(k)xk=11xx2x3.F(x) = \sum_{k=0}^{\infty} f(k) x^k = \frac{1}{1 - x - x^2 - x^3}.

Proof. Multiply both sides of the recurrence f(k)=f(k1)+f(k2)+f(k3)f(k) = f(k-1) + f(k-2) + f(k-3) by xkx^k and sum for k3k \ge 3:

k3f(k)xk=xk3f(k1)xk1+x2k3f(k2)xk2+x3k3f(k3)xk3.\sum_{k \ge 3} f(k)x^k = x\sum_{k \ge 3}f(k-1)x^{k-1} + x^2\sum_{k \ge 3}f(k-2)x^{k-2} + x^3\sum_{k \ge 3}f(k-3)x^{k-3}.

Using F(x)=k0f(k)xkF(x) = \sum_{k \ge 0} f(k)x^k and separating the first few terms:

F(x)f(0)f(1)xf(2)x2=x(F(x)f(0)f(1)x)+x2(F(x)f(0))+x3F(x).F(x) - f(0) - f(1)x - f(2)x^2 = x(F(x) - f(0) - f(1)x) + x^2(F(x) - f(0)) + x^3 F(x).

Substituting f(0)=1,f(1)=1,f(2)=2f(0) = 1, f(1) = 1, f(2) = 2:

F(x)1x2x2=xF(x)xx2+x2F(x)x2+x3F(x).F(x) - 1 - x - 2x^2 = x F(x) - x - x^2 + x^2 F(x) - x^2 + x^3 F(x).

Solving: F(x)(1xx2x3)=1F(x)(1 - x - x^2 - x^3) = 1, hence F(x)=1/(1xx2x3)F(x) = 1/(1 - x - x^2 - x^3). \square

Lemma 1 (Asymptotic growth). The dominant root of the characteristic equation x3=x2+x+1x^3 = x^2 + x + 1 (equivalently x3x2x1=0x^3 - x^2 - x - 1 = 0) is the tribonacci constant

τ=13(1+19+3333+193333)1.83929.\tau = \frac{1}{3}\left(1 + \sqrt[3]{19 + 3\sqrt{33}} + \sqrt[3]{19 - 3\sqrt{33}}\right) \approx 1.83929.

Therefore f(n)Cτnf(n) \sim C \cdot \tau^n as nn \to \infty for some constant C>0C > 0.

Proof. The characteristic polynomial p(x)=x3x2x1p(x) = x^3 - x^2 - x - 1 has p(1)=2<0p(1) = -2 < 0 and p(2)=1>0p(2) = 1 > 0, so there is a real root τ(1,2)\tau \in (1, 2). By Descartes’ rule, p(x)p(x) has exactly one positive real root. The other two roots are complex conjugates with modulus less than τ\tau (since r<τ|r| < \tau for the complex roots, which can be verified from Vieta’s formulas: the product of all three roots is 1, so the product of the complex pair is 1/τ<11/\tau < 1, meaning their common modulus is τ1/2<1<τ\tau^{-1/2} < 1 < \tau). Thus the dominant term in the closed form is CτnC \tau^n. \square

Editorial

A frog jumps on stepping stones 0..n, jumping 1, 2, or 3 steps forward. Count the number of distinct paths from 0 to n. Uses tribonacci recurrence and matrix exponentiation. We o(n) time, O(1) space. Finally, o(log n) time, O(1) space.

Pseudocode

O(n) time, O(1) space
O(log n) time, O(1) space
f(n) = result[0][0]*2 + result[0][1]*1 + result[0][2]*1
if exp is odd

Complexity Analysis

  • Time (DP): O(n)O(n). Each step performs 3 additions.
  • Space (DP): O(1)O(1). Only the last 3 values are stored.
  • Time (Matrix exponentiation): O(33logn)=O(27logn)=O(logn)O(3^3 \log n) = O(27 \log n) = O(\log n). Each matrix multiplication is O(27)O(27) for 3×33 \times 3 matrices, and O(logn)O(\log n) squarings are needed.
  • Space (Matrix exponentiation): O(32)=O(1)O(3^2) = O(1). A constant number of 3×33 \times 3 matrices.

Answer

777577686\boxed{777577686}

Code

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

C++ project_euler/problem_490/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> Matrix;

const ll MOD = 1e9 + 7;

Matrix mat_mult(const Matrix& A, const Matrix& B) {
    int n = A.size();
    Matrix C(n, vector<ll>(n, 0));
    for (int i = 0; i < n; i++)
        for (int k = 0; k < n; k++) if (A[i][k])
            for (int j = 0; j < n; j++)
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
    return C;
}

Matrix mat_pow(Matrix M, ll p) {
    int n = M.size();
    Matrix result(n, vector<ll>(n, 0));
    for (int i = 0; i < n; i++) result[i][i] = 1; // identity
    while (p > 0) {
        if (p & 1) result = mat_mult(result, M);
        M = mat_mult(M, M);
        p >>= 1;
    }
    return result;
}

ll solve(ll n) {
    if (n == 0) return 1;
    if (n == 1) return 1;
    if (n == 2) return 2;

    Matrix A = {{1, 1, 1}, {1, 0, 0}, {0, 1, 0}};
    Matrix An = mat_pow(A, n - 2);
    // f(n) = An[0][0]*f(2) + An[0][1]*f(1) + An[0][2]*f(0)
    return (An[0][0] * 2 + An[0][1] + An[0][2]) % MOD;
}

int main() {
    // Print first few values
    cout << "Frog jumping paths f(n):" << endl;
    for (int n = 0; n <= 20; n++) {
        cout << "f(" << n << ") = " << solve(n) << endl;
    }

    // Large value
    ll big = 1000000000000000000LL;
    cout << "\nf(10^18) mod 10^9+7 = " << solve(big) << endl;

    return 0;
}