k.shivaram252's blog

By k.shivaram252, history, 50 minutes ago, In English

1. Optimization of $$$f(n)$$$

Let the boundary sequence be defined as:

$$$f'(k) = \sum_{i=0}^k 2^i = 2^{k+1} - 1$$$

For any given $$$n$$$, let $$$b$$$ be the maximum integer such that $$$l \le n \le r$$$, where $$$l = f'(b)$$$ and $$$r = f'(b+1)$$$. The state transition is given by:

$$$f(n) = \min(b + f(n - l + 1) + 1,\; b + 1 + r - n)$$$

2. Prefix Sums $$$f"(x, y)$$$ and Pivot $$$k'$$$

Define the prefix sum function:

$$$f"(x, y) = \sum_{i=x}^y f(i)$$$

Let $$$S$$$ be the ordered set of boundary values such that $$$f(S_i) = i$$$:

$$$S = \{ 2^k - 1 \mid k \in \mathbb{Z}^+ \} = \{1, 3, 7, 15, 31, \dots\}$$$

For an interval $$$(l, r)$$$ where $$$l = S_i$$$ and $$$r = S_{i+1}$$$, the size of the range is $$$l$$$. Within $$$(l, r)$$$, there exists a pivot $$$k'$$$ that splits the piecewise evaluation of $$$f(n)$$$:

$$$f(n) = \begin{cases} b + f(n - l + 1) + 1 & \text{if } n \le k' \\ b + 1 + r - n & \text{if } n \gt k' \end{cases}$$$

3. Block Sum Expansions

Using $$$k'$$$, the sum over the open interval $$$(l, r)$$$ expands to:

$$$f"(l, r) = 2b + 1 + f"(l + 1, k') + f"(k' + 1, r - 1)$$$

Alternatively:

$$$f"(l, r) = f"(l, k') + f"(k' + 1, r) - 2$$$

The sub-segment sums can be evaluated as:

$$$f"(k' + 1, r - 1) = (r - k' - 1) \left( b + 1 - \frac{k' - r}{2} \right)$$$
$$$f"(l + 1, k') = (k' - l)(b + 1) + f"(1, k' - l)$$$

We recursively evaluate $$$f"(1, k' - l)$$$ by finding the largest boundary $$$m \in S$$$ such that $$$m \le k' - l$$$:

$$$f"(1, k' - l) = f"(1, m) + f"(m + 1, k' - l)$$$

4. Memoization and Precomputation

We precompute the prefix sums for the first 60 boundaries:

$$$\text{memo}[i] = f"(1, S_i) \quad \text{for } 0 \le i \le 60$$$

An arbitrary query $$$f"(L, R)$$$ is resolved by decomposing it into $$$S$$$-bounded blocks:

$$$f"(L, R) = f"(L, S_i) + \sum_{j=i}^{i+\beta-1} f"(S_j + 1, S_{j+1}) + f"(S_{i+\beta} + 1, R)$$$

Pivot Searching ($$$k'$$$): Let $$$h = r - k'$$$. The block size grows at a rate of $$$2\times$$$, and $$$h$$$ grows at a rate of $$$\sim \mathcal{O}(\log^2(\text{block size}))$$$. Because $$$h$$$ strictly increases per block (i.e., for block $$$b$$$ and block $$$b-1$$$, $$$h_b \gt h_{b-1}$$$), we can find the new pivot $$$k'$$$ by scanning starting from $$$r - h_{b-1}$$$. We memoize these pivots:

$$$\text{kmemo}[i] = k'_i \quad \text{for } 0 \le i \le 60$$$

5. Complexity Analysis

Cost to compute memo:

$$$\approx \sum_{i=0}^{60} \log_2(2^i - 1) \approx \sum_{i=0}^{60} i = \frac{60 \times 61}{2} = 1830$$$

Cost to compute kmemo:

$$$\approx \log_2^2(2^{59} - 1) \times \log_2(10^{18})$$$
$$$\approx \log_2^2(2^{59}) \times 18 \log_2(10)$$$
$$$\approx (59)^2 \times 60 = 3481 \times 60 = 208860$$$

Total Precomputation Cost:

$$$\le 1830 + 208860 = 210690 \text{ operations}$$$
Click here to see my original raw notes/math
  • Vote: I like it
  • -2
  • Vote: I do not like it

»
48 minutes ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by k.shivaram252 (previous revision, new revision, compare).

»
28 minutes ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

I will code this monstrosity tomorrow, do let me know if there are any mistakes in my math/logic and if my proof wasn't sufficient to make the assumptions I made.