Average and Variance
Find ordered quadruples (a,b,c,d) with 1 <= a <= b <= c <= d <= n where the arithmetic mean equals twice the variance. Define S(n) = sum (a+b+c+d) over all qualifying quadruples. Given S(5) = 48 an...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Denote the average of \(k\) numbers \(x_1, ..., x_k\) by \(\bar {x} = \frac {1}{k} \sum _i x_i\). Their variance is defined as \(\frac {1}{k} \sum _i \left ( x_i - \bar {x} \right ) ^ 2\).
Let \(S(n)\) be the sum of all quadruples of integers \((a,b,c,d)\) satisfying \(1 \leq a \leq b \leq c \leq d \leq n\) such that their average is exactly twice their variance.
For \(n=5\), there are \(5\) such quadruples, namely: \((1, 1, 1, 3), (1, 1, 3, 3), (1, 2, 3, 4), (1, 3, 4, 4), (2, 2, 3, 5)\).
Hence \(S(5)=48\). You are also given \(S(10^3)=37048340\).
Hence \(S(5)=48\). You are also given \(S(10^3)=37048340\).
Problem 791: Average and Variance
Mathematical Foundation
Let and .
Theorem 1. The constraint (mean equals twice the population variance) is equivalent to , and valid solutions exist only when is even.
Proof. The mean is and the population variance is
Setting :
Multiplying by 8: , hence , so .
For to be an integer, we need . Since and have the same parity, if is odd then is odd, which is not divisible by 4. If is even, write ; then , which is divisible by 4.
Lemma 1. For even , the constraint becomes with , .
Proof. Direct substitution of into .
Lemma 2. Write , , , where (to satisfy the sum constraint). Equivalently, let be the deviations from the mean . Then the sum-of-squares constraint reduces to , i.e., we are counting lattice points on a sphere in the shifted coordinates.
Proof. Let for each variable. Then . We have
Wait, let us recompute. , , mean .
So , with the constraint , and each is a half-integer (if is odd) or integer (if is even) depending on parity.
Theorem 2 (Enumeration). For a given even sum with , one enumerates quadruples by fixing and (with ), computing the constraint on from both the sum and the sum-of-squares equations, and checking that . Given and , the values and are the roots of a quadratic, yielding at most one valid ordered pair per .
Proof. Let and . Then . For to be real and integral, we need the discriminant to be a perfect square. Then , . We verify .
Editorial
over all qualifying quadruples. Given ,. We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.
Pseudocode
total = 0
for m = 2 to 2n: // s = 2m, s ranges 4..4n
target_q = m * (m + 1)
for a = 1 to min(m/2, n): // a <= s/4 = m/2
for b = a to (2m - 2a) / 3: // b <= c <= d, so b <= (2m - a - b)/2
sigma = 2*m - a - b
pi = target_q - a*a - b*b
disc = 2*pi - sigma*sigma
If disc < 0 then continue
sqrt_disc = isqrt(disc)
If sqrt_disc * sqrt_disc != disc then continue
If (sigma - sqrt_disc) % 2 != 0 then continue
c = (sigma - sqrt_disc) / 2
d = (sigma + sqrt_disc) / 2
If c < b or d > n then continue
total = (total + 2*m) % MOD // contribution is s = 2m
Return total
For , a direct triple loop is too slow. The efficient approach uses the lattice-point characterization: for each , count representations of as a sum of 4 squares subject to the linear and ordering constraints, exploiting the factorization of the representation number and Moebius-type sums to handle the ordering and range constraints. This reduces computation to via a sieve over .
## Complexity Analysis
- **Time:** $O(n \log n)$ using sieve-based enumeration of representations and modular accumulation.
- **Space:** $O(n)$ for the sieve arrays.
## Answer
$$
\boxed{404890862}
$$ Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
/* Problem 791: Average and Variance */
int main() {
printf("Problem 791: Average and Variance\n");
return 0;
}
"""
Problem 791: Average and Variance
Find ordered quadruples $(a,b,c,d)$ with $1 \le a \le b \le c \le d \le n$ where the arithmetic mean equals twice the variance. $S(n) = \sum(a+b+c+d)$ over all qualifying quadruples. Given $S(5)=48$,
"""
print("Problem 791: Average and Variance")