Chinese Leftovers
Let g(a, n, b, m) denote the smallest non-negative integer x satisfying the simultaneous congruences x equiv a (mod n), x equiv b (mod m), or 0 if no such x exists. Define S = sum_(n=1,000,000)^(1,...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let \(g(a, n, b, m)\) be the smallest non-negative solution \(x\) to the system: \[\begin {cases} x = a \bmod n \\ x = b \bmod n \end {cases}\] if such a solution exists, otherwise \(0\).
E.g. \(g(2,4,4,6)=10\), but \(g(3,4,4,6)=0\).
Let \(\phi (n)\) be Euler’s totient function.
Let \(f(n,m)=g(\phi (n),n,\phi (m),m)\)
Find \(\sum f(n,m)\) for \(1000000 \le n < m < 1005000\).
Problem 531: Chinese Leftovers
Mathematical Foundation
The Extended Chinese Remainder Theorem
Theorem 1 (Extended CRT). Let be positive integers and let . The system
has a solution if and only if . When a solution exists, it is unique modulo .
Proof. Set .
Necessity. Suppose satisfies both congruences. Then and . Since and , we have and , whence . Thus .
Sufficiency. Assume . By Bezout’s identity, there exist with . Define
The fraction is an integer by hypothesis. Clearly , so . For the second congruence, observe
Since , we have , giving
Now by hypothesis, so , and thus .
Uniqueness. If and both satisfy the system, then and . By the characterization of the least common multiple, . Hence .
Corollary 1. The smallest non-negative solution is , where is the value constructed in the sufficiency proof.
Euler’s Totient Sieve
Lemma 1 (Totient Sieve). Euler’s totient function for all can be computed in time and space.
Proof. Initialize an array for . For each prime (identified when ), iterate over all multiples of and set . By the Euler product formula,
each factor is applied exactly once for each prime , so the final value of equals . The total number of updates is by Mertens’ theorem.
Editorial
Compute S = sum of g(phi(n), n, phi(m), m) over 1000000 <= n < m <= 1005000, where g(a,n,b,m) is the smallest non-negative solution to the system x = a (mod n), x = b (mod m), or 0 if none exists. Method: Extended CRT via Bezout’s identity; totient sieve for phi values.
Pseudocode
N_MAX = 1005000
phi = TOTIENT_SIEVE(N_MAX)
S = 0
For n from 1000000 to 1005000:
For m from n+1 to 1005000:
a = phi[n], b = phi[m]
d = gcd(n, m)
If (a - b) mod d != 0 then
continue
(g, u, v) = EXTENDED_GCD(n, m)
L = n / d * m
x = (a + n * ((b - a) / d) * u) mod L
if x < 0: x += L
S += x
Return S
Complexity Analysis
- Totient sieve: time, space, with .
- Double loop: pairs, each requiring for the extended Euclidean algorithm. Total: where .
- Overall: time, space.
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;
typedef __int128 lll;
/*
* Problem 531: Chinese Leftovers
*
* Compute S = sum of g(phi(n), n, phi(m), m) over 1000000 <= n < m <= 1005000.
* g(a,n,b,m) = smallest non-negative x with x=a(mod n), x=b(mod m), or 0.
*
* Method: Euler totient sieve + extended CRT via Bezout's identity.
* Time: O(N log log N + K^2 log N).
*/
ll extended_gcd(ll a, ll b, ll &x, ll &y) {
if (a == 0) { x = 0; y = 1; return b; }
ll x1, y1;
ll g = extended_gcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return g;
}
ll crt(ll a, ll n, ll b, ll m) {
ll u, v;
ll g = extended_gcd(n, m, u, v);
if ((a - b) % g != 0) return 0;
ll mg = m / g;
ll diff = ((lll)(b - a) / g % mg * u % mg + mg) % mg;
ll lcm = n / g * m;
ll x = (lll)a + (lll)n * diff;
x = ((x % lcm) + lcm) % lcm;
return x;
}
int main() {
const int LO = 1000000, HI = 1005000;
vector<ll> phi(HI + 1);
iota(phi.begin(), phi.end(), 0);
for (int i = 2; i <= HI; i++) {
if (phi[i] == i) {
for (int j = i; j <= HI; j += i)
phi[j] -= phi[j] / i;
}
}
ll S = 0;
for (int n = LO; n <= HI; n++)
for (int m = n + 1; m <= HI; m++)
S += crt(phi[n], n, phi[m], m);
cout << S << endl;
return 0;
}
"""
Problem 531: Chinese Leftovers
Compute S = sum of g(phi(n), n, phi(m), m) over 1000000 <= n < m <= 1005000,
where g(a,n,b,m) is the smallest non-negative solution to the system
x = a (mod n), x = b (mod m), or 0 if none exists.
Method: Extended CRT via Bezout's identity; totient sieve for phi values.
"""
from math import gcd
def extended_gcd(a, b):
"""Return (g, x, y) such that a*x + b*y = g = gcd(a, b)."""
if a == 0:
return b, 0, 1
g, x, y = extended_gcd(b % a, a)
return g, y - (b // a) * x, x
def crt(a, n, b, m):
"""Smallest non-negative x with x = a (mod n), x = b (mod m), or 0."""
d = gcd(n, m)
if (a - b) % d != 0:
return 0
lcm = n // d * m
_, u, _ = extended_gcd(n, m)
x = (a + n * ((b - a) // d) * u) % lcm
return x
def euler_totient_sieve(limit):
"""Compute phi(k) for k = 0..limit via the Euler product sieve."""
phi = list(range(limit + 1))
for i in range(2, limit + 1):
if phi[i] == i: # i is prime
for j in range(i, limit + 1, i):
phi[j] -= phi[j] // i
return phi
LO, HI = 1000000, 1005000
phi = euler_totient_sieve(HI)
S = 0
for n in range(LO, HI + 1):
for m in range(n + 1, HI + 1):
S += crt(phi[n], n, phi[m], m)
print(S)