All Euler problems
Project Euler

Idempotent Matrices

A matrix M is called idempotent if M^2 = M. Let I(n) be the number of idempotent 3 x 3 matrices whose entries are taken from {0, 1,..., n-1} and arithmetic is performed modulo n. Find sum_(n=2)^200...

Source sync Apr 19, 2026
Problem #0572
Level Level 25
Solved By 410
Languages C++, Python
Answer 19737656
Length 331 words
modular_arithmeticlinear_algebranumber_theory

Problem Statement

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

A matrix \(M\) is called idempotent if \(M^2 = M\).

Let \(M\) be a three by three matrix : \(M=\begin {pmatrix} a & b & c\\ d & e & f\\ g & h & i\\ \end {pmatrix}\).

Let \(C(n)\) be the number of idempotent three by three matrices \(M\) with integer elements such that \[ -n \le a,b,c,d,e,f,g,h,i \le n\] .

You are given \(C(1)=164\) and \(C(2)=848\).

Find \(C(200)\).

Problem 572: Idempotent Matrices

Mathematical Foundation

Theorem 1 (CRT multiplicativity). The function I(n)I(n) is multiplicative over the prime factorization of nn: if n=p1a1pkakn = p_1^{a_1} \cdots p_k^{a_k}, then

I(n)=i=1kI(piai).I(n) = \prod_{i=1}^{k} I(p_i^{a_i}).

Proof. By the Chinese Remainder Theorem, Z/nZi=1kZ/piaiZ\mathbb{Z}/n\mathbb{Z} \cong \prod_{i=1}^{k} \mathbb{Z}/p_i^{a_i}\mathbb{Z} as rings. This isomorphism extends entry-wise to M3(Z/nZ)i=1kM3(Z/piaiZ)M_3(\mathbb{Z}/n\mathbb{Z}) \cong \prod_{i=1}^{k} M_3(\mathbb{Z}/p_i^{a_i}\mathbb{Z}). A matrix MM is idempotent in the product ring if and only if each component is idempotent. Therefore I(n)=iI(piai)I(n) = \prod_{i} I(p_i^{a_i}). \square

Theorem 2 (Count over Fp\mathbb{F}_p). The number of idempotent 3×33 \times 3 matrices over Fp\mathbb{F}_p is

I(p)=r=03(3r)ppr(3r),I(p) = \sum_{r=0}^{3} \binom{3}{r}_p \cdot p^{r(3-r)},

where (3r)p\binom{3}{r}_p is the Gaussian binomial coefficient:

(3r)p=i=0r1(p3pi)i=0r1(prpi).\binom{3}{r}_p = \frac{\prod_{i=0}^{r-1}(p^3 - p^i)}{\prod_{i=0}^{r-1}(p^{r} - p^i)}.

Proof. An idempotent MM3(Fp)M \in M_3(\mathbb{F}_p) of rank rr is a projection onto its image V=Im(M)V = \mathrm{Im}(M), an rr-dimensional subspace, along a complementary (3r)(3-r)-dimensional subspace W=ker(M)W = \ker(M) such that Fp3=VW\mathbb{F}_p^3 = V \oplus W. Choosing VV amounts to choosing an rr-dimensional subspace of Fp3\mathbb{F}_p^3; there are (3r)p\binom{3}{r}_p such subspaces. Given VV, we need a complement WW such that MM acts as the identity on VV and zero on WW. The number of complements to VV in Fp3\mathbb{F}_p^3 is pr(3r)p^{r(3-r)} (each of the 3r3-r basis vectors of WW can be shifted by an arbitrary element of VV, giving prp^r choices per basis vector, but modulo the GL\mathrm{GL} action the count is exactly pr(3r)p^{r(3-r)}). Summing over rr yields the formula. \square

Theorem 3 (Hensel lifting for idempotents). For a prime pp and exponent a1a \geq 1,

I(pa)=r=03(3r)ppr(3r)p(a1)2r(3r).I(p^a) = \sum_{r=0}^{3} \binom{3}{r}_p \cdot p^{r(3-r)} \cdot p^{(a-1) \cdot 2r(3-r)}.

Proof. An idempotent Mˉ\bar{M} of rank rr over Fp\mathbb{F}_p lies on the smooth locus of the idempotent variety {M:M2=M}\{M : M^2 = M\} in M3M_3. The tangent space at Mˉ\bar{M} to this variety has dimension 2r(3r)2r(3-r) (it consists of matrices XX satisfying MˉX+XMˉ=X\bar{M}X + X\bar{M} = X, which in a basis adapted to the decomposition Fp3=Im(Mˉ)ker(Mˉ)\mathbb{F}_p^3 = \mathrm{Im}(\bar{M}) \oplus \ker(\bar{M}) forces XX to have the block form (0AB0)\begin{pmatrix} 0 & A \\ B & 0 \end{pmatrix} with AMr×(3r)A \in M_{r \times (3-r)} and BM(3r)×rB \in M_{(3-r) \times r}, yielding dimension 2r(3r)2r(3-r)). By Hensel’s lemma for smooth varieties, each Fp\mathbb{F}_p-point lifts to exactly p(a1)2r(3r)p^{(a-1) \cdot 2r(3-r)} solutions modulo pap^a. \square

Lemma 1 (Explicit Gaussian binomials for n=3n=3).

(30)p=1,(31)p=p2+p+1,(32)p=p2+p+1,(33)p=1.\binom{3}{0}_p = 1, \quad \binom{3}{1}_p = p^2 + p + 1, \quad \binom{3}{2}_p = p^2 + p + 1, \quad \binom{3}{3}_p = 1.

Proof. Direct computation: (31)p=p31p1=p2+p+1\binom{3}{1}_p = \frac{p^3 - 1}{p - 1} = p^2 + p + 1, and (32)p=(31)p\binom{3}{2}_p = \binom{3}{1}_p by symmetry of Gaussian binomials. \square

Editorial

.200 where I(n) counts 3x3 idempotent matrices mod n. 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

    precompute primes up to N_max via sieve

    Set total <- 0
    For n from 2 to N_max:
        Set factors <- prime_factorization(n)
        Set I_n <- 1
        For each each (p, a) in factors:
            Set I_pa <- 0
            For r from 0 to 3:
                Set gb <- gaussian_binomial(3, r, p)
                Set base <- gb * p^(r * (3 - r))
                Set lift <- p^((a - 1) * 2 * r * (3 - r))
                Set I_pa <- I_pa + base * lift
            Set I_n <- I_n * I_pa
        Set total <- total + I_n

    Return total

    if k == 0 or k == n: return 1
    Set num <- product of (p^(n-i) - 1) for i = 0, ..., k-1
    Set den <- product of (p^(i+1) - 1) for i = 0, ..., k-1
    Return num / den

Complexity Analysis

  • Time: O(Nω(N)4)O(N \cdot \omega(N) \cdot 4) where ω(N)log2N\omega(N) \leq \log_2 N is the number of distinct prime factors. Since N=200N = 200, this is negligible: at most 200×3×4=2400200 \times 3 \times 4 = 2400 arithmetic operations.
  • Space: O(N)O(N) for the sieve of primes; O(1)O(1) auxiliary per factorization.

Answer

19737656\boxed{19737656}

Code

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

C++ project_euler/problem_572/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<vector<ll>> Matrix;

// Gaussian binomial coefficient [n choose k]_q
ll gaussian_binomial(int n, int k, ll q) {
    if (k < 0 || k > n) return 0;
    if (k == 0) return 1;
    ll num = 1, den = 1;
    for (int i = 0; i < k; i++) {
        ll qni = 1, qi1 = 1;
        for (int j = 0; j < n - i; j++) qni *= q;
        for (int j = 0; j < i + 1; j++) qi1 *= q;
        num *= (qni - 1);
        den *= (qi1 - 1);
    }
    return num / den;
}

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

// Count idempotent 3x3 matrices over F_p
ll count_idempotent_prime(ll p, int sz = 3) {
    ll total = 0;
    for (int r = 0; r <= sz; r++) {
        ll gb = gaussian_binomial(sz, r, p);
        total += gb * power(p, r * (sz - r));
    }
    return total;
}

// Count idempotent 3x3 matrices over Z/p^a Z
ll count_idempotent_pp(ll p, int a, int sz = 3) {
    ll total = 0;
    for (int r = 0; r <= sz; r++) {
        ll gb = gaussian_binomial(sz, r, p);
        ll base_count = gb * power(p, r * (sz - r));
        ll lift = power(p, (a - 1) * 2 * r * (sz - r));
        total += base_count * lift;
    }
    return total;
}

map<ll, int> factorize(ll n) {
    map<ll, int> f;
    for (ll d = 2; d * d <= n; d++) {
        while (n % d == 0) { f[d]++; n /= d; }
    }
    if (n > 1) f[n]++;
    return f;
}

int main() {
    ll total = 0;
    for (int n = 2; n <= 200; n++) {
        auto facs = factorize(n);
        ll In = 1;
        for (auto &[p, a] : facs) {
            In *= count_idempotent_pp(p, a);
        }
        total += In;
    }
    cout << total << endl;
    return 0;
}