The Archive Token Ledger
Context
An archive service assigns each report a deterministic integer ledger value. A report's value depends on two earlier report positions selected from the value of the immediately preceding report. Given one requested position, calculate its ledger value without repeatedly recalculating earlier positions.
Problem
Define a sequence L using one-based positions. The first two values are L(1)=1 and L(2)=2. For every position i greater than 2, first inspect L(i-1). Define p = 1 + (L(i-1) mod (i-1)) and q = 1 + ((L(i-1) // (i-1)) mod (i-1)). Then define L(i) = L(p) + L(q) + i. Both p and q are always earlier valid positions, so each new value can be computed after the preceding value and all smaller values are known. Return L(n), the ledger value at the requested position. A direct recursive solution repeats the same positions; compute each needed position once instead.
Examples
n = 11n = 22n = 763Constraints
1 <= n <= 1000- Types:
nis int; result is int
Function signature
def archive_ledger_value(n: int) -> int
nint- The one-based position of the report whose ledger value is requested.
- returns int
- The integer value L(n) defined by the sequence recurrence.
Adapted from MBPP problem task_84 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Sequence positions are one-based.
- Use integer arithmetic; the returned value may become large.