Project Euler Lab - Problem 663

#663 - Sums of Subarrays

● AdvancedOfficial difficulty: 53%RecurrencesTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Let \(t_k\) be the tribonacci numbers defined as:
\(\quad t_0 = t_1 = 0\);
\(\quad t_2 = 1\);
\(\quad t_k = t_{k-1} + t_{k-2} + t_{k-3} \quad \text{ for } k \ge 3\).

For a given integer \(n\), let \(A_n\) be an array of length \(n\) (indexed from \(0\) to \(n-1\)), that is initially filled with zeros.
The array is changed iteratively by replacing \(A_n[(t_{2 i-2} \bmod n)]\) with \(A_n[(t_{2 i-2} \bmod n)]+2 (t_{2 i-1} \bmod n)-n+1\) in each step \(i\).
After each step \(i\), define \(M_n(i)\) to be \(\displaystyle \max\{\sum_{j=p}^q A_n[j]: 0\le p\le q \lt n\}\), the maximal sum of any contiguous subarray of \(A_n\).

The first 6 steps for \(n=5\) are illustrated below:
Initial state: \(\, A_5=\{0,0,0,0,0\}\)
Step 1: \(\quad \Rightarrow A_5=\{-4,0,0,0,0\}\), \(M_5(1)=0\)
Step 2: \(\quad \Rightarrow A_5=\{-4, -2, 0, 0, 0\}\), \(M_5(2)=0\)
Step 3: \(\quad \Rightarrow A_5=\{-4, -2, 4, 0, 0\}\), \(M_5(3)=4\)
Step 4: \(\quad \Rightarrow A_5=\{-4, -2, 6, 0, 0\}\), \(M_5(4)=6\)
Step 5: \(\quad \Rightarrow A_5=\{-4, -2, 6, 0, 4\}\), \(M_5(5)=10\)
Step 6: \(\quad \Rightarrow A_5=\{-4, 2, 6, 0, 4\}\), \(M_5(6)=12\)

Let \(\displaystyle S(n,l)=\sum_{i=1}^l M_n(i)\). Thus \(S(5,6)=32\).
You are given \(S(5,100)=2416\), \(S(14,100)=3881\) and \(S(107,1000)=1618572\).

Find \(S(10\,000\,003,10\,200\,000)-S(10\,000\,003,10\,000\,000)\).

This problem is taken from Project Euler, Problem 663.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=663. Published Sunday, 31st March 2019, 04:00 am. Solved by 437 members at time of mirroring.

Why this is useful

Algorithmic Development. Optimal substructure and state-space reasoning are exactly how American-option pricing and optimal execution are solved (Phases 13, 16).

We classify relevance honestly - not every Euler problem is a trading application.

Prerequisites

Lessons that prepare you:
19.8 Bit Manipulation and State Compression · 19.14 Computational Complexity, Feasibility Estimation, and Proving Algorithms Correct · 19.6 Recurrence Relations and Generating Functions · 2.7 Sequences, Series, Convergence, and Power Series

Recommended stepping-stone problems: #551 · #208 · #343

Concepts: sequences-series brute-force-reduction

Likely techniques: bitmask-dp

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.