PracticeSimulation

The Archive Token Ledger

MediumSimulationaccumulationstate trackingindex arithmetic Time not estimated Not started

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

Example 1
Input: n = 1
Output: 1
Explanation: For n = 1, the function returns 1.
Example 2
Input: n = 2
Output: 2
Explanation: For n = 2, the function returns 2.
Example 3
Input: n = 7
Output: 63
Explanation: For n = 7, the stored values end with 63 at position 7, and the function returns 63.

Constraints

  • 1 <= n <= 1000
  • Types: n is int; result is int

Function signature

def archive_ledger_value(n: int) -> int
n int
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.
Approved · c025r1-abs_538_v314 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1