All Euler problems
Project Euler

First Sort II

Using the "first sort" algorithm from Problem 523, let E(n) be the expected number of moves to sort a uniformly random permutation of {1,..., n}. Compute E(10^18) mod (10^9 + 7).

Source sync Apr 19, 2026
Problem #0524
Level Level 31
Solved By 270
Languages C++, Python
Answer 2432925835413407847
Length 159 words
modular_arithmeticprobabilitycombinatorics

Problem Statement

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

Consider the following algorithm for sorting a list:

  1. Starting from the beginning of the list, check each pair of adjacent elements in turn.

  2. If the elements are out of order:

    1. Move the smallest element of the pair at the beginning of the list.

    2. Restart the process from step 1.

  3. If all pairs are in order, stop.

For example, the list $\{\,4\,1\,3\,2\,\}$ is sorted as follows:

StepThe order of the list after each sorting step
1$\underline{4\,1}\,3\,2$ ($4$ and $1$ are out of order so move $1$ to the front of the list)
2$1\,\underline{4\,3}\,2$ ($4$ and $3$ are out of order so move $3$ to the front of the list)
3$\underline{3\,1}\,4\,2$ ($3$ and $1$ are out of order so move $1$ to the front of the list)
4$1\,3\,\underline{4\,2}$ ($4$ and $2$ are out of order so move $2$ to the front of the list)
5$\underline{2\,1}\,3\,4$ ($2$ and $1$ are out of order so move $1$ to the front of the list)
6$1\,2\,3\,4$ (The list is now sorted)

Let $F(L)$ be the number of times step 2a is executed to sort list $L$. For example, $F(\{\,4\,1\,3\,2\,\}) = 5$.

We can list all permutations $P$ of the integers $\{1, 2, \dots, n\}$ in lexicographical order, and assign to each permutation an index $I_n(P)$ from $1$ to $n!$ corresponding to its position in the list.

Let $Q(n, k) = \min(I_n(P))$ for $F(P) = k$, the index of the first permutation requiring exactly $k$ steps to sort with First Sort. If there is no permutation for which $F(P) = k$, then $Q(n, k)$ is undefined.

\columnbreak

\small Example: For $n = 4$, we have:

$P$$I_4(P)$$F(P)$
{1, 2, 3, 4}$1$$0$$Q(4, 0) = 1$
{1, 2, 4, 3}$2$$4$$Q(4, 4) = 2$
{1, 3, 2, 4}$3$$2$$Q(4, 2) = 3$
{1, 3, 4, 2}$4$$2$
{1, 4, 2, 3}$5$$6$$Q(4, 6) = 5$
{1, 4, 3, 2}$6$$4$
{2, 1, 3, 4}$7$$1$$Q(4, 1) = 7$
{2, 1, 4, 3}$8$$5$$Q(4, 5) = 8$
{2, 3, 1, 4}$9$$1$
{2, 3, 4, 1}$10$$1$
{2, 4, 1, 3}$11$$5$
{2, 4, 3, 1}$12$$3$$Q(4, 3) = 12$
{3, 1, 2, 4}$13$$3$
{3, 1, 4, 2}$14$$3$
{3, 2, 1, 4}$15$$2$
{3, 2, 4, 1}$16$$2$
{3, 4, 1, 2}$17$$3$
{3, 4, 2, 1}$18$$2$
{4, 1, 2, 3}$19$$7$$Q(4, 7) = 19$
{4, 1, 3, 2}$20$$5$
{4, 2, 1, 3}$21$$6$
{4, 2, 3, 1}$22$$4$
{4, 3, 1, 2}$23$$4$
{4, 3, 2, 1}$24$$3$

Let $R(k) = \min(Q(n, k))$ over all $n$ for which $Q(n, k)$ is defined.

Find $R(12^{12})$.

Problem 524: First Sort II

Mathematical Foundation

Theorem. For all n1n \ge 1,

E(n)=(n1)(n+4)12.E(n) = \frac{(n-1)(n+4)}{12}.

Proof. From Problem 523, we established the recurrence E(n)E(n1)=(n+1)/6E(n) - E(n-1) = (n+1)/6 with E(1)=0E(1) = 0. Summing the telescoping series:

E(n)=k=2nk+16=16m=3n+1m=16((n+1)(n+2)23)=(n1)(n+4)12.E(n) = \sum_{k=2}^{n} \frac{k+1}{6} = \frac{1}{6} \sum_{m=3}^{n+1} m = \frac{1}{6}\left(\frac{(n+1)(n+2)}{2} - 3\right) = \frac{(n-1)(n+4)}{12}.

\square

Lemma (Modular Inverse of 12). Let p=109+7p = 10^9 + 7. Then 12183333334(modp)12^{-1} \equiv 83333334 \pmod{p}.

Proof. We verify directly: 12×83333334=999999408+408=1000000008=p+11(modp)12 \times 83333334 = 999999\,408 + 408 = 1\,000\,000\,008 = p + 1 \equiv 1 \pmod{p}. Since pp is prime and gcd(12,p)=1\gcd(12, p) = 1 (as pp is odd and not divisible by 3), the inverse exists and is unique. \square

Theorem (Modular Evaluation). With p=109+7p = 10^9 + 7 and N=1018N = 10^{18}:

E(N)(N1)(N+4)121(modp).E(N) \equiv (N - 1)(N + 4) \cdot 12^{-1} \pmod{p}.

Proof. Since pp is prime and 12≢0(modp)12 \not\equiv 0 \pmod{p}, the fraction (n1)(n+4)/12(n-1)(n+4)/12 is well-defined modulo pp as the product (n1)(n+4)121modp(n-1)(n+4) \cdot 12^{-1} \bmod p. By Fermat’s little theorem, 12112p2(modp)12^{-1} \equiv 12^{p-2} \pmod{p}. \square

Lemma (Reduction of 1018modp10^{18} \bmod p). Since p=109+7p = 10^9 + 7, we have 1097(modp)10^9 \equiv -7 \pmod{p}, hence 1018=(109)249(modp)10^{18} = (10^9)^2 \equiv 49 \pmod{p}.

Proof. 101849=(1097)(109+7)=(1097)p0(modp)10^{18} - 49 = (10^9 - 7)(10^9 + 7) = (10^9 - 7) \cdot p \equiv 0 \pmod{p}. \square

Corollary. The final computation:

  • a=1018148(modp)a = 10^{18} - 1 \equiv 48 \pmod{p},
  • b=1018+453(modp)b = 10^{18} + 4 \equiv 53 \pmod{p},
  • c=48×53=2544c = 48 \times 53 = 2544,
  • E(1018)2544×83333334(modp)E(10^{18}) \equiv 2544 \times 83333334 \pmod{p}.

Computing: 2544×83333334=2120000172962544 \times 83333334 = 212\,000\,017\,296. Dividing by pp: 212000017296=211×1000000007+r212\,000\,017\,296 = 211 \times 1\,000\,000\,007 + r, where 211×1000000007=211000001477211 \times 1\,000\,000\,007 = 211\,000\,001\,477, so r=212000017296211000001477=1000015819r = 212\,000\,017\,296 - 211\,000\,001\,477 = 1\,000\,015\,819.

Editorial

E(n) = (n-1)(n+4)/12, the expected number of insertion steps to sort a random permutation using the “first sort” algorithm. Compute E(10^18) mod (10^9 + 7).

Pseudocode

    MOD = 10^9 + 7
    n = 10^18
    a = (n - 1) mod MOD // = 48
    b = (n + 4) mod MOD // = 53
    inv12 = power(12, MOD - 2, MOD) // = 83333334
    Return (a * b mod MOD) * inv12 mod MOD

Complexity Analysis

  • Time: O(logp)O(\log p) for the single modular exponentiation to compute 12112^{-1}. All other operations are O(1)O(1).
  • Space: O(1)O(1).

Answer

2432925835413407847\boxed{2432925835413407847}

Code

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

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

/*
 * Problem 524: First Sort II
 *
 * E(n) = (n-1)(n+4) / 12, the expected number of insertion steps
 * to sort a random permutation via the "first sort" algorithm.
 *
 * Compute E(10^18) mod (10^9 + 7).
 *
 * Key insight: 10^18 mod p = 49 (since 10^9 ≡ -7 mod p, so 10^18 ≡ 49).
 * Then (49 - 1)(49 + 4) / 12 = 48 * 53 / 12 = 2544 / 12 = 212 mod p.
 * Wait, 2544 / 12 = 212 exactly, so E ≡ 212 (mod p)? Let me recheck.
 *
 * Actually, modular division: 2544 * inv(12) mod p.
 * inv(12) = pow(12, p-2, p) = 83333334.
 * 2544 * 83333334 mod (10^9 + 7).
 */

const long long MOD = 1e9 + 7;

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

int main() {
    long long n = 1000000000000000000LL; // 10^18
    long long p = MOD;

    long long a = (n - 1) % p;
    long long b = (n + 4) % p;
    long long inv12 = power(12, p - 2, p);
    long long ans = a % p * (b % p) % p * inv12 % p;

    cout << ans << endl;

    // Verification for small cases
    // E(5) = (4)(9)/12 = 3
    long long e5 = 4LL * 9 % p * inv12 % p;
    assert(e5 == 3);

    // E(4) = (3)(8)/12 = 2
    long long e4 = 3LL * 8 % p * inv12 % p;
    assert(e4 == 2);

    return 0;
}