All Euler problems
Project Euler

Multidimensional Sieve

The Mobius function mu(n) and Mobius inversion extend to multiple dimensions. Given an arithmetic function f(n_1,..., n_k), compute its Mobius inversion efficiently.

Source sync Apr 19, 2026
Problem #0888
Level Level 34
Solved By 221
Languages C++, Python
Answer 227429102
Length 301 words
number_theorylinear_algebramodular_arithmetic

Problem Statement

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

Two players play a game with a number of piles of stones, alternating turns. Each turn a player can choose to remove 1, 2, 4, or 9 stones from a single pile; or alternatively they can choose to split a pile containing two or more stones into two non-empty piles. The winner is the player who removes the last stone.

A collection of piles is called a losing position if the player to move cannot force a win with optimal play. Define \(S(N, m)\) to be the number of distinct losing positions arising from \(m\) piles of stones where each pile contains from \(1\) to \(N\) stones. Two positions are considered equivalent if they consist of the same pile sizes. That is, the order of the piles does not matter.

You are given \(S(12,4)=204\) and \(S(124,9)=2259208528408\).

Find \(S(12491249,1249)\). Give your answer modulo \(912491249\).

Problem 888: Multidimensional Sieve

Mathematical Analysis

Theorem 1 (Mobius Inversion, 1D)

If g(n)=dnf(d)g(n) = \sum_{d \mid n} f(d), then f(n)=dnμ(n/d)g(d)f(n) = \sum_{d \mid n} \mu(n/d) g(d).

Proof. dnμ(n/d)g(d)=dnμ(n/d)edf(e)=enf(e)d:ednμ(n/d)=enf(e)[ne=1]=f(n)\sum_{d \mid n} \mu(n/d) g(d) = \sum_{d \mid n} \mu(n/d) \sum_{e \mid d} f(e) = \sum_{e \mid n} f(e) \sum_{d: e \mid d \mid n} \mu(n/d) = \sum_{e \mid n} f(e) \cdot [\frac{n}{e} = 1] = f(n)

using dmμ(d)=[m=1]\sum_{d \mid m} \mu(d) = [m = 1]. \square

Theorem 2 (Multidimensional Inversion)

If g(n1,,nk)=d1n1dknkf(d1,,dk)g(n_1, \ldots, n_k) = \sum_{d_1 \mid n_1} \cdots \sum_{d_k \mid n_k} f(d_1, \ldots, d_k), then:

f(n1,,nk)=d1n1dknkμ(n1/d1)μ(nk/dk)g(d1,,dk)f(n_1, \ldots, n_k) = \sum_{d_1 \mid n_1} \cdots \sum_{d_k \mid n_k} \mu(n_1/d_1) \cdots \mu(n_k/d_k) \cdot g(d_1, \ldots, d_k)

Theorem 3 (Inclusion-Exclusion Connection)

For squarefree arguments, Mobius inversion reduces to inclusion-exclusion:

dnμ(d)=[n=1]=SP(n)(1)S\sum_{d \mid n} \mu(d) = [n = 1] = \sum_{S \subseteq P(n)} (-1)^{|S|}

where P(n)P(n) is the set of prime factors of nn.

Theorem 4 (Sieve of Coprime Tuples)

The number of kk-tuples (a1,,ak)(a_1, \ldots, a_k) with 1aiN1 \leq a_i \leq N and gcd(a1,,ak)=1\gcd(a_1, \ldots, a_k) = 1:

Ck(N)=d=1Nμ(d)N/dkC_k(N) = \sum_{d=1}^{N} \mu(d) \lfloor N/d \rfloor^k

Proof. By Mobius inversion on dgcd(a)μ(d)=[gcd(a)=1]\sum_{d \mid \gcd(\mathbf{a})} \mu(d) = [\gcd(\mathbf{a}) = 1]:

Ck(N)=a[gcd=1]=adgcdμ(d)=dμ(d)N/dkC_k(N) = \sum_\mathbf{a} [\gcd = 1] = \sum_\mathbf{a} \sum_{d \mid \gcd} \mu(d) = \sum_d \mu(d) \lfloor N/d \rfloor^k \qquad \square

Concrete Numerical Examples

Mobius Function Values

nnμ(n)\mu(n)Reason
11Empty product
21-1One prime factor
31-1One prime factor
404=224 = 2^2, not squarefree
51-1Prime
616=2×36 = 2 \times 3, two factors
12012=22×312 = 2^2 \times 3
301-130=2×3×530 = 2 \times 3 \times 5, three factors

Coprime Pairs C2(N)C_2(N)

NNC2(N)=dμ(d)N/d2C_2(N) = \sum_d \mu(d) \lfloor N/d \rfloor^2Direct count
111
233
377
51919
106363

Euler Totient via Mobius

ϕ(n)=dnμ(d)(n/d)=npn(11/p)\phi(n) = \sum_{d \mid n} \mu(d) \cdot (n/d) = n \prod_{p \mid n} (1 - 1/p)

nnϕ(n)\phi(n)n(11/p)n \prod(1-1/p)
6261/22/3=26 \cdot 1/2 \cdot 2/3 = 2
124121/22/3=412 \cdot 1/2 \cdot 2/3 = 4
308301/22/34/5=830 \cdot 1/2 \cdot 2/3 \cdot 4/5 = 8

Dirichlet Convolution

The Mobius function is the Dirichlet inverse of the constant function 1(n)=1\mathbf{1}(n) = 1. For arithmetic functions f,gf, g:

(fg)(n)=dnf(d)g(n/d)(f * g)(n) = \sum_{d \mid n} f(d) g(n/d)

Key identities:

  • μ1=ε\mu * \mathbf{1} = \varepsilon (identity for Dirichlet convolution)
  • ϕ=μid\phi = \mu * \text{id} (Euler’s totient)
  • σk=idk1\sigma_k = \text{id}_k * \mathbf{1} (divisor power sum)

Mertens Function

M(n)=k=1nμ(k)M(n) = \sum_{k=1}^{n} \mu(k). The Mertens conjecture M(n)<n|M(n)| < \sqrt{n} was disproven by Odlyzko and te Riele (1985), but M(n)=O(n)M(n) = O(\sqrt{n}) is equivalent to the Riemann Hypothesis.

Complexity Analysis

OperationTime
Sieve μ(n)\mu(n) up to NNO(NloglogN)O(N \log \log N)
Ck(N)C_k(N) via Mobius sieveO(N)O(N)
Multidimensional Mobius transformO(NklogN)O(N^k \log N)
Dirichlet convolutionO(NlogN)O(N \log N) or O(NN)O(N \sqrt{N})

Answer

227429102\boxed{227429102}

Code

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

C++ project_euler/problem_888/solution.cpp
/*
 * Problem 888: Multidimensional Sieve
 * Mobius function sieve and coprime tuple counting.
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<int> mobius_sieve(int N) {
    vector<int> mu(N + 1, 0), primes;
    vector<bool> is_prime(N + 1, true);
    mu[1] = 1;
    for (int i = 2; i <= N; i++) {
        if (is_prime[i]) { primes.push_back(i); mu[i] = -1; }
        for (int p : primes) {
            if ((ll)i * p > N) break;
            is_prime[i * p] = false;
            if (i % p == 0) { mu[i * p] = 0; break; }
            else mu[i * p] = -mu[i];
        }
    }
    return mu;
}

ll coprime_pairs(int N, const vector<int>& mu) {
    ll total = 0;
    for (int d = 1; d <= N; d++)
        total += (ll)mu[d] * (N / d) * (N / d);
    return total;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N = 1000;
    auto mu = mobius_sieve(N);

    cout << "=== Mobius Function ===" << endl;
    for (int n = 1; n <= 30; n++)
        cout << "mu(" << n << ") = " << mu[n] << endl;

    cout << "\n=== Coprime Pairs ===" << endl;
    for (int n : {10, 50, 100, 500, 1000})
        cout << "C_2(" << n << ") = " << coprime_pairs(n, mu) << endl;

    cout << "\nAnswer: C_2(1000) = " << coprime_pairs(1000, mu) << endl;
    return 0;
}