All Euler problems
Project Euler

Divisor Pairs

Let Pairs(n) denote the number of ordered pairs (d_1, d_2) of positive divisors of n satisfying d_1 <= d_2 and d_1 * d_2 = n. Define S(N) = sum_(n=1)^N Pairs(n). We seek S(10^10).

Source sync Apr 19, 2026
Problem #0561
Level Level 16
Solved By 929
Languages C++, Python
Answer 452480999988235494
Length 341 words
modular_arithmeticnumber_theorygeometry

Problem Statement

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

Let \(S(n)\) be the number of pairs \((a,b)\) of distinct divisors of \(n\) such that \(a\) divides \(b\).

For \(n=6\) we get the following pairs: \((1,2), (1,3), (1,6),( 2,6)\) and \((3,6)\). So \(S(6)=5\).

Let \(p_m\#\) be the product of the first \(m\) prime numbers, so \(p_2\# = 2*3 = 6\).

Let \(E(m, n)\) be the highest integer \(k\) such that \(2^k\) divides \(S((p_m\#)^n)\).

We can easily verify that \(E(2,1) = 0\) since \(2^0\) is the highest power of 2 that divides S(6)=5.

Let \(Q(n)=\displaystyle \sum _{i=1}^{n} E(904961, i)\). You are given \(Q(8)=2714886\).

Evaluate \(Q(10^{12})\).

Problem 561: Divisor Pairs

Mathematical Foundation

Definition. For a positive integer nn, let τ(n)=dn1\tau(n) = \sum_{d \mid n} 1 denote the number of positive divisors of nn, and let [P][P] denote the Iverson bracket of a predicate PP.

Theorem 1 (Reduction to the Divisor Function). For every positive integer nn,

Pairs(n)=τ(n)2=τ(n)+[n is a perfect square]2.\operatorname{Pairs}(n) = \left\lceil \frac{\tau(n)}{2} \right\rceil = \frac{\tau(n) + [n \text{ is a perfect square}]}{2}.

Proof. The map dn/dd \mapsto n/d is an involution on the divisor set D(n)={dZ>0:dn}D(n) = \{d \in \mathbb{Z}_{>0} : d \mid n\}. This involution partitions D(n)D(n) into:

  • Transposed pairs {d,n/d}\{d, n/d\} with d<n/dd < n/d, i.e., d<nd < \sqrt{n}, and
  • A fixed point {d}\{d\} when d=n/dd = n/d, i.e., n=d2n = d^2.

Each transposed pair {d,n/d}\{d, n/d\} contributes exactly one ordered pair (d,n/d)(d, n/d) with d<n/dn/dd < n/d \le n/d. The fixed point, when it exists, contributes the pair (d,d)(d, d). Therefore

Pairs(n)=τ(n)/2+[n is a perfect square]=τ(n)/2,\operatorname{Pairs}(n) = \lfloor \tau(n)/2 \rfloor + [n \text{ is a perfect square}] = \lceil \tau(n)/2 \rceil,

where the last equality follows because τ(n)\tau(n) is odd if and only if nn is a perfect square. \square

Lemma 1 (Summatory Reduction). We have

S(N)=12 ⁣(n=1Nτ(n)+N).S(N) = \frac{1}{2}\!\left(\sum_{n=1}^{N} \tau(n) + \lfloor \sqrt{N} \rfloor\right).

Proof. Summing Theorem 1 over n=1,,Nn = 1, \ldots, N:

S(N)=n=1Nτ(n)+[n is a perfect square]2=12 ⁣(n=1Nτ(n)+#{kZ>0:k2N}).S(N) = \sum_{n=1}^{N} \frac{\tau(n) + [n \text{ is a perfect square}]}{2} = \frac{1}{2}\!\left(\sum_{n=1}^{N} \tau(n) + \#\{k \in \mathbb{Z}_{>0} : k^2 \le N\}\right).

The count of perfect squares in {1,,N}\{1, \ldots, N\} is N\lfloor \sqrt{N} \rfloor. \square

Theorem 2 (Dirichlet Hyperbola Method). For any positive integer NN with M=NM = \lfloor \sqrt{N} \rfloor,

n=1Nτ(n)=2d=1MNdM2.\sum_{n=1}^{N} \tau(n) = 2\sum_{d=1}^{M} \left\lfloor \frac{N}{d} \right\rfloor - M^2.

Proof. Since τ(n)=dn1\tau(n) = \sum_{d \mid n} 1, we have

n=1Nτ(n)=#{(a,b)Z>02:abN}.\sum_{n=1}^{N} \tau(n) = \#\{(a, b) \in \mathbb{Z}_{>0}^2 : ab \le N\}.

Partition the lattice points (a,b)(a, b) under the hyperbola ab=Nab = N into three regions:

  • R1={(a,b):aM,  bN/a}R_1 = \{(a, b) : a \le M,\; b \le N/a\},
  • R2={(a,b):bM,  aN/b}R_2 = \{(a, b) : b \le M,\; a \le N/b\},
  • R12={(a,b):aM,  bM}R_{12} = \{(a, b) : a \le M,\; b \le M\}.

By symmetry R1=R2=d=1MN/d|R_1| = |R_2| = \sum_{d=1}^{M} \lfloor N/d \rfloor, and R12=M2|R_{12}| = M^2. Since R1R2R_1 \cup R_2 covers all lattice points under the hyperbola and R1R2=R12R_1 \cap R_2 = R_{12}, inclusion-exclusion gives

n=1Nτ(n)=R1+R2R12=2d=1MNdM2.\sum_{n=1}^{N} \tau(n) = |R_1| + |R_2| - |R_{12}| = 2\sum_{d=1}^{M} \left\lfloor \frac{N}{d} \right\rfloor - M^2. \quad \square

Theorem 3 (Block Decomposition for N/d\sum \lfloor N/d \rfloor). The sum d=1MN/d\sum_{d=1}^{M} \lfloor N/d \rfloor can be evaluated in O(N)O(\sqrt{N}) arithmetic operations.

Proof. For each value q=N/dq = \lfloor N/d \rfloor, the set of indices dd yielding this quotient forms a contiguous block {dmin,,dmax}\{d_{\min}, \ldots, d_{\max}\} where dmax=min(N/q,M)d_{\max} = \min(\lfloor N/q \rfloor, M). The contribution of this block is q(dmaxdmin+1)q \cdot (d_{\max} - d_{\min} + 1), computable in O(1)O(1).

It remains to bound the number of distinct quotients. If dNd \le \sqrt{N}, then dd itself ranges over at most N\sqrt{N} values. If d>Nd > \sqrt{N}, then q=N/d<Nq = \lfloor N/d \rfloor < \sqrt{N}, so qq takes at most N\sqrt{N} values. Hence there are at most 2N2\sqrt{N} distinct quotients in total, and the block decomposition runs in O(N)O(\sqrt{N}) time. \square

Corollary. Combining Lemma 1, Theorem 2, and Theorem 3, S(N)S(N) is computable in O(N)O(\sqrt{N}) time and O(1)O(1) space.

Editorial

Compute S(N) = sum_{n=1}^{N} Pairs(n), where Pairs(n) = ceil(tau(n)/2). By Lemma 1: S(N) = (sum_tau + floor(sqrt(N))) / 2. By the Dirichlet hyperbola method: sum_tau = 2 * T - M^2, where T = sum_{d=1}^{M} floor(N/d) and M = floor(sqrt(N)). T is evaluated in O(sqrt(N)) via block decomposition of floor(N/d).

Pseudocode

    M = floor(sqrt(N))
    T = 0
    d = 1
    While d <= M:
        q = floor(N / d)
        d_max = min(floor(N / q), M)
        T += q * (d_max - d + 1)
        d = d_max + 1
    sum_tau = 2 * T - M * M
    Return (sum_tau + M) / 2

Complexity Analysis

  • Time: O(N)O(\sqrt{N}). The block decomposition iterates over O(N)O(\sqrt{N}) distinct quotient values, each processed in O(1)O(1).
  • Space: O(1)O(1). Only a constant number of integer variables are maintained.

Answer

452480999988235494\boxed{452480999988235494}

Code

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

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

/*
 * Problem 561: Divisor Pairs
 *
 * Count ordered pairs of divisors satisfying specific ordering constraints.
 *
 * Mathematical foundation: divisor enumeration and multiplicative functions.
 * Algorithm: Dirichlet convolution.
 * Complexity: O(N log N).
 *
 * The implementation follows these steps:
 * 1. Precompute auxiliary data (primes, sieve, etc.).
 * 2. Apply the core Dirichlet convolution.
 * 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 Dirichlet convolution.
     *   - Process elements in the appropriate order.
     *   - Accumulate partial results.
     *
     * Step 3: Output with modular reduction.
     */

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

    return 0;
}