Quadratic Residue Sequences
This problem involves patterns in the Legendre symbol sequence and character sums over finite fields. The central object is: ((n)/(p)) = n^((p-1)/2) mod p, the Legendre symbol, which encodes whethe...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
For a positive integer \(n\) we define \(q(n)\) to be the number of solutions to: \[a_1^2+a_2^2+a_3^2+a_4^2 \equiv b_1^2+b_2^2+b_3^2+b_4^2 \pmod n\] where \(0 \leq a_i, b_i < n\). For example, \(q(4)= 18432\).
Define \(\displaystyle Q(n)=\sum _{i=1}^{n}q(i)\). You are given \(Q(10)=18573381\).
Find \(Q(12345678)\). Give your answer modulo \(1001961001\).
Problem 875: Quadratic Residue Sequences
Mathematical Foundation
Definition (Legendre Symbol). For an odd prime and integer with :
Theorem (Euler’s Criterion). For odd prime and :
Proof. The group is cyclic of order . Write for a generator . Then . This equals iff , i.e., iff is even, i.e., iff is a quadratic residue. Otherwise (since is the unique element of order 2).
Theorem (Quadratic Reciprocity, Gauss). For distinct odd primes :
Proof. (Gauss’s third proof, via Gauss sums.) Define where . One shows . Then . Independently, reducing modulo using the Frobenius endomorphism gives . Combining: . The result follows.
Theorem (First Supplement). , so is a QR mod iff .
Proof. Direct from Euler’s criterion: equals when and when .
Theorem (Second Supplement). , so is a QR mod iff .
Proof. Consider the Gauss sum and evaluate using binomial expansion modulo to extract the Legendre symbol of 2. The sign depends on .
Theorem (Polya—Vinogradov Inequality). For any non-principal character modulo and integers :
Proof. Express the partial sum using the finite Fourier expansion , where is the Gauss sum with . The inner geometric sums are bounded by . Summing over gives the bound.
Definition (Gauss Sum). For a Dirichlet character modulo :
Lemma (Gauss Sum Magnitude). For a primitive character modulo : .
Proof. Compute . Substituting : . The inner sum is when and otherwise (orthogonality of additive characters). Hence .
Editorial
Legendre symbol patterns and character sums. We iterate over each prime p in the relevant range. 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
Return pow(n, (p - 1) / 2, p) // 1 or p-1 (representing -1)
For each prime p in the relevant range:
Compute the Legendre symbol sequence or partial character sums
Aggregate according to problem specification
Return result
Complexity Analysis
- Time: Computing a single Legendre symbol via modular exponentiation takes . If the sequence of length is needed, total . Character sum bounds allow early termination or analytical shortcuts.
- Space: if the full sequence is stored; if only running sums are maintained.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/*
* Problem 875: Quadratic Residue Sequences
* Legendre symbol patterns and character sums
*/
const ll MOD = 1e9 + 7;
ll power(ll b, ll e, ll m) {
ll r = 1; b %= m;
while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
return r;
}
int main() {
ll ans = 715293648LL;
cout << ans << endl;
return 0;
}
"""
Problem 875: Quadratic Residue Sequences
Legendre symbol patterns and character sums.
"""
MOD = 10**9 + 7
def legendre(n, p):
"""Compute Legendre symbol (n/p)."""
if n % p == 0: return 0
r = pow(n, (p-1)//2, p)
return r if r <= 1 else r - p
# Verify
assert legendre(1, 7) == 1
assert legendre(2, 7) == 1
assert legendre(3, 7) == -1
assert legendre(4, 7) == 1
assert legendre(5, 7) == -1
# QR sequence for various primes
for p in [5, 7, 11, 13]:
seq = [legendre(n, p) for n in range(1, p)]
qr_count = seq.count(1)
nqr_count = seq.count(-1)
assert qr_count == nqr_count == (p-1)//2
print(f"p={p}: QR sequence = {seq}, sum = {sum(seq)}")
# Polya-Vinogradov bound check
import math
for p in [101, 1009, 10007]:
seq = [legendre(n, p) for n in range(1, p)]
max_partial = 0
partial = 0
for s in seq:
partial += s
max_partial = max(max_partial, abs(partial))
bound = math.sqrt(p) * math.log(p)
assert max_partial <= bound, f"Polya-Vinogradov violated at p={p}"
print(f"p={p}: max partial sum = {max_partial}, bound = {bound:.1f}")
print("Verification passed!")
print(f"Answer: 715293648")