Project Euler Lab - Problem 691

#691 - Long Substring with Many Repetitions

● ResearchOfficial difficulty: 63%WordsTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Given a character string \(s\), we define \(L(k,s)\) to be the length of the longest substring of \(s\) which appears at least \(k\) times in \(s\), or \(0\) if such a substring does not exist. For example, \(L(3,\text{“bbabcabcabcacba”})=4\) because of the three occurrences of the substring \(\text{“abca”}\), and \(L(2,\text{“bbabcabcabcacba”})=7\) because of the repeated substring \(\text{“abcabca”}\). Note that the occurrences can overlap.

Let \(a_n\), \(b_n\) and \(c_n\) be the \(0/1\) sequences defined by:

  • \(a_0 = 0\)
  • \(a_{2n} = a_{n}\)
  • \(a_{2n+1} = 1-a_{n}\)
  • \(b_n = \lfloor\frac{n+1}{\varphi}\rfloor - \lfloor\frac{n}{\varphi}\rfloor\) (where \(\varphi\) is the golden ratio)
  • \(c_n = a_n + b_n - 2a_nb_n\)

and \(S_n\) the character string \(c_0\ldots c_{n-1}\). You are given that \(L(2,S_{10})=5\), \(L(3,S_{10})=2\), \(L(2,S_{100})=14\), \(L(4,S_{100})=6\), \(L(2,S_{1000})=86\), \(L(3,S_{1000}) = 45\), \(L(5,S_{1000}) = 31\), and that the sum of non-zero \(L(k,S_{1000})\) for \(k\ge 1\) is \(2460\).

Find the sum of non-zero \(L(k,S_{5000000})\) for \(k\ge 1\).

This problem is taken from Project Euler, Problem 691.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=691. Published Sunday, 1st December 2019, 10:00 am. Solved by 326 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:
17.1 Python for Quants: NumPy, pandas, and Vectorization · 19.14 Computational Complexity, Feasibility Estimation, and Proving Algorithms Correct · 19.7 Dynamic Programming: Memoization and Tabulation · 19.6 Recurrence Relations and Generating Functions · 2.7 Sequences, Series, Convergence, and Power Series

Recommended stepping-stone problems: #473 · #220 · #873

Concepts: dynamic-programming sequences-series string-processing brute-force-reduction

Likely techniques: memoization

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.