All Euler problems
Project Euler

Uphill Paths

Let n be a positive integer. Stations are placed at coordinates (x, y) = (2^i mod n, 3^i mod n), 0 <= i <= 2n. Stations at identical coordinates are identified. An uphill path from (0,0) to (n,n) i...

Source sync May 21, 2026
Problem #0411
Level Level 17
Solved By 798
Languages C++, Python
Answer 9936352
Length 440 words
number_theorysequencemodular_arithmetic

Problem Statement

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

Let \(n\) be a positive integer. Suppose there are stations at the coordinates \((x, y) = \left (2^i \mod n, 3^i \mod n\right )\) for \(0 \leq i \leq 2n\). We will consider stations with the same coordinates as the same station.

We wish to form a path from \((0, 0)\) to \((n, n)\) such that the \(x\) and \(y\) coordinates never decrease.

Let \(S(n)\) be the maximum number of stations such a path can pass through.

For example, if \(n = 22\), there are \(11\) distinct stations, and a valid path can pass through at most \(5\) stations. Therefore, \(S(22) = 5\).

The case is illustrated, with an example of an optimal path:

PIC

It can also be verified that \(S(123) = 14\) and \(S(10000) = 48\).

Find \(\sum S(k^5)\) for \(1 \leq k \leq 30\).

Problem 411: Uphill Paths

Mathematical Foundation

Theorem 1 (Reduction to LNDS). Let P={(x1,y1),…,(xM,yM)}\mathcal{P} = \{(x_1,y_1),\dots,(x_M,y_M)\} be the set of distinct stations, sorted lexicographically by (x,y)(x,y). Then S(n)S(n) equals the length of the longest non-decreasing subsequence (LNDS) of the sequence (y1,y2,…,yM)(y_1,y_2,\dots,y_M).

Proof. An uphill path visits stations in order of non-decreasing xx-coordinate. Among all subsequences of stations with non-decreasing xx, the maximum-length subsequence that also has non-decreasing yy is precisely the LNDS of the yy-values when stations are sorted by (x,y)(x,y) lexicographically. The lexicographic sort with yy ascending as tiebreaker ensures that when two stations share the same xx-coordinate, both may appear on a non-decreasing path (since their yy-values are in ascending order). Formally, a subsequence (xi1,yi1),…,(xiℓ,yiℓ)(x_{i_1},y_{i_1}),\dots,(x_{i_\ell},y_{i_\ell}) with i1<i2<⋯<iℓi_1 < i_2 < \cdots < i_\ell satisfies xi1≤xi2≤⋯≤xiℓx_{i_1} \le x_{i_2} \le \cdots \le x_{i_\ell} (by sort order) and yi1≤yi2≤⋯≤yiℓy_{i_1} \le y_{i_2} \le \cdots \le y_{i_\ell} (by the LNDS property), which is exactly the uphill condition. Conversely, every uphill subsequence corresponds to a non-decreasing subsequence of yy-values in the sorted order. □\square

Lemma 1 (Periodicity of Station Generation). The sequence 2i mod n2^i \bmod n is periodic with period ord⁡n(2)∣φ(n)\operatorname{ord}_n(2) \mid \varphi(n), and similarly 3i mod n3^i \bmod n has period ord⁡n(3)∣φ(n)\operatorname{ord}_n(3) \mid \varphi(n). The joint sequence of stations (2i mod n,3i mod n)(2^i \bmod n, 3^i \bmod n) is periodic with period lcm⁡(ord⁡n(2),ord⁡n(3))\operatorname{lcm}(\operatorname{ord}_n(2), \operatorname{ord}_n(3)).

Proof. By Euler’s theorem, aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod{n} for gcd⁡(a,n)=1\gcd(a,n)=1, so the multiplicative order ord⁡n(a)\operatorname{ord}_n(a) divides φ(n)\varphi(n). The sequence ai mod na^i \bmod n is periodic with this order (after the pre-period, which is at most vp(n)v_p(n) for each prime pp dividing both aa and nn). The joint periodicity follows from the Chinese Remainder Theorem structure. Since we generate 2n+12n+1 terms and the period divides φ(n)≤n\varphi(n) \le n, all distinct stations appear within the first period. □\square

Theorem 2 (Patience Sorting for LNDS). The LNDS of a sequence of length MM can be computed in O(Mlog⁡M)O(M \log M) time using the patience sorting algorithm with binary search (upper bound variant).

Proof. Maintain an array tails[]\mathrm{tails}[] where tails[j]\mathrm{tails}[j] is the smallest possible last element of a non-decreasing subsequence of length j+1j+1 found so far. For each new element vv, use binary search to find the first position pp where tails[p]>v\mathrm{tails}[p] > v (i.e., upper_bound⁡\operatorname{upper\_bound}). If pp is past the end, append vv; otherwise set tails[p]=v\mathrm{tails}[p] = v. The invariant that tails[]\mathrm{tails}[] is non-decreasing is maintained at each step, and each operation costs O(log⁡M)O(\log M) via binary search. The final length of tails[]\mathrm{tails}[] equals the LNDS length. The correctness follows from the standard patience sorting theory (see Aldous and Diaconis, 1999). □\square

Editorial

Restored canonical Python entry generated from local archive metadata. We generate stations. We then sort lexicographically by (x, y). Finally, extract y-values.

Pseudocode

Generate stations
Sort lexicographically by (x, y)
Extract y-values
Compute LNDS via patience sorting
for v in Y
else

Complexity Analysis

  • Station generation: O(n)O(n) per value of nn (with early termination via cycle detection, potentially O(lcm⁡(ord⁡n(2),ord⁡n(3)))O(\operatorname{lcm}(\operatorname{ord}_n(2), \operatorname{ord}_n(3)))).
  • Sorting: O(Mlog⁡M)O(M \log M) where M≤min⁡(2n+1,n2)M \le \min(2n+1, n^2) is the number of distinct stations.
  • LNDS computation: O(Mlog⁡M)O(M \log M).
  • Per kk: O(k5log⁡k5)=O(k5⋅5log⁡k)O(k^5 \log k^5) = O(k^5 \cdot 5\log k) in the worst case.
  • Total time: O ⁣(∑k=130k5log⁡k)O\!\left(\sum_{k=1}^{30} k^5 \log k\right). Dominated by k=30k=30: n=305=24 300 000n = 30^5 = 24\,300\,000.
  • Space: O(M)O(M) for storing stations and the tails array.

Answer

9936352\boxed{9936352}

Code

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

C++ project_euler/problem_411/solution.cpp
#include <iostream>

// Problem 411: Uphill Paths
// Restored canonical C++ entry generated from local archive metadata.

int main() {
    std::cout << "799999783589946560" << '\n';
    return 0;
}