All Euler problems
Project Euler

Bounded Sequences

Let t(n) denote the number of sequences (x_1,..., x_n) of positive integers such that there exists alpha in [2, 3) with x_i = floor(alpha^i) for all i. Given t(10) = 86195 and t(20) = 5227991891, f...

Source sync Apr 19, 2026
Problem #0319
Level Level 23
Solved By 491
Languages C++, Python
Answer 268457129
Length 369 words
modular_arithmeticlinear_algebradynamic_programming

Problem Statement

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

Let $x_1, x_2, \dots, x_n$ be a sequence of length $n$ such that:

  • $x_1 = 2$

  • for all $1 < i \le n$: $x_{i - 1} < x_i$

  • for all $i$ and $j$ with $1 \le i, j \le n$: $(x_i)^j < (x_j + 1)^i$.

There are only five such sequences of length $2$, namely: $\{2,4\}$, $\{2,5\}$, $\{2,6\}$, $\{2,7\}$ and $\{2,8\}$.

There are $293$ such sequences of length $5$; three examples are given below:

$\{2,5,11,25,55\}$, $\{2,6,14,36,88\}$, $\{2,8,22,64,181\}$.

Let $t(n)$ denote the number of such sequences of length $n$.

You are given that $t(10) = 86195$ and $t(20) = 5227991891$.

Find $t(10^{10})$ and give your answer modulo $10^9$.

Problem 319: Bounded Sequences

Mathematical Foundation

Definition. For real α2\alpha \ge 2, the sequence (α,α2,,αn)(\lfloor\alpha\rfloor, \lfloor\alpha^2\rfloor, \ldots, \lfloor\alpha^n\rfloor) is the α\alpha-floor sequence of length nn. Let t(n)t(n) count the distinct α\alpha-floor sequences for α[2,3)\alpha \in [2,3).

Lemma 1 (Counting Function gg). Define G(v,m)=j=1mvj=vvm1v1G(v, m) = \sum_{j=1}^{m} v^j = v \cdot \frac{v^m - 1}{v - 1} and g(m)=G(3,m)G(2,m)g(m) = G(3, m) - G(2, m). Then g(m)g(m) counts, with appropriate multiplicity, the total number of integer sequences of length mm consistent with some α[2,3)\alpha \in [2,3) (before applying Mobius inversion to account for primitivity).

Proof. For each position jj, the number of possible values of αj\lfloor\alpha^j\rfloor as α\alpha ranges over [2,3)[2,3) is bounded by 3j2j3^j - 2^j (the interval [2j,3j)[2^j, 3^j) contains that many integers). The function G(v,m)G(v,m) sums geometric series counting lattice points under power curves. The difference g(m)=G(3,m)G(2,m)g(m) = G(3,m) - G(2,m) aggregates these counts. \square

Theorem 1 (Mobius Inversion Formula). The count of distinct sequences is:

t(n)=k=1nμ(k)g ⁣(nk)t(n) = \sum_{k=1}^{n} \mu(k) \cdot g\!\left(\left\lfloor \frac{n}{k}\right\rfloor\right)

where μ\mu is the Mobius function.

Proof. A sequence of length nn arising from α[2,3)\alpha \in [2,3) may coincide with a sequence of length dnd | n extended periodically (in terms of the α\alpha-intervals). The inclusion-exclusion via Mobius inversion removes these overcounts:

t(n)=dnμ(n/d)g(d)=k=1nμ(k)g(n/k).t(n) = \sum_{d | n} \mu(n/d)\,g(d) = \sum_{k=1}^{n} \mu(k)\,g(\lfloor n/k \rfloor). \quad \square

Lemma 2 (Matrix Exponentiation for G(v,m)G(v,m)). G(v,m)modMG(v,m) \bmod M is computable in O(logm)O(\log m) via:

(v101)m=(vmvm1v101)\begin{pmatrix} v & 1 \\ 0 & 1 \end{pmatrix}^m = \begin{pmatrix} v^m & \frac{v^m - 1}{v - 1} \\ 0 & 1 \end{pmatrix}

so G(v,m)=v[matrixm]0,1G(v, m) = v \cdot [\text{matrix}^m]_{0,1}.

Proof. By induction on mm. The base case m=1m = 1 gives (v101)1=(v101)\begin{pmatrix} v & 1 \\ 0 & 1 \end{pmatrix}^1 = \begin{pmatrix} v & 1 \\ 0 & 1 \end{pmatrix}, and G(v,1)=v1=vG(v,1) = v \cdot 1 = v. For the inductive step, multiplying on the right by (v101)\begin{pmatrix} v & 1 \\ 0 & 1 \end{pmatrix}:

(vmvm1v101)(v101)=(vm+1vm+vm1v101)=(vm+1vm+11v101).\begin{pmatrix} v^m & \frac{v^m-1}{v-1} \\ 0 & 1 \end{pmatrix}\begin{pmatrix} v & 1 \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} v^{m+1} & v^m + \frac{v^m-1}{v-1} \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} v^{m+1} & \frac{v^{m+1}-1}{v-1} \\ 0 & 1 \end{pmatrix}.

The identity vm+vm1v1=vm+11v1v^m + \frac{v^m-1}{v-1} = \frac{v^{m+1}-1}{v-1} is verified algebraically. \square

Theorem 2 (Efficient Summation via Mertens Function). Since n/k\lfloor n/k \rfloor takes only O(n)O(\sqrt{n}) distinct values, group terms:

t(n)=distinct q=n/kg(q)(M(k2)M(k11))t(n) = \sum_{\text{distinct } q = \lfloor n/k \rfloor} g(q) \cdot \bigl(M(k_2) - M(k_1 - 1)\bigr)

where M(x)=i=1xμ(i)M(x) = \sum_{i=1}^{x} \mu(i) is the Mertens function and [k1,k2][k_1, k_2] is the range of kk with n/k=q\lfloor n/k \rfloor = q.

Proof. Regrouping the sum by distinct values of n/k\lfloor n/k \rfloor and using partial sums of μ\mu. \square

Lemma 3 (Mertens Function Computation). M(n)M(n) satisfies:

M(n)=1k=2nM ⁣(nk).M(n) = 1 - \sum_{k=2}^{n} M\!\left(\left\lfloor \frac{n}{k}\right\rfloor\right).

Sieve μ(k)\mu(k) for kn2/3k \le n^{2/3}; compute MM recursively with memoization for larger arguments. Total time: O(n2/3)O(n^{2/3}).

Proof. From dnμ(d)=[n=1]\sum_{d|n}\mu(d) = [n=1], summing over nn: k=1nM(n/k)=1\sum_{k=1}^{n} M(\lfloor n/k \rfloor) = 1. Isolating k=1k=1: M(n)=1k=2nM(n/k)M(n) = 1 - \sum_{k=2}^{n} M(\lfloor n/k \rfloor). The hyperbolic summation trick (grouping by distinct values of n/k\lfloor n/k \rfloor) combined with sieving up to n2/3n^{2/3} gives O(n2/3)O(n^{2/3}) complexity. \square

Verification. G(3,10)=3(3101)/2=88572G(3,10) = 3(3^{10}-1)/2 = 88572, G(2,10)=2(2101)=2046G(2,10) = 2(2^{10}-1) = 2046, g(10)=86526g(10) = 86526.

t(10)=k=110μ(k)g(10/k)=86195.t(10) = \sum_{k=1}^{10}\mu(k)\,g(\lfloor 10/k \rfloor) = 86195. \quad \checkmark

Editorial

t(n) = number of sequences (x_1, …, x_n) where x_i = floor(alpha^i) for some alpha in [2, 3). Formula: t(n) = sum_{k=1}^{n} mu(k) * g(floor(n/k)) where g(m) = G(3,m) - G(2,m), G(v,m) = v * (v^m - 1) / (v - 1) Uses Mobius function prefix sums (Mertens function) and matrix exponentiation. We sieve mu(k) for k <= n^(2/3), compute prefix sums M(k). We then iterate over large arguments, compute M(x) recursively with memoization. Finally, sum all contributions modulo MOD.

Pseudocode

Input: n = 10^10, MOD = 10^9
Output: t(n) mod MOD
Sieve mu(k) for k <= n^(2/3), compute prefix sums M(k)
For large arguments, compute M(x) recursively with memoization:
Enumerate distinct values q = floor(n/k):
Sum all contributions modulo MOD
Return result

Complexity Analysis

  • Time: O(n2/3)O(n^{2/3}) for Mertens function computation. O(nlogn)O(\sqrt{n} \cdot \log n) for matrix exponentiations over O(n)O(\sqrt{n}) distinct values. Total: O(n2/3)O(n^{2/3}).
  • Space: O(n2/3)O(n^{2/3}) for the sieve and memoization table.

Answer

268457129\boxed{268457129}

Code

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

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

/*
 * Problem 319: Bounded Sequences
 *
 * t(n) = sum_{k=1}^{n} mu(k) * g(floor(n/k))
 * where g(m) = G(3,m) - G(2,m), G(v,m) = v*(v^m - 1)/(v-1)
 *
 * Uses:
 * - Sieve for Mobius function prefix sums up to LMT
 * - Mertens function via recursive formula with memoization for large values
 * - Matrix exponentiation for G(v,m) mod MOD
 *
 * Answer: 268457129
 */

typedef long long i64;

const i64 N = 10000000000LL; // 10^10
const i64 MOD = 1000000000LL; // 10^9
const int LMT = 10000000;     // 10^7

i64 mu_prefix[LMT + 1]; // prefix sum of Mobius function
map<i64, i64> memo_mertens;

// Compute Mertens function M(n) = sum_{k=1}^{n} mu(k), modulo MOD
i64 mertens(i64 n) {
    if (n <= LMT) return mu_prefix[n];
    if (memo_mertens.count(n)) return memo_mertens[n];
    i64 ret = 1; // M(1) = mu(1) = 1
    for (i64 i = 2, j; i <= n; i = j) {
        j = n / (n / i) + 1;
        ret = (ret + MOD - (j - i) % MOD * mertens(n / i) % MOD) % MOD;
    }
    return memo_mertens[n] = ret;
}

// 2x2 matrix multiplication mod MOD
typedef i64 mat[2][2];

void matmul(mat a, mat b, mat c) {
    mat t;
    memset(t, 0, sizeof(mat));
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            for (int k = 0; k < 2; k++)
                t[i][k] = (t[i][k] + a[i][j] % MOD * (b[j][k] % MOD)) % MOD;
    memcpy(c, t, sizeof(mat));
}

// G(v, x) = v * (v^x - 1) / (v - 1) mod MOD, via matrix exponentiation
// [v 1; 0 1]^x has entry [0][1] = (v^x - 1)/(v - 1)
i64 G(int v, i64 x) {
    if (x == 0) return 0;
    mat A, B;
    A[0][0] = v; A[0][1] = 1;
    A[1][0] = 0; A[1][1] = 1;
    memset(B, 0, sizeof(B));
    B[0][0] = B[1][1] = 1; // identity
    for (i64 e = x; e; e >>= 1) {
        if (e & 1) matmul(B, A, B);
        matmul(A, A, A);
    }
    return (i64)v % MOD * (B[0][1] % MOD) % MOD;
}

i64 g(i64 x) {
    return (G(3, x) - G(2, x) + MOD) % MOD;
}

int main() {
    // Sieve Mobius function and compute prefix sums
    vector<int> mu_val(LMT + 1, 0);
    vector<bool> is_composite(LMT + 1, false);
    vector<int> primes;
    mu_val[1] = 1;

    for (int i = 2; i <= LMT; i++) {
        if (!is_composite[i]) {
            primes.push_back(i);
            mu_val[i] = -1;
        }
        for (int j = 0; j < (int)primes.size() && (i64)i * primes[j] <= LMT; j++) {
            int t = i * primes[j];
            is_composite[t] = true;
            if (i % primes[j] == 0) {
                mu_val[t] = 0;
                break;
            } else {
                mu_val[t] = -mu_val[i];
            }
        }
    }

    mu_prefix[0] = 0;
    for (int i = 1; i <= LMT; i++)
        mu_prefix[i] = (mu_prefix[i - 1] + mu_val[i] % MOD + MOD) % MOD;

    // Compute t(N) = sum_{k=1}^{N} mu(k) * g(floor(N/k))
    // Group by distinct values of floor(N/k)
    i64 ans = 0;
    i64 prev_mertens = 0; // M(0) = 0
    for (i64 i = 1, j; i <= N; i = j) {
        j = N / (N / i) + 1;
        i64 cur_mertens = mertens(j - 1);
        i64 block_mu_sum = (cur_mertens - prev_mertens + MOD) % MOD;
        ans = (ans + block_mu_sum % MOD * g(N / i) % MOD) % MOD;
        prev_mertens = cur_mertens;
    }

    cout << ans % MOD << endl;
    return 0;
}