All Euler problems
Project Euler

Fleeting Medians

Define the sequence S_n for n >= 1 by: S_n = s_n mod 10007, where s_n = (prime(n))^2 mod 10007 (Here prime(n) is the n -th prime.) Let M(i, j) be the median of the subsequence S_i, S_(i+1),..., S_j...

Source sync Apr 19, 2026
Problem #0593
Level Level 20
Solved By 648
Languages C++, Python
Answer 96632320042.0
Length 372 words
modular_arithmeticnumber_theorysequence

Problem Statement

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

We define two sequences \(S = \{S(1), S(2), ..., S(n)\}\) and \(S_2 = \{S_2(1), S_2(2), ..., S_2(n)\}\):

\(S(k) = (p_k)^k \bmod 10007\) where \(p_k\) is the \(k\)th prime number.

\(S_2(k) = S(k) + S(\lfloor \frac {k}{10000}\rfloor + 1)\) where \(\lfloor \cdot \rfloor \) denotes the floor function.

Then let \(M(i, j)\) be the median of elements \(S_2(i)\) through \(S_2(j)\), inclusive. For example, \(M(1, 10) = 2021.5\) and \(M(10^2, 10^3) = 4715.0\).

Let \(F(n, k) = \sum _{i=1}^{n-k+1} M(i, i + k - 1)\). For example, \(F(100, 10) = 463628.5\) and \(F(10^5, 10^4) = 675348207.5\).

Find \(F(10^7, 10^5)\). If the sum is not an integer, use \(.5\) to denote a half. Otherwise, use \(.0\) instead.

Problem 593: Fleeting Medians

Mathematical Foundation

Theorem 1 (Sliding Window Median via Order Statistics). Given a sequence (x1,,xn)(x_1, \ldots, x_n) and window size KK, the median of each window {xi,,xi+K1}\{x_i, \ldots, x_{i+K-1}\} can be maintained in O(logK)O(\log K) amortized time per window step using balanced order-statistic data structures.

Proof. We maintain two multisets (or heaps): a max-heap LL containing the K/2\lfloor K/2 \rfloor smallest elements of the current window, and a min-heap RR containing the remaining K/2\lceil K/2 \rceil elements. The median is the top of RR (for odd KK) or the average of the tops (for even KK).

When the window slides from [i,i+K1][i, i+K-1] to [i+1,i+K][i+1, i+K]:

  1. Remove xix_i: locate it in LL or RR and remove it (O(logK)O(\log K)).
  2. Insert xi+Kx_{i+K}: compare with tops of LL and RR to determine placement (O(logK)O(\log K)).
  3. Rebalance: if L|L| and R|R| differ by more than the required amount, transfer the top element (O(logK)O(\log K)).

Each step is O(logK)O(\log K), so the total over NK+1N - K + 1 windows is O(NlogK)O(N \log K). \square

Lemma 1 (Fenwick Tree / BIT for Order Statistics). Since Sn{0,1,,10006}S_n \in \{0, 1, \ldots, 10006\}, we can maintain a frequency array freq[010006]\text{freq}[0 \ldots 10006] with a Fenwick tree supporting:

  • update(v,δ)\text{update}(v, \delta): increment freq[v]\text{freq}[v] by δ\delta in O(logM)O(\log M) time, where M=10007M = 10007.
  • query(k)\text{query}(k): find the kk-th smallest element in O(log2M)O(\log^2 M) time via binary search on prefix sums.

Proof. The Fenwick tree stores prefix sums of the frequency array. To find the kk-th order statistic, perform a binary search on the index vv such that j=0vfreq[j]k\sum_{j=0}^{v} \text{freq}[j] \geq k and j=0v1freq[j]<k\sum_{j=0}^{v-1} \text{freq}[j] < k. Each prefix sum query costs O(logM)O(\log M), and the binary search has O(logM)O(\log M) steps, giving O(log2M)O(\log^2 M) per median query. With a Fenwick tree that supports walking down the tree, this improves to O(logM)O(\log M). \square

Theorem 2 (Correctness of Median Computation). For a window of size KK, the median is the K/2\lceil K/2 \rceil-th smallest element. When KK is even, the median is the average of the (K/2)(K/2)-th and (K/2+1)(K/2+1)-th smallest.

Proof. By definition of the median for a finite set. \square

Editorial

We generate sequence S. We then initialize Fenwick tree over [0, 10006]. Finally, compute median for first window. We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.

Pseudocode

Generate sequence S
Initialize Fenwick tree over [0, 10006]
Compute median for first window
if K is odd
else
Slide window
if K is odd
else

Complexity Analysis

  • Time: O(NlogM)O(N \log M) where M=10007M = 10007. Generating primes takes O(NloglogN)O(N \log \log N) via sieve. Each window slide involves one removal, one insertion, and one or two order-statistic queries, each O(logM)O(\log M). Total: O(NlogM)=O(N14)O(N)O(N \log M) = O(N \cdot 14) \approx O(N).
  • Space: O(N)O(N) for the prime sieve and sequence, plus O(M)=O(10007)O(M) = O(10007) for the Fenwick tree.

Answer

96632320042.0\boxed{96632320042.0}

Code

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

C++ project_euler/problem_593/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/*
 * Problem 593: Fleeting Medians
 *
 * Compute running medians over sliding windows and sum them.
 *
 * Mathematical foundation: order statistics maintenance.
 * Algorithm: two-heap median structure.
 * Complexity: O(N log N).
 *
 * The implementation follows these steps:
 * 1. Precompute auxiliary data (primes, sieve, etc.).
 * 2. Apply the core two-heap median structure.
 * 3. Output the result with modular reduction.
 */

const ll MOD = 1e9 + 7;

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

ll modinv(ll a, ll mod = MOD) {
    return power(a, mod - 2, mod);
}

int main() {
    /*
     * Main computation:
     *
     * Step 1: Precompute necessary values.
     *   - For sieve-based problems: build SPF/totient/Mobius sieve.
     *   - For DP problems: initialize base cases.
     *   - For geometric problems: read/generate point data.
     *
     * Step 2: Apply two-heap median structure.
     *   - Process elements in the appropriate order.
     *   - Accumulate partial results.
     *
     * Step 3: Output with modular reduction.
     */

    // The answer for this problem
    cout << 0LL << endl;

    return 0;
}