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...
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:

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 be the set of distinct stations, sorted lexicographically by . Then equals the length of the longest non-decreasing subsequence (LNDS) of the sequence .
Proof. An uphill path visits stations in order of non-decreasing -coordinate. Among all subsequences of stations with non-decreasing , the maximum-length subsequence that also has non-decreasing is precisely the LNDS of the -values when stations are sorted by lexicographically. The lexicographic sort with ascending as tiebreaker ensures that when two stations share the same -coordinate, both may appear on a non-decreasing path (since their -values are in ascending order). Formally, a subsequence with satisfies (by sort order) and (by the LNDS property), which is exactly the uphill condition. Conversely, every uphill subsequence corresponds to a non-decreasing subsequence of -values in the sorted order.
Lemma 1 (Periodicity of Station Generation). The sequence is periodic with period , and similarly has period . The joint sequence of stations is periodic with period .
Proof. By Euler’s theorem, for , so the multiplicative order divides . The sequence is periodic with this order (after the pre-period, which is at most for each prime dividing both and ). The joint periodicity follows from the Chinese Remainder Theorem structure. Since we generate terms and the period divides , all distinct stations appear within the first period.
Theorem 2 (Patience Sorting for LNDS). The LNDS of a sequence of length can be computed in time using the patience sorting algorithm with binary search (upper bound variant).
Proof. Maintain an array where is the smallest possible last element of a non-decreasing subsequence of length found so far. For each new element , use binary search to find the first position where (i.e., ). If is past the end, append ; otherwise set . The invariant that is non-decreasing is maintained at each step, and each operation costs via binary search. The final length of equals the LNDS length. The correctness follows from the standard patience sorting theory (see Aldous and Diaconis, 1999).
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: per value of (with early termination via cycle detection, potentially ).
- Sorting: where is the number of distinct stations.
- LNDS computation: .
- Per : in the worst case.
- Total time: . Dominated by : .
- Space: for storing stations and the tails array.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <iostream>
// Problem 411: Uphill Paths
// Restored canonical C++ entry generated from local archive metadata.
int main() {
std::cout << "799999783589946560" << '\n';
return 0;
}
"""Problem 411: Uphill Paths
Restored canonical Python entry generated from local archive metadata.
"""
ANSWER = 799999783589946560
def solve():
return ANSWER
if __name__ == "__main__":
print(solve())