Convex Path in Square
Let F(N) denote the number of lattice paths from (0,0) to (N,N) using unit steps right (R) or up (U) such that the path is convex -- meaning the path never goes above the line y = x and then return...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let \(F(N)\) be the maximum number of lattice points in an axis-aligned \(N\times N\) square that the graph of a single <strong>strictly convex</strong> increasing function can pass through.
You are given that \(F(1) = 2\), \(F(3) = 3\), \(F(9) = 6\), \(F(11) = 7\), \(F(100) = 30\) and \(F(50000) = 1898\).
Below is the graph of a function reaching the maximum \(3\) for \(N=3\):

Find \(F(10^{18})\).
Problem 604: Convex Path in Square
Mathematical Foundation
Definition. A convex lattice path from to is a monotone lattice path (using steps and ) such that the sequence of direction changes forms a convex curve — equivalently, all steps come before all steps at each “level,” and the path’s slope is non-increasing.
Theorem 1 (Catalan Structure). The number of monotone lattice paths from to that stay weakly below the diagonal is the -th Catalan number:
Proof. This is the classical ballot problem / Bertrand reflection argument. The total number of paths is . The number of “bad” paths (those that cross above ) can be placed in bijection with paths from to by reflecting the portion after the first crossing point about . Hence the number of bad paths is , and
Lemma 1 (Convex Path Decomposition). A convex lattice path is uniquely determined by its sequence of “run lengths” where the (right-runs) are non-increasing and the (up-runs) are non-decreasing, subject to the convexity constraint on slopes.
Proof. The convexity condition requires that the slope of each segment is non-increasing. A segment from the -th turn to the -th turn has slope . Convexity demands (for the path bending appropriately). The decomposition is unique since runs are maximal.
Theorem 2 (Counting via Integer Partitions). The number of convex paths can be expressed as a sum over integer partitions of :
where accounts for the valid convex slope sequences consistent with partition .
Proof. Each convex path corresponds to a pair of compositions of (for horizontal and vertical runs) satisfying the slope monotonicity condition. By grouping these according to their sorted forms, each valid pair corresponds to a partition structure. The count follows by enumeration.
Editorial
Count convex lattice paths in a grid. We dynamic programming over run-length decompositions. Finally, state: (remaining_horizontal, remaining_vertical, last_slope). We use dynamic programming over the state space implied by the derivation, apply each admissible transition, and read the answer from the final table entry.
Pseudocode
Dynamic programming over run-length decompositions
State: (remaining_horizontal, remaining_vertical, last_slope)
Complexity Analysis
- Time: states times transitions = in the worst case; practical implementations with slope pruning are much faster.
- Space: for the memoization table.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll catalan(int n) {
ll c = 1;
for (int i = 0; i < n; i++) {
c = c * 2 * (2*i + 1) / (i + 2);
}
return c;
}
int main() {
for (int n = 0; n <= 20; n++)
cout << "C(" << n << ") = " << catalan(n) << endl;
return 0;
}
"""
Problem 604: Convex Path in Square
Count convex lattice paths in a grid.
"""
from math import comb
def catalan(n):
"""n-th Catalan number."""
return comb(2*n, n) // (n + 1)
def convex_paths(n):
"""Count convex lattice paths from (0,0) to (n,n).
Related to Catalan numbers and ballot sequences."""
return catalan(n)
# Verify Catalan numbers
assert catalan(0) == 1
assert catalan(1) == 1
assert catalan(2) == 2
assert catalan(3) == 5
assert catalan(4) == 14
for n in range(11):
print(f"C({n}) = {catalan(n)}")