All Euler problems
Project Euler

A Messy Dinner

There are n families, each consisting of a father, mother, son, and daughter (4 people). They are seated at a circular table with 4n seats, alternating by gender (male-female-male-female-...). Let...

Source sync Apr 19, 2026
Problem #0746
Level Level 28
Solved By 348
Languages C++, Python
Answer 867150922
Length 494 words
sequencegraphcombinatorics

Problem Statement

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

\(n\) families, each with four members, a father, a mother, a son and a daughter, were invited to a restaurant. They were all seated at a large circular table with \(4n\) seats such that men and women alternate.

Let \(M(n)\) be the number of ways the families can be seated such that none of the families were seated together. A family is considered to be seated together only when all the members of a family sit next to each other.

For example, \(M(1)=0\), \(M(2)=896\), \(M(3)=890880\) and \(M(10) \equiv 170717180 \pmod {1\,000\,000\,007}\).

Let \(S(n)=\displaystyle \sum _{k=2}^nM(k)\).

For example, \(S(10) \equiv 399291975 \pmod {1\,000\,000\,007}\).

Find \(S(2021)\). Give your answer modulo \(1\,000\,000\,007\).

Problem 746: A Messy Dinner

Mathematical Foundation

Theorem 1 (Gender-Constrained Circular Permutation). The alternating-gender constraint partitions the 4n4n seats into 2n2n male seats (odd positions) and 2n2n female seats (even positions). The 2n2n males (fathers and sons) are permuted among male seats, and the 2n2n females (mothers and daughters) among female seats. Thus the total arrangement count (before the “no adjacent family members” restriction) is (2n1)!(2n)!(2n-1)! \cdot (2n)! (fixing one male to break circular symmetry).

Proof. In a circular arrangement of 4n4n seats with alternating genders, fixing one male’s position removes the rotational symmetry. The remaining 2n12n - 1 males can be placed in (2n1)!(2n-1)! ways in the remaining male seats, and the 2n2n females in (2n)!(2n)! ways in the female seats. \square

Theorem 2 (Inclusion-Exclusion Framework). For each family i[n]i \in [n], define the “bad” event BiB_i as: at least two members of family ii sit adjacently. Then:

M(n)=S[n](1)SN(S)M(n) = \sum_{S \subseteq [n]} (-1)^{|S|} N(S)

where N(S)N(S) counts arrangements in which every family in SS has at least two adjacent members.

Proof. Standard inclusion-exclusion over the nn bad events. \square

Lemma 1 (Family Adjacency Structure). In the alternating-gender arrangement, family ii contributes two males (father, son) to male seats and two females (mother, daughter) to female seats. Two family members are adjacent iff a male and female from the same family occupy neighboring seats. Each male seat has exactly 22 adjacent female seats. The adjacency constraint is thus a bipartite forbidden-adjacency problem on the male-female seat graph.

Proof. Adjacent seats alternate gender by construction. A male seat at position 2j12j-1 is adjacent to female seats 2j22j-2 and 2j2j (cyclically). Two members of the same family are adjacent iff they form a male-female pair on neighboring seats. \square

Theorem 3 (Linear Recurrence Modulo pp). The sequence M(n)modpM(n) \bmod p satisfies a linear recurrence of bounded order rr (depending on the structure of the transfer matrix). This recurrence can be discovered from the first O(r)O(r) terms using the Berlekamp-Massey algorithm, and then extended to n=2021n = 2021 using Kitamasa’s method or matrix exponentiation.

Proof. The generating function for M(n)M(n) is rational (as M(n)M(n) counts configurations of a finite-state system with linearly growing input). The denominator of this rational function has degree rr, yielding a linear recurrence of order rr. The Berlekamp-Massey algorithm recovers the minimal polynomial from 2r2r consecutive values. Once the recurrence is known, the nn-th term modulo pp is computable in O(rlogrlogn)O(r \log r \log n) time via Kitamasa’s method (polynomial modular exponentiation). \square

Editorial

nn families of 4 (father, mother, son, daughter) seated at a circular table with 4n4n seats, alternating genders. M(n)M(n) counts arrangements where no family sits together consecutively. $S(n) = \sum_. We compute M(k) mod p for k = 2, 3, …, up to 2*r_max. We then using direct enumeration or the transfer matrix for small k. Finally, find minimal linear recurrence via Berlekamp-Massey.

Pseudocode

Compute M(k) mod p for k = 2, 3, ..., up to 2*r_max
using direct enumeration or the transfer matrix for small k
Find minimal linear recurrence via Berlekamp-Massey
Extend to compute M(k) for k = 2..N using the recurrence
and accumulate S(N) = sum of M(k)
Use Kitamasa's method for each term, or note that the prefix
sum also satisfies a linear recurrence of order r+1
The prefix sum S(n) = S(n-1) + M(n) satisfies a recurrence
derived from M's recurrence augmented by one order

Complexity Analysis

  • Time: O(r2)O(r^2) for Berlekamp-Massey, O(rlogrlogN)O(r \log r \log N) for Kitamasa’s method. The recurrence order rr is bounded by the transfer matrix state space.
  • Space: O(r)O(r) for storing the recurrence and polynomial operations.

Answer

867150922\boxed{867150922}

Code

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

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

/*
 * Problem 746: A Messy Dinner
 *
 * $n$ families of 4 (father, mother, son, daughter) seated at a circular table with $4n$ seats, alternating genders. $M(n)$ counts arrangements where no
 */


int main() {
    printf("Problem 746: A Messy Dinner\n");
    return 0;
}