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...
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:
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 beads with distinct colors, each color appearing exactly times, under the cyclic group (rotations only).
Theorem 1 (Burnside Counting Formula)
Theorem. Let . Then
Proof. By Burnside’s lemma, the number of orbits of the cyclic group acting on the set of valid colorings is
where denotes the rotation by positions and is the set of colorings invariant under .
Step 1 (Characterizing fixed points). A coloring is fixed by if and only if it is periodic with period dividing . Such a coloring consists of identical copies of a block of length . Each block must contain exactly copies of each of the colors. This requires ; when , the count .
Step 2 (Counting valid blocks). When , the block of length contains copies of each of colors. The number of such arrangements is the multinomial coefficient
Step 3 (Grouping by gcd). For each divisor of , the number of integers satisfying is exactly , where is Euler’s totient function. Therefore
Step 4 (Substitution). Set where is a positive integer. The condition with is equivalent to , i.e., . Furthermore, and . Thus
Lemma 1 (Verification of Given Values)
Lemma. The formula reproduces all four given values.
Proof. Direct computation:
- .
- .
- .
- .
Lemma 2 (Search Bounds)
Lemma. For and , we have . For and , we have . Hence it suffices to search and .
Proof. We have . Since , we need . For fixed , is increasing in (the dominant term grows super-exponentially), so once for some , all larger also exceed the bound. Numerical evaluation confirms suffices.
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 pairs . For each pair, the inner loop iterates over at most divisors of (in general, for ). Each iteration performs arithmetic operations for the factorial computation. Since all parameters are bounded by small constants, the total running time is .
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Project Euler Problem 281: Pizza Toppings
Burnside's lemma applied to necklaces of m*n beads with m colors, each
appearing n times, under the cyclic group C_{m*n}:
f(m,n) = (1/(m*n)) * sum_{t | n} phi(n/t) * (m*t)! / (t!)^m
Sum all f(m,n) <= 10^15 for m >= 2, n >= 1.
"""
from math import factorial
from sympy import totient
def f(m, n):
N = m * n
result = 0
for t in range(1, n + 1):
if n % t == 0:
result += int(totient(n // t)) * factorial(m * t) // factorial(t) ** m
return result // N
LIMIT = 10**15
total = 0
for m in range(2, 19):
for n in range(1, 30):
val = f(m, n)
if val <= LIMIT:
total += val
print(total)
