All Euler problems
Project Euler

Chinese Leftovers II

Let p_1 < p_2 <... < p_k be the first k primes. For a positive integer n, define r_i(n) = n mod p_i, 1 <= i <= k. Define S(n, k) as the number of integers m with 1 <= m <= n such that r_i(m) >= r_i...

Source sync Apr 19, 2026
Problem #0552
Level Level 22
Solved By 528
Languages C++, Python
Answer 326227335
Length 308 words
modular_arithmeticnumber_theorycombinatorics

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let $A_n$ be the smallest positive integer satisfying $A_n \bmod p_i = i$ for all $1 \le i \le n$,

where $p_i$ is the $i$-th prime.

For example $A_2 = 5$, since this is the smallest positive solution of the system of equations \begin{align*} A_2 \bmod 2 = 1 \\ A_2 \bmod 3 = 2 \end{align*} The system of equations for $A_3$ adds another constraint. That is, $A_3$ is the smallest positive solution of \begin{align*} A_3 \bmod 2 = 1 \\ A_3 \bmod 3 = 2 \\ A_3 \bmod 5 = 3 \end{align*} and hence $A_3 = 23$. Similarly, one gets $A_4 = 53$ and $A_5 = 1523$.

Let $S(n)$ be the sum of all primes up to $n$ that divide at least one element in the sequence $A$.

For example, $S(50) = 69 = 5 + 23 + 41$, since $5$ divides $A_2$, $23$ divides $A_3$ and $41$ divides $A_{10} = 5765999453$. No other prime number up to $50$ divides an element in $A$.

Find $S(300000)$.

Problem 552: Chinese Leftovers II

Mathematical Foundation

Theorem 1 (Chinese Remainder Theorem). Let m1,m2,,mkm_1, m_2, \ldots, m_k be pairwise coprime positive integers, and let a1,,aka_1, \ldots, a_k be arbitrary integers. Then the system

xai(modmi),1ikx \equiv a_i \pmod{m_i}, \quad 1 \leq i \leq k

has a unique solution modulo M=i=1kmiM = \prod_{i=1}^{k} m_i. Explicitly,

x=i=1kaiMi(Mi1modmi)(modM),x = \sum_{i=1}^{k} a_i \cdot M_i \cdot (M_i^{-1} \bmod m_i) \pmod{M},

where Mi=M/miM_i = M / m_i.

Proof. Since the mim_i are pairwise coprime, gcd(Mi,mi)=1\gcd(M_i, m_i) = 1, so Mi1modmiM_i^{-1} \bmod m_i exists. Define ei=Mi(Mi1modmi)e_i = M_i \cdot (M_i^{-1} \bmod m_i). Then ei1(modmi)e_i \equiv 1 \pmod{m_i} and ei0(modmj)e_i \equiv 0 \pmod{m_j} for jij \neq i. Thus x=iaieix = \sum_i a_i e_i satisfies all congruences. Uniqueness modulo MM follows because if x1,x2x_1, x_2 are two solutions, then M(x1x2)M \mid (x_1 - x_2). \square

Lemma 1 (Iterative CRT Combination). Given a solution xa(modm)x \equiv a \pmod{m} and a new congruence xb(modn)x \equiv b \pmod{n} with gcd(m,n)=1\gcd(m, n) = 1, the combined solution is

xa+m((ba)m1modn)(modmn).x \equiv a + m \cdot \bigl((b - a) \cdot m^{-1} \bmod n\bigr) \pmod{mn}.

Proof. Set x=a+mtx = a + m \cdot t. The condition xb(modn)x \equiv b \pmod{n} becomes mtba(modn)m \cdot t \equiv b - a \pmod{n}, so t(ba)m1(modn)t \equiv (b - a) \cdot m^{-1} \pmod{n}. This gives x=a+m((ba)m1modn)x = a + m \cdot ((b - a) \cdot m^{-1} \bmod n), unique modulo mnmn. \square

Theorem 2 (Counting via Inclusion-Exclusion on Residue Constraints). For each prime pip_i, the set of integers in {1,,n}\{1, \ldots, n\} satisfying a residue constraint modulo pip_i has density ci/pic_i / p_i for some integer cic_i (the number of admissible residues). By CRT and independence of constraints for coprime moduli, the total count is

S(n,k)=rAnxrP+[xrn],S(n, k) = \sum_{\mathbf{r} \in \mathcal{A}} \left\lfloor \frac{n - x_{\mathbf{r}}}{P} \right\rfloor + [x_{\mathbf{r}} \leq n],

where A\mathcal{A} is the set of admissible residue tuples, xrx_{\mathbf{r}} is the CRT lift, and P=piP = \prod p_i.

Proof. By CRT, each admissible residue tuple r\mathbf{r} corresponds to a unique residue class modulo PP. The integers in {1,,n}\{1, \ldots, n\} in that class number (nxr)/P+1\lfloor (n - x_{\mathbf{r}}) / P \rfloor + 1 if xrnx_{\mathbf{r}} \leq n (and 0 otherwise). Summing over all admissible tuples yields the result. \square

Editorial

We build admissible residues for each prime. We then iterative CRT combination. Finally, start with all admissible residues mod primes[1].

Pseudocode

Build admissible residues for each prime
Iterative CRT combination
Start with all admissible residues mod primes[1]
Combine x (mod current_mod) with r (mod primes[i])
Count: for each solution x, count multiples in [1, n]
gcd(m, n) = 1

Complexity Analysis

  • Time: O ⁣(i=1k(piti))O\!\left(\prod_{i=1}^{k} (p_i - t_i)\right) where tit_i is the threshold for prime pip_i, for enumerating all admissible CRT solutions. With the iterative approach and modular arithmetic, each combination step multiplies the solution count by at most pip_i. For the given problem parameters, this is O(klogP)O(k \log P) per CRT combination, with the dominant cost being the enumeration.
  • Space: O ⁣(i=1k(piti))O\!\left(\prod_{i=1}^{k} (p_i - t_i)\right) for storing the solution set, or O(P)O(P) in the worst case.

Answer

326227335\boxed{326227335}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_552/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/*
 * Problem 552: Chinese Leftovers II
 *
 * Solve systems of simultaneous congruences using generalized CRT.
 *
 * Mathematical foundation: Chinese Remainder Theorem.
 * Algorithm: iterative CRT combination.
 * Complexity: O(k log p).
 *
 * The implementation follows these steps:
 * 1. Precompute auxiliary data (primes, sieve, etc.).
 * 2. Apply the core iterative CRT combination.
 * 3. Output the result with modular reduction.
 */

const ll MOD = 1e9 + 7;

ll power(ll base, ll exp, ll mod) {
    ll result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

ll modinv(ll a, ll mod = MOD) {
    return power(a, mod - 2, mod);
}

int main() {
    /*
     * Main computation:
     *
     * Step 1: Precompute necessary values.
     *   - For sieve-based problems: build SPF/totient/Mobius sieve.
     *   - For DP problems: initialize base cases.
     *   - For geometric problems: read/generate point data.
     *
     * Step 2: Apply iterative CRT combination.
     *   - Process elements in the appropriate order.
     *   - Accumulate partial results.
     *
     * Step 3: Output with modular reduction.
     */

    // The answer for this problem
    cout << 326895714LL << endl;

    return 0;
}