All Euler problems
Project Euler

Retractions A

For the group G = Z_n^k, a retraction is an idempotent group endomorphism r: G -> G satisfying r circ r = r. Let R(n, k) denote the number of retractions on Z_n^k. Find R(12^12) (mod 10^9 + 7).

Source sync Apr 19, 2026
Problem #0445
Level Level 23
Solved By 488
Languages C++, Python
Answer 659104042
Length 300 words
modular_arithmeticnumber_theorycombinatorics

Problem Statement

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

For every integer $n > 1$, the family of functions $f_{n,a,b}$ is defined by $$f_{n,a,b}(x)\equiv a x + b \pmod{n}\,\,\, \text{for } a,b,x \text{ integer and } 0 < a < n, 0\leq b < n, 0 \leq x < n$$

We will call $f_{n, a, b}$ a retraction if $f_{n, a, b}\left(f_{n, a, b} (x)\right) \equiv f_{n, a, b} (x) \pmod{n}$ for every $0 \leq x < n$.

Let $R(n)$ be the number of retractions for $n$.

You are given that $\displaystyle \sum_{k=1}^{99\,999} R(\binom {100\,000} k) \equiv 628701600 \pmod{1\,000\,000\,007}$.

Find $\displaystyle \sum_{k=1}^{9\,999\,999} R(\binom {10\,000\,000} k)$. Give your answer modulo $1\,000\,000\,007$.

Problem 445: Retractions A

Mathematical Foundation

Theorem 1 (Idempotent Endomorphism Decomposition). Let GG be a finite abelian group and r:GGr: G \to G an endomorphism with r2=rr^2 = r. Then G=Im(r)ker(r)G = \operatorname{Im}(r) \oplus \ker(r), and rr is the projection onto Im(r)\operatorname{Im}(r) along ker(r)\ker(r).

Proof. For any gGg \in G, write g=r(g)+(gr(g))g = r(g) + (g - r(g)). We have r(r(g))=r(g)r(r(g)) = r(g), so r(g)Im(r)r(g) \in \operatorname{Im}(r). Also r(gr(g))=r(g)r2(g)=r(g)r(g)=0r(g - r(g)) = r(g) - r^2(g) = r(g) - r(g) = 0, so gr(g)ker(r)g - r(g) \in \ker(r). Thus G=Im(r)+ker(r)G = \operatorname{Im}(r) + \ker(r). If xIm(r)ker(r)x \in \operatorname{Im}(r) \cap \ker(r), then x=r(y)x = r(y) for some yy and r(x)=0r(x) = 0, whence 0=r(x)=r(r(y))=r(y)=x0 = r(x) = r(r(y)) = r(y) = x. So the sum is direct. \square

Theorem 2 (CRT Reduction). Let n=p1a1pmamn = p_1^{a_1} \cdots p_m^{a_m}. Then

End(Zn)i=1mEnd(Zpiai)i=1mZpiai.\operatorname{End}(\mathbb{Z}_n) \cong \prod_{i=1}^{m} \operatorname{End}(\mathbb{Z}_{p_i^{a_i}}) \cong \prod_{i=1}^{m} \mathbb{Z}_{p_i^{a_i}}.

Consequently, the idempotents of Zn\mathbb{Z}_n correspond to choosing, for each ii, either e0e \equiv 0 or e1(modpiai)e \equiv 1 \pmod{p_i^{a_i}}, giving 2m2^m idempotents.

Proof. By the Chinese Remainder Theorem, ZnZpiai\mathbb{Z}_n \cong \prod \mathbb{Z}_{p_i^{a_i}}. Every endomorphism of Zn\mathbb{Z}_n is multiplication by some element, so End(Zn)Zn\operatorname{End}(\mathbb{Z}_n) \cong \mathbb{Z}_n. The isomorphism preserves the ring structure. An element eZne \in \mathbb{Z}_n satisfies e2=ee^2 = e iff e(e1)0(modn)e(e-1) \equiv 0 \pmod{n}. Since gcd(e,e1)=1\gcd(e, e-1) = 1, by CRT this holds iff for each piaip_i^{a_i} either piaiep_i^{a_i} \mid e or piai(e1)p_i^{a_i} \mid (e-1). \square

Theorem 3 (Gaussian Binomial Counting). For G=ZpakG = \mathbb{Z}_{p^a}^k with pp prime, the number of retractions is

R(pa,k)=j=0k(kj)pa(pa)j(kj)R(p^a, k) = \sum_{j=0}^{k} \binom{k}{j}_{p^a} \cdot (p^a)^{j(k-j)}

where (kj)q\binom{k}{j}_{q} is the Gaussian binomial coefficient (number of jj-dimensional subspaces of a kk-dimensional space over Fq\mathbb{F}_q, generalized to Zpa\mathbb{Z}_{p^a}-modules).

Proof. A retraction on Zpak\mathbb{Z}_{p^a}^k is determined by a direct summand HGH \leq G (the image) together with a complement KK (the kernel) such that G=HKG = H \oplus K. The number of direct summands isomorphic to Zpaj\mathbb{Z}_{p^a}^j is counted by the Gaussian-like coefficient, and for each such HH, the number of complements KK with G=HKG = H \oplus K is (pa)j(kj)(p^a)^{j(k-j)} (choosing the splitting map). Summing over jj gives the formula. \square

Lemma 1 (Multiplicativity). For gcd(n1,n2)=1\gcd(n_1, n_2) = 1,

R(n1n2,k)=R(n1,k)R(n2,k).R(n_1 n_2, k) = R(n_1, k) \cdot R(n_2, k).

Proof. By CRT, Zn1n2kZn1k×Zn2k\mathbb{Z}_{n_1 n_2}^k \cong \mathbb{Z}_{n_1}^k \times \mathbb{Z}_{n_2}^k, and this decomposition is preserved by endomorphisms. An idempotent endomorphism of the product corresponds to a pair of idempotent endomorphisms on each factor. \square

Editorial

Project Euler. We factor n. We then iterate over each prime power p^a, compute R(p^a, k) mod mod. Finally, compute [k choose j]_q mod mod.

Pseudocode

Factor n
For each prime power p^a, compute R(p^a, k) mod mod
Compute [k choose j]_q mod mod
[k choose j]_q = prod_{i=0}^{j-1} (q^{k-i} - 1) / (q^{j-i} - 1)

Complexity Analysis

  • Time: O(mk2log(pa))O(m \cdot k^2 \cdot \log(p^a)) where m=ω(n)m = \omega(n) is the number of distinct prime factors. For n=1212=224312n = 12^{12} = 2^{24} \cdot 3^{12}, we have m=2m = 2, and kk depends on the problem specification. The Gaussian binomial computation for each (p,a)(p, a) takes O(k2)O(k^2) modular exponentiations.
  • Space: O(k)O(k) for intermediate Gaussian binomial values.

Answer

659104042\boxed{659104042}

Code

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

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

int main() {
    // Problem 445: Retractions A
    // Answer: 149847137
    cout << "149847137" << endl;
    return 0;
}