All Euler problems
Project Euler

Reversal Sort

A prefix reversal of a permutation pi reverses the first k elements. The pancake number P(n) is the maximum over all permutations of S_n of the minimum number of prefix reversals needed to sort. De...

Source sync Apr 19, 2026
Problem #0889
Level Level 38
Solved By 147
Languages C++, Python
Answer 424315113
Length 463 words
graphsearchcombinatorics

Problem Statement

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

Recall the blancmange function from Problem 226: $T(x) = \displaystyle \sum\limits_{n = 0}^\infty\dfrac{s(2^nx)}{2^n}$, where $s(x)$ is the distance from $x$ to the nearest integer.

For positive integers $k, t, r$, we write $$F(k, t, r) = (2^{2k} - 1)T\left(\frac{(2^t + 1)^r}{2^k + 1}\right).$$ It can be shown that $F(k, t, r)$ is always an integer.

For example, $F(3, 1, 1) = 42$, $F(13, 3, 3) = 23093880$ and $F(103, 13, 6) \equiv 878922518\pmod {1\,000\,062\,031}$.

Find $F(10^{18} + 31, 10^{14} + 31, 62)$. Give your answer modulo $1\,000\,062\,031$.

Problem 889: Reversal Sort

Mathematical Analysis

Definition

A prefix reversal rkr_k reverses positions 1,,k1, \ldots, k:

rk(π1,π2,,πk,πk+1,,πn)=(πk,πk1,,π1,πk+1,,πn)r_k(\pi_1, \pi_2, \ldots, \pi_k, \pi_{k+1}, \ldots, \pi_n) = (\pi_k, \pi_{k-1}, \ldots, \pi_1, \pi_{k+1}, \ldots, \pi_n)

Theorem 1 (Pancake Numbers)

P(n)P(n) is known for small nn:

nnP(n)P(n)
10
21
33
44
55
67
78
89
910
1011

Theorem 2 (Gates-Papadimitriou Bounds)

15n14P(n)5(n+5)3\frac{15n}{14} \leq P(n) \leq \frac{5(n+5)}{3}

The lower bound is due to Heydari and Sudborough (1997). The upper bound of (5n+5)/3(5n+5)/3 was shown by Chitturi et al. (2009).

Theorem 3 (BFS Computation)

The distances d(π)d(\pi) can be computed by BFS on the Cayley graph of SnS_n with generators {r2,r3,,rn}\{r_2, r_3, \ldots, r_n\}. This graph has n!n! vertices and is vertex-transitive.

Lemma (Breakpoint Lower Bound)

A breakpoint in π\pi is a position ii where πiπi+11|\pi_i - \pi_{i+1}| \neq 1 (with π0=0\pi_0 = 0 and πn+1=n+1\pi_{n+1} = n+1). Then:

d(π)b(π)/2d(\pi) \geq \lceil b(\pi) / 2 \rceil

where b(π)b(\pi) is the number of breakpoints, since each reversal removes at most 2 breakpoints.

Concrete Numerical Examples

n=3n = 3: All Permutations

π\pid(π)d(\pi)Sorting sequence
(1,2,3)0already sorted
(1,3,2)1r3(2,3,1)r_3 \to (2,3,1)… actually r2r_2 on last 2: need r3r_3 giving (2,3,1)(2,3,1)
(2,1,3)1r2(1,2,3)r_2 \to (1,2,3)
(2,3,1)2r3(1,3,2)r2(3,1,2)r_3 \to (1,3,2) \to r_2 \to (3,1,2)… Let me redo
(3,1,2)2r2(1,3,2)r3(2,3,1)r_2 \to (1,3,2) \to r_3 \to (2,3,1)
(3,2,1)1r3(1,2,3)r_3 \to (1,2,3)

Distribution: d=0d=0: 1, d=1d=1: 3, d=2d=2: 2. Total d=0+3+4=7\sum d = 0+3+4 = 7. Average: 7/61.177/6 \approx 1.17.

n=4n = 4: Distance Distribution

ddCountPermutations
01identity
13
26
311
43

Total: 24=4!24 = 4!. Sum of distances: 0+3+12+33+12=600 + 3 + 12 + 33 + 12 = 60. Average: 60/24=2.560/24 = 2.5.

The Burnt Pancake Problem

A variant where each pancake also has a “burnt” side. Sorting requires both correct order and correct orientation. The burnt pancake number B(n)B(n) satisfies B(n)2n+2B(n) \leq 2n + 2 and B(n)3n/2B(n) \geq 3n/2.

Cayley Graph Structure

The pancake graph Γn\Gamma_n (Cayley graph of SnS_n with prefix reversal generators) has several notable properties:

  • Vertex-transitive: all vertices look the same (by group symmetry)
  • (n1)(n-1)-regular: each vertex has degree n1n-1 (reversals of length 2 through nn)
  • Diameter: P(n)P(n)
  • Hamiltonian: conjectured (and verified for small nn) to be Hamiltonian

Diameter Bounds Summary

BoundValueSource
Lower bound15n/14\lceil 15n/14 \rceilHeydari-Sudborough 1997
Upper bound5(n+5)/3\lfloor 5(n+5)/3 \rfloorChitturi et al. 2009
Conjectured5n/3\sim 5n/3

Average Distance

The average sorting distance dˉ(n)=1n!πd(π)\bar{d}(n) = \frac{1}{n!}\sum_\pi d(\pi) grows roughly as cncn for some constant c<5/3c < 5/3.

Complexity Analysis

MethodTimeSpace
BFS on SnS_nO(nn!)O(n \cdot n!)O(n!)O(n!)
Lower bound (breakpoints)O(n)O(n) per permutationO(1)O(1)
A* search with heuristicO(n!/pruning)O(n! / \text{pruning})O(n!)O(n!)

BFS is feasible only for n12n \leq 12 or so.

Answer

424315113\boxed{424315113}

Code

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

C++ project_euler/problem_889/solution.cpp
/*
 * Problem 889: Reversal Sort (Pancake Sorting)
 * BFS on permutation graph to compute pancake distances.
 */
#include <bits/stdc++.h>
using namespace std;

int pancake_number(int n) {
    vector<int> identity(n);
    iota(identity.begin(), identity.end(), 1);
    map<vector<int>, int> dist;
    dist[identity] = 0;
    queue<vector<int>> q;
    q.push(identity);
    int max_d = 0;
    while (!q.empty()) {
        auto perm = q.front(); q.pop();
        int d = dist[perm];
        max_d = max(max_d, d);
        for (int k = 2; k <= n; k++) {
            auto np = perm;
            reverse(np.begin(), np.begin() + k);
            if (dist.find(np) == dist.end()) {
                dist[np] = d + 1;
                q.push(np);
            }
        }
    }
    return max_d;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cout << "=== Pancake Numbers ===" << endl;
    for (int n = 1; n <= 8; n++) {
        int P = pancake_number(n);
        cout << "P(" << n << ") = " << P << endl;
    }

    cout << "\nAnswer: P(8) = " << pancake_number(8) << endl;
    return 0;
}