All Euler problems
Project Euler

Pizza Toppings

A pizza (a perfect circle) is cut into m * n equal slices. Let f(m,n) denote the number of distinct distributions of m distinct toppings on the pizza, each topping covering exactly n consecutive sl...

Source sync Apr 19, 2026
Problem #0281
Level Level 14
Solved By 1,186
Languages C++, Python
Answer 1485776387445623
Length 413 words
number_theorymodular_arithmeticsearch

Problem Statement

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

You are given a pizza (perfect circle) that has been cut into $m \cdot n$ equal pieces and you want to have exactly one topping on each slice.

Let $f(m, n)$ denote the number of ways you can have toppings on the pizza with $m$ different toppings ($m \ge 2$), using each topping on exactly $n$ slices ($n \ge 1$).

Reflections are considered distinct, rotations are not.

Thus, for instance, $f(2,1) = 1$, $f(2, 2) = f(3, 1) = 2$ and $f(3, 2) = 16$.

$f(3, 2)$ is shown below:

Problem animation

Find the sum of all $f(m, n)$ such that $f(m, n) \le 10^{15}$.

Problem 281: Pizza Toppings

Mathematical Development

Reformulation as a Necklace Counting Problem

The problem is equivalent to counting necklaces of N=mnN = mn beads with mm distinct colors, each color appearing exactly nn times, under the cyclic group CNC_N (rotations only).

Theorem 1 (Burnside Counting Formula)

Theorem. Let N=mnN = mn. Then

f(m,n)=1mntnϕ ⁣(nt)(mt)!(t!)m.f(m,n) = \frac{1}{mn} \sum_{t \mid n} \phi\!\left(\frac{n}{t}\right) \cdot \frac{(mt)!}{(t!)^m}.

Proof. By Burnside’s lemma, the number of orbits of the cyclic group CNC_N acting on the set of valid colorings is

f(m,n)=1Nk=0N1Fix(rk),f(m,n) = \frac{1}{N} \sum_{k=0}^{N-1} |\operatorname{Fix}(r^k)|,

where rkr^k denotes the rotation by kk positions and Fix(rk)\operatorname{Fix}(r^k) is the set of colorings invariant under rkr^k.

Step 1 (Characterizing fixed points). A coloring is fixed by rkr^k if and only if it is periodic with period dividing g=gcd(k,N)g = \gcd(k, N). Such a coloring consists of N/gN/g identical copies of a block of length gg. Each block must contain exactly g/mg/m copies of each of the mm colors. This requires mgm \mid g; when mgm \nmid g, the count Fix(rk)=0|\operatorname{Fix}(r^k)| = 0.

Step 2 (Counting valid blocks). When mgm \mid g, the block of length gg contains g/mg/m copies of each of mm colors. The number of such arrangements is the multinomial coefficient

(gg/m,g/m,,g/m)=g!((g/m)!)m.\binom{g}{g/m, g/m, \ldots, g/m} = \frac{g!}{\bigl((g/m)!\bigr)^m}.

Step 3 (Grouping by gcd). For each divisor gg of NN, the number of integers k{0,1,,N1}k \in \{0, 1, \ldots, N-1\} satisfying gcd(k,N)=g\gcd(k,N) = g is exactly ϕ(N/g)\phi(N/g), where ϕ\phi is Euler’s totient function. Therefore

f(m,n)=1NgNmgϕ ⁣(Ng)g!((g/m)!)m.f(m,n) = \frac{1}{N} \sum_{\substack{g \mid N \\ m \mid g}} \phi\!\left(\frac{N}{g}\right) \cdot \frac{g!}{\bigl((g/m)!\bigr)^m}.

Step 4 (Substitution). Set g=mtg = mt where tt is a positive integer. The condition gN=mng \mid N = mn with mgm \mid g is equivalent to mtmnmt \mid mn, i.e., tnt \mid n. Furthermore, N/g=mn/(mt)=n/tN/g = mn/(mt) = n/t and (g/m)!=t!(g/m)! = t!. Thus

f(m,n)=1mntnϕ ⁣(nt)(mt)!(t!)m.f(m,n) = \frac{1}{mn} \sum_{t \mid n} \phi\!\left(\frac{n}{t}\right) \cdot \frac{(mt)!}{(t!)^m}. \qquad \blacksquare

Lemma 1 (Verification of Given Values)

Lemma. The formula reproduces all four given values.

Proof. Direct computation:

  • f(2,1)=12ϕ(1)2!(1!)2=1212=1f(2,1) = \frac{1}{2}\,\phi(1)\,\frac{2!}{(1!)^2} = \frac{1}{2}\cdot 1 \cdot 2 = 1.
  • f(2,2)=14[ϕ(2)2!(1!)2+ϕ(1)4!(2!)2]=14[12+16]=2f(2,2) = \frac{1}{4}\bigl[\phi(2)\,\frac{2!}{(1!)^2} + \phi(1)\,\frac{4!}{(2!)^2}\bigr] = \frac{1}{4}[1 \cdot 2 + 1 \cdot 6] = 2.
  • f(3,1)=13ϕ(1)3!(1!)3=63=2f(3,1) = \frac{1}{3}\,\phi(1)\,\frac{3!}{(1!)^3} = \frac{6}{3} = 2.
  • f(3,2)=16[ϕ(2)3!(1!)3+ϕ(1)6!(2!)3]=16[6+90]=16f(3,2) = \frac{1}{6}\bigl[\phi(2)\,\frac{3!}{(1!)^3} + \phi(1)\,\frac{6!}{(2!)^3}\bigr] = \frac{1}{6}[6 + 90] = 16. \qquad \blacksquare

Lemma 2 (Search Bounds)

Lemma. For m19m \ge 19 and n1n \ge 1, we have f(m,1)=(m1)!>1015f(m,1) = (m-1)! > 10^{15}. For m=2m = 2 and n30n \ge 30, we have f(2,n)>1015f(2,n) > 10^{15}. Hence it suffices to search 2m182 \le m \le 18 and 1n291 \le n \le 29.

Proof. We have f(m,1)=1mϕ(1)m!(1!)m=(m1)!f(m,1) = \frac{1}{m}\phi(1)\frac{m!}{(1!)^m} = (m-1)!. Since 18!=6402373705728000>101518! = 6402373705728000 > 10^{15}, we need m18m \le 18. For fixed mm, f(m,n)f(m,n) is increasing in nn (the dominant term (mn)!/(n!)m(mn)!/(n!)^m grows super-exponentially), so once f(m,n)>1015f(m,n) > 10^{15} for some nn, all larger nn also exceed the bound. Numerical evaluation confirms n29n \le 29 suffices. \qquad \blacksquare

Editorial

Burnside’s lemma applied to necklaces of mn beads with m colors, each appearing n times, under the cyclic group C_{mn}: f(m,n) = (1/(mn)) * sum_{t | n} phi(n/t) * (mt)! / (t!)^m Sum all f(m,n) <= 10^15 for m >= 2, n >= 1. We iterate over each divisor t of n.

Pseudocode

    total = 0
    for m = 2, 3, ..., 18:
        for n = 1, 2, ..., 29:
            val = f(m, n)
            If val <= 10^15 then
                total += val
    Return total

    result = 0
    for each divisor t of n:
        result += euler_phi(n / t) * (m*t)! / (t!)^m
    Return result / (m * n)

Complexity Analysis

  • Time: The outer loops iterate over at most 17×29=49317 \times 29 = 493 pairs (m,n)(m,n). For each pair, the inner loop iterates over at most d(n)d(29)=2d(n) \le d(29) = 2 divisors of nn (in general, d(n)6d(n) \le 6 for n29n \le 29). Each iteration performs O(mn)O(mn) arithmetic operations for the factorial computation. Since all parameters are bounded by small constants, the total running time is O(1)O(1).
  • Space: O(1)O(1).

Answer

1485776387445623\boxed{1485776387445623}

Code

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

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

typedef long long ll;
typedef __int128 lll;

ll phi(ll n) {
    ll result = n;
    for (ll p = 2; p * p <= n; p++) {
        if (n % p == 0) {
            while (n % p == 0) n /= p;
            result -= result / p;
        }
    }
    if (n > 1) result -= result / n;
    return result;
}

double log_multinomial(int m, int t) {
    double result = 0;
    for (int i = 1; i <= m * t; i++) result += log((double)i);
    for (int i = 1; i <= t; i++) result -= m * log((double)i);
    return result;
}

// Compute (m*t)! / (t!)^m via iterated binomial coefficients
lll exact_multinomial(int m, int t) {
    lll result = 1;
    int total = m * t;
    for (int k = 0; k < m; k++) {
        int remaining = total - k * t;
        for (int i = 0; i < t; i++) {
            result *= (remaining - i);
            result /= (i + 1);
        }
    }
    return result;
}

ll f(int m, int n) {
    ll total_beads = (ll)m * n;
    lll result = 0;
    for (int t = 1; t <= n; t++) {
        if (n % t != 0) continue;
        double log_val = log_multinomial(m, t);
        if (log_val > 45) return -1;
        lll multi = exact_multinomial(m, t);
        result += (lll)phi(n / t) * multi;
    }
    result /= total_beads;
    if (result > (lll)1000000000000000LL) return -1;
    return (ll)result;
}

int main() {
    ll LIMIT = 1000000000000000LL;
    ll total = 0;
    for (int m = 2; m <= 18; m++) {
        for (int n = 1; n <= 29; n++) {
            ll val = f(m, n);
            if (val >= 0 && val <= LIMIT)
                total += val;
        }
    }
    cout << total << endl;
    return 0;
}