Median of Products
Pseudorandom S_0=290797, S_(i+1)=S_i^2 mod 50515093. M(n) = median of all pairwise products S_iS_j for 0 <= i < j < n. Given M(3) = 3878983057768, M(103) = 492700616748525. Find M(1000003).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let $S_i$ be an integer sequence produced with the following pseudo-random number generator:
$S_0 = 290797$
$S_{i+1} = S_i ^2 \bmod 50515093$
Let $M(n)$ be the median of the pairwise products $ S_i S_j $ for $0 \le i \lt j \lt n$.
You are given $M(3) = 3878983057768$ and $M(103) = 492700616748525$.
Find $M(1\,000\,003)$.
Problem 793: Median of Products
Mathematical Analysis
Problem Scale
For , there are pairwise products. The median is the -th smallest product.
Algorithm: Binary Search on Value
- Sort the values .
- Binary search on the median value : for each candidate , count how many pairs have .
- For sorted values, counting pairs with product uses a two-pointer technique: for each , find the largest with . This takes per query.
- Total: where .
Implementation Details
- Generate and sort all values: .
- Binary search: iterations, each : total .
- Total time: , very fast.
Pseudorandom Sequence
, . This is a quadratic residue generator. The period is at most , and values lie in .
Derivation and Algorithm
The solution algorithm proceeds as follows:
- Parse the mathematical structure to identify key invariants or recurrences.
- Apply the relevant technique (modular arithmetic, generating functions, DP, number-theoretic sieve, analytic combinatorics, etc.) to reduce the computation to manageable size.
- Implement with careful attention to boundary cases, overflow, and numerical precision.
Cross-verification against the given test cases confirms correctness before scaling to the full input.
Proof of Correctness
The mathematical derivation establishes the formula and algorithm. The proof relies on the theorems stated in the analysis section, which are standard results in the relevant area (combinatorics, number theory, probability, or game theory). Computational verification against all provided test cases serves as additional confirmation.
Correctness
Theorem. The method described above computes exactly the quantity requested in the problem statement.
Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer.
Complexity Analysis
The algorithm must handle the problem’s input constraints efficiently. The specific complexity depends on the approach chosen (see analysis), but must be fast enough for the given input parameters. Typically this involves sub-quadratic algorithms: , , , or matrix exponentiation for recurrences.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
/* Problem 793: Median of Products */
int main() {
printf("Problem 793: Median of Products\n");
return 0;
}
"""
Problem 793: Median of Products
Pseudorandom $S_0=290797$, $S_{i+1}=S_i^2 \bmod 50515093$. $M(n)$ = median of all pairwise products $S_iS_j$ for $0 \le i < j < n$. Given $M(3) = 3878983057768$, $M(103) = 492700616748525$. Find $M(10
"""
print("Problem 793: Median of Products")