Project Euler Problem 475

12n musicians participate at a music festival.

Project Euler Problem 475

Solution

Answer: 75780067

Let the $12n$ musicians be divided into $3n$ fixed quartets from day 1:

$$Q_1,Q_2,\dots,Q_{3n}, \qquad |Q_i|=4.$$

On day 2 we must partition all musicians into $4n$ unordered trios such that no trio contains two members from the same quartet.

We seek

$$f(12n).$$


1. Mathematical analysis

Step 1: Start from all trio partitions

The number of ways to partition $12n$ labeled people into $4n$ unordered trios is

$$\frac{(12n)!}{(3!)^{4n}(4n)!} = \frac{(12n)!}{6^{4n}(4n)!}.$$

Now impose the restrictions.


Step 2: Inclusion–exclusion

Inside each quartet there are

$$\binom{4}{2}=6$$

forbidden pairs.

Let an event $E_{ab}$ mean:

musicians $a,b$ (from the same quartet) appear in the same trio.

We apply inclusion–exclusion over all forbidden pairs.


Step 3: What subsets of forbidden pairs are feasible?

Fix one quartet with vertices ${1,2,3,4}$.

We choose a subset $S$ of its 6 edges.

A selected edge means that pair must lie in a common trio.

Since a trio has size 3, every connected component of the graph $({1,2,3,4},S)$ must have size at most 3.

The only feasible graph types are:

Type Count Sign Effect
empty 1 $+$ nothing
one edge 6 $-$ one forced pair
two disjoint edges 3 $+$ two forced pairs
connected 3-vertex graph with 2 edges 12 $+$ one forced triple
triangle 4 $-$ one forced triple

The last two both force the same thing: all 3 vertices must lie in one trio.

Thus the net contribution for one quartet is

$$P(x,y)=1-6x+3x^2+8y,$$

where

  • $x$ counts a forced pair-component,
  • $y$ counts a forced triple-component.

Since quartets are independent, for all $3n$ quartets:

$$P(x,y)^{3n}.$$

Let

$$[x^a y^b]P(x,y)^{3n}=c_{a,b}.$$

Then:

  • $a$ = number of forced pairs,
  • $b$ = number of forced triples.

Step 4: Count completions for fixed $(a,b)$

A forced triple already occupies one trio.

A forced pair must be completed by one additional singleton.

After fixing:

  • $a$ pair-components,
  • $b$ triple-components,

the remaining number of free singletons is

$$12n-2a-3b.$$

We must choose distinct singleton partners for the $a$ pairs:

$$\frac{(12n-2a-3b)!}{(12n-3a-3b)!}.$$

The remaining

$$12n-3a-3b = 3(4n-a-b)$$

singletons are partitioned into

$$4n-a-b$$

unordered trios:

$$\frac{(12n-3a-3b)!}{6^{4n-a-b}(4n-a-b)!}.$$

Multiplying:

$$\frac{(12n-2a-3b)!}{6^{4n-a-b}(4n-a-b)!}.$$

Therefore

$$\boxed{ f(12n)= \sum_{a,b} c_{a,b} \frac{(12n-2a-3b)!} {6^{,4n-a-b}(4n-a-b)!} }$$

where

$$c_{a,b}=x^a y^b^{3n}.$$


2. Python implementation

MOD = 1_000_000_007

def solve(n):
    # DP for coefficients of:
    # (1 - 6x + 3x^2 + 8y)^(3n)

    from collections import defaultdict

    dp = {(0, 0): 1}

    for _ in range(3 * n):
        ndp = defaultdict(int)

        for (a, b), v in dp.items():

            # 1
            ndp[(a, b)] = (ndp[(a, b)] + v) % MOD

            # -6x
            ndp[(a + 1, b)] = (ndp[(a + 1, b)] - 6 * v) % MOD

            # +3x^2
            ndp[(a + 2, b)] = (ndp[(a + 2, b)] + 3 * v) % MOD

            # +8y
            ndp[(a, b + 1)] = (ndp[(a, b + 1)] + 8 * v) % MOD

        dp = ndp

    # factorials
    N = 12 * n

    fact = [1] * (N + 1)
    for i in range(1, N + 1):
        fact[i] = fact[i - 1] * i % MOD

    invfact = [1] * (4 * n + 1)

    invfact[4 * n] = pow(fact[4 * n], MOD - 2, MOD)

    for i in range(4 * n, 0, -1):
        invfact[i - 1] = invfact[i] * i % MOD

    inv6 = pow(6, MOD - 2, MOD)

    pow_inv6 = [1] * (4 * n + 1)
    for i in range(1, 4 * n + 1):
        pow_inv6[i] = pow_inv6[i - 1] * inv6 % MOD

    ans = 0

    for (a, b), c in dp.items():

        m = 12 * n - 2 * a - 3 * b
        k = 4 * n - a - b

        if m < 0 or k < 0:
            continue

        term = c
        term %= MOD

        term *= fact[m]
        term %= MOD

        term *= pow_inv6[k]
        term %= MOD

        term *= invfact[k]
        term %= MOD

        ans += term
        ans %= MOD

    return ans % MOD

# Checks from the problem statement
print(solve(1))   # 576
print(solve(2))   # 509089824

# Required answer
print(solve(50))

3. Code walkthrough

Polynomial DP

We build coefficients of

$$(1-6x+3x^2+8y)^{3n}$$

using dynamic programming.

Each quartet contributes one factor.

The state:

dp[(a,b)]

stores the coefficient of $x^a y^b$.


Factorials

We need terms of the form

$$\frac{m!}{6^k k!}.$$

Modulo arithmetic uses Fermat inverses because

$$10^9+7$$

is prime.


Final summation

For every coefficient $c_{a,b}$, we add

$$c_{a,b} \cdot \frac{(12n-2a-3b)!} {6^{4n-a-b}(4n-a-b)!}.$$


4. Verification

The program reproduces both given values:

$$f(12)=576$$

and

$$f(24)\bmod 10^9+7 = 509089824.$$

So the derivation is consistent.

Running the computation for $n=50$ gives:

$$f(600)\bmod 1,000,000,007 = 75780067.$$


Answer: 75780067