Let $S_n$ be an integer sequence produced with the following pseudo-random number generator:
\begin{align}
S_0 & = 290797 \\
S_{n+1} & = S_n^2 \bmod 50515093
\end{align}
Let $A(i, j)$ be the minimum of the numbers $S_i, S_{i+1}, \dots, S_j$ for $i\le j$.
Let $M(N) = \sum A(i, j)$ for $1 \le i \le j \le N$.
We can verify that $M(10) = 432256955$ and $M(10\,000) = 3264567774119$.
Find $M(2\,000\,000\,000)$.
To find the value of $M(2,000,000,000)$, we need to compute the minimum value of the subsequence $S_i, S_{i+1}, \dots, S_j$ for all possible $i$ and $j$, where $1 \leq i \leq j \leq 2,000,000,000$, and then sum up all these minimum values.
First, let’s create a Python function that generates the pseudo-random sequence $S_n$ based on the given formula:
“`python
def generate_sequence(n):
S = [290797] # Initialize with S_0 = 290797
for i in range(1, n):
S.append((S[-1] ** 2) % 50515093)
return S
“`
Next, let’s create a function `compute_m` that calculates the value of $M(N)$:
“`python
def compute_m(N):
sequence = generate_sequence(N)
M = 0
for i in range(N):
for j in range(i, N):
min_value = min(sequence[i:j+1])
M += min_value
return M
“`
Note that this brute-force approach will be extremely slow for large values of $N$ because it has a time complexity of $O(N^3)$. Therefore, we need to optimize our solution.
To optimize the solution, we will use dynamic programming to avoid redundant calculations. We will create a list `min_values` of size `N` to store the minimum values of subsequences $S_i, S_{i+1}, \dots, S_j$ for each index $i$. Then, we can use this list to calculate $M(N)$ without repeating the same calculations multiple times:
“`python
def compute_m_optimized(N):
sequence = generate_sequence(N)
min_values = [sequence[i] for i in range(N)]
M = min_values[0] # Initialize M with the first value of the sequence
for i in range(1, N):
min_values[i] = min(min_values[i], sequence[i])
M += min_values[i]
for j in range(i+1, N):
min_values[j] = min(min_values[j], sequence[j], min_values[j-1])
M += min_values[j]
return M
“`
This optimized approach has a time complexity of $O(N^2)$, which is more efficient than the brute-force approach.
Finally, let’s compute the value of $M(2,000,000,000)$ using the `compute_m_optimized` function:
“`python
result = compute_m_optimized(2000000000)
print(“M(2,000,000,000) =”, result)
“`
Please note that calculating $M(2,000,000,000)$ may take a significant amount of time even with the optimized approach due to the large size of the input.
More Answers:
Pencils of RaysCircumscribed Circles
Maximum Integer Partition Product