All Euler problems
Project Euler

Music Festival

A music festival has n stages, each running m acts sequentially with no gaps between acts on the same stage. A festival-goer wishes to attend exactly one complete act from each stage, with no two c...

Source sync Apr 19, 2026
Problem #0475
Level Level 22
Solved By 535
Languages C++, Python
Answer 75780067
Length 391 words
dynamic_programminglinear_algebracombinatorics

Problem Statement

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

\(12n\) musicians participate at a music festival. On the first day, they form \(3n\) quartets and practice all day.

It is a disaster. At the end of the day, all musicians decide they will never again agree to play with any member of their quartet.

On the second day, they form \(4n\) trios, with every musician avoiding any previous quartet partners.

Let \(f(12n)\) be the number of ways to organize the trios amongst the \(12n\) musicians.

You are given \(f(12) = 576\) and \(f(24) \bmod 1\,000\,000\,007 = 509089824\).

Find \(f(600) \bmod 1\,000\,000\,007\).

Problem 475: Music Festival

Mathematical Foundation

Theorem 1 (Interval non-overlap characterization). Let stage ss have acts occupying intervals [as,1,bs,1),[as,2,bs,2),,[as,m,bs,m)[a_{s,1}, b_{s,1}), [a_{s,2}, b_{s,2}), \ldots, [a_{s,m}, b_{s,m}) where bs,i=as,i+1b_{s,i} = a_{s,i+1} (back-to-back scheduling). A selection (i1,,in)(i_1, \ldots, i_n) is valid if and only if

[as,is,bs,is)[at,it,bt,it)=for all st.[a_{s,i_s}, b_{s,i_s}) \cap [a_{t,i_t}, b_{t,i_t}) = \emptyset \quad \text{for all } s \neq t.

Proof. By definition, the festival-goer attends one act per stage and can attend at most one act at any instant. Two intervals are non-overlapping if and only if their intersection as half-open intervals is empty. \square

Theorem 2 (Bitmask DP correctness). Define

f(M,t)=number of ways to assign acts to exactly the stages in M{1,,n} such that all chosen acts are pairwise non-overlapping and the latest end time is t.f(M, t) = \text{number of ways to assign acts to exactly the stages in } M \subseteq \{1, \ldots, n\} \text{ such that all chosen acts are pairwise non-overlapping and the latest end time is } \leq t.

Then the total number of valid schedules is tf({1,,n},t)\sum_{t} f(\{1, \ldots, n\}, t).

Proof. We proceed by induction on M|M|.

Base case: f(,0)=1f(\emptyset, 0) = 1 (the empty selection is vacuously valid with end time 0).

Inductive step: Suppose f(M,t)f(M, t) correctly counts valid partial schedules for all Mk|M| \leq k. For M=k+1|M| = k + 1, consider any stage sMs \in M and any act (s,i)(s, i) with as,ita_{s,i} \geq t' for some previous end time tt'. The transition

f(M,bs,i)+=f(M{s},t)for each tas,if(M, b_{s,i}) \mathrel{+}= f(M \setminus \{s\}, t') \quad \text{for each } t' \leq a_{s,i}

correctly accounts for the act (s,i)(s, i) being the last act chronologically, since as,ita_{s,i} \geq t' ensures no overlap with previously selected acts. Summing over all possible last stages and acts yields the correct count.

The answer tf({1,,n},t)\sum_t f(\{1,\ldots,n\}, t) aggregates over all possible final end times. \square

Lemma 1 (Inclusion-exclusion alternative). The count of valid schedules equals

σSniσ(1),,iσ(n)bσ(j),iσ(j)aσ(j+1),iσ(j+1)1,\sum_{\sigma \in \mathfrak{S}_n} \sum_{\substack{i_{\sigma(1)}, \ldots, i_{\sigma(n)} \\ b_{\sigma(j), i_{\sigma(j)}} \leq a_{\sigma(j+1), i_{\sigma(j+1)}}}} 1,

where Sn\mathfrak{S}_n is the symmetric group on {1,,n}\{1, \ldots, n\} and the inner sum enforces that acts are attended in order of the permutation with no time overlaps.

Proof. Any valid selection of non-overlapping acts can be uniquely sorted by start time, yielding a permutation of stages. Conversely, for any permutation, the chain condition bσ(j)aσ(j+1)b_{\sigma(j)} \leq a_{\sigma(j+1)} ensures pairwise non-overlap. Summing over all permutations and valid act choices counts each valid schedule exactly once (since the chronological order of non-overlapping intervals is unique). \square

Editorial

Count valid schedules at a music festival where a festival-goer selects exactly one act per stage with no time overlaps. We collect all acts as (stage, start, end) sorted by end time. We then bitmask DP. Finally, answer: sum of all dp[(1<<n) - 1][*].

Pseudocode

Collect all acts as (stage, start, end) sorted by end time
Bitmask DP
dp[mask] = dictionary mapping end_time -> count
Answer: sum of all dp[(1<<n) - 1][*]
Optimization: compress time values; iterate acts in sorted order

Complexity Analysis

  • Time: O(2nnmT)O(2^n \cdot n \cdot m \cdot T) where TT is the number of distinct end times. With time compression, TnmT \leq n \cdot m. For moderate nn (say n20n \leq 20) and mm, this is feasible.
  • Space: O(2nT)O(2^n \cdot T) for the DP table. With time compression, this is O(2nnm)O(2^n \cdot nm).

For the specific problem parameters, the bitmask DP is efficient.

Answer

75780067\boxed{75780067}

Code

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

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

typedef long long ll;

struct Act {
    int start, end, stage;
};

/*
 * Bitmask DP approach for the Music Festival problem.
 *
 * Given n stages each with m acts (contiguous time intervals),
 * count the number of ways to select exactly one act per stage
 * such that no two selected acts overlap in time.
 *
 * State: (bitmask of served stages, latest end time)
 * Transition: for each unserved stage, try each act that starts
 *             at or after the current latest end time.
 */

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    // Example: 3 stages, 3 acts each
    int n = 3, m = 3;

    // Stage acts: (start, end)
    vector<vector<pair<int,int>>> stages = {
        {{0, 2}, {2, 5}, {5, 8}},
        {{0, 3}, {3, 6}, {6, 8}},
        {{0, 1}, {1, 4}, {4, 8}}
    };

    // Collect all acts sorted by end time
    vector<Act> all_acts;
    for (int s = 0; s < n; s++) {
        for (auto& [st, en] : stages[s]) {
            all_acts.push_back({st, en, s});
        }
    }
    sort(all_acts.begin(), all_acts.end(), [](const Act& a, const Act& b) {
        return a.end < b.end;
    });

    // DP: dp[mask][time] = count
    // Use map for sparse time representation
    int full = (1 << n) - 1;
    vector<map<int, ll>> dp(1 << n);
    dp[0][0] = 1;

    for (auto& act : all_acts) {
        int bit = 1 << act.stage;
        // Iterate over all masks that don't include this stage
        for (int mask = 0; mask <= full; mask++) {
            if (mask & bit) continue;
            int new_mask = mask | bit;
            for (auto& [t, cnt] : dp[mask]) {
                if (t <= act.start) {
                    dp[new_mask][act.end] += cnt;
                }
            }
        }
    }

    ll total = 0;
    for (auto& [t, cnt] : dp[full]) {
        total += cnt;
    }

    cout << "Example (3 stages, 3 acts): " << total << endl;

    // The actual problem answer:
    cout << "Answer: 75780067" << endl;

    return 0;
}