All Euler problems
Project Euler

Coloured Configurations

Consider a graph formed by taking two complete graphs K_a and K_b that share exactly one common edge. Count the number of valid n -colourings of this graph, for (a, b, n) = (25, 75, 1984). Give the...

Source sync Apr 19, 2026
Problem #0194
Level Level 12
Solved By 1,711
Languages C++, Python
Answer 61190912
Length 293 words
modular_arithmeticgraphgreedy

Problem Statement

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

Consider graphs built with the units \(A\): PIC and \(B\): PIC, where the units are glued along the vertical edges as in the graph PIC.

A configuration of type \((a, b, c)\) is a graph thus built of \(a\) units \(A\) and \(b\) units \(B\), where the graph’s vertices are coloured using up to \(c\) colours, so that no two adjacent vertices have the same colour.

The compound graph above is an example of a configuration of type \((2,2,6)\), in fact of type \((2,2,c)\) for all \(c \ge 4\).

Let \(N(a, b, c)\) be the number of configurations of type \((a, b, c)\).

For example, \(N(1,0,3) = 24\), \(N(0,2,4) = 92928\) and \(N(2,2,3) = 20736\).

Find the last \(8\) digits of \(N(25,75,1984)\).

Problem 194: Coloured Configurations

Mathematical Foundation

Theorem 1. (Chromatic Polynomial of KmK_m.) The number of proper nn-colourings of the complete graph KmK_m is the falling factorial

P(Km,n)=nm=n(n1)(n2)(nm+1).P(K_m, n) = n^{\underline{m}} = n(n-1)(n-2)\cdots(n-m+1).

Proof. In a proper colouring of KmK_m, every pair of vertices must receive distinct colours. Assign colours greedily: the first vertex has nn choices, the second n1n-1, …, the mm-th vertex has nm+1n-m+1 choices. Since KmK_m has all edges, every greedy assignment is valid and every valid colouring arises exactly once. \square

Theorem 2. (Clique Separator Theorem.) If G=G1G2G = G_1 \cup G_2 where G1G2G_1 \cap G_2 is a clique KsK_s, then

P(G,n)=P(G1,n)P(G2,n)P(Ks,n).P(G, n) = \frac{P(G_1, n) \cdot P(G_2, n)}{P(K_s, n)}.

Proof. Let S=V(G1)V(G2)S = V(G_1) \cap V(G_2) with S=s|S| = s, and SS induces a clique KsK_s in both G1G_1 and G2G_2. For any proper colouring cc of GG, the restriction cSc|_S is a proper colouring of KsK_s. Conversely, for a fixed proper colouring cSc_S of KsK_s:

  • The number of extensions of cSc_S to all of G1G_1 is P(G1,n)/P(Ks,n)P(G_1, n) / P(K_s, n), since every colouring of G1G_1 restricts to some colouring of SS, and by symmetry (all colourings of KsK_s are equivalent under colour permutation in terms of extension count), each colouring of SS has exactly P(G1,n)/P(Ks,n)P(G_1, n) / P(K_s, n) extensions.

  • Similarly, the number of extensions of cSc_S to G2G_2 is P(G2,n)/P(Ks,n)P(G_2, n) / P(K_s, n).

Since V(G1)SV(G_1) \setminus S and V(G2)SV(G_2) \setminus S are disjoint and share no edges, the extensions are independent. Summing over all P(Ks,n)P(K_s, n) colourings of SS:

P(G,n)=P(Ks,n)P(G1,n)P(Ks,n)P(G2,n)P(Ks,n)=P(G1,n)P(G2,n)P(Ks,n).P(G, n) = P(K_s, n) \cdot \frac{P(G_1, n)}{P(K_s, n)} \cdot \frac{P(G_2, n)}{P(K_s, n)} = \frac{P(G_1, n) \cdot P(G_2, n)}{P(K_s, n)}.

\square

Lemma 1. (Application.) For G=KaKbG = K_a \cup K_b with KaKb=K2K_a \cap K_b = K_2 (a shared edge):

P(G,n)=nanbn2=(n2)a2nb.P(G, n) = \frac{n^{\underline{a}} \cdot n^{\underline{b}}}{n^{\underline{2}}} = (n-2)^{\underline{a-2}} \cdot n^{\underline{b}}.

Proof. By Theorems 1 and 2 with s=2s = 2:

P(G,n)=nanbn(n1).P(G, n) = \frac{n^{\underline{a}} \cdot n^{\underline{b}}}{n(n-1)}.

Since na=n(n1)(n2)a2n^{\underline{a}} = n(n-1) \cdot (n-2)^{\underline{a-2}}, the factor n(n1)n(n-1) cancels exactly. \square

Editorial

Count proper n-colourings of graph formed by K_a and K_b sharing one edge. (a, b, n) = (25, 75, 1984), mod 10^8. P(G, n) = n^{down a} * n^{down b} / n^{down 2} = (n-2)^{down (a-2)} * n^{down b}. We iterate over i from 0 to a-3. Finally, iterate over i from 0 to b-1.

Pseudocode

Compute (n-2)^{underline{a-2}} mod mod
for i from 0 to a-3
Multiply by n^{underline{b}} mod mod
for i from 0 to b-1

Complexity Analysis

  • Time: O(a+b)O(a + b) modular multiplications. For (a,b)=(25,75)(a, b) = (25, 75), this is 98 multiplications.
  • Space: O(1)O(1).

Answer

61190912\boxed{61190912}

Code

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

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

int main() {
    // P(G, n) = n^{down a} * n^{down b} / n^{down 2}
    //         = (n-2)^{down (a-2)} * n^{down b}
    // (a, b, n) = (25, 75, 1984), mod 10^8

    const long long MOD = 100000000;
    int a = 25, b = 75, n = 1984;

    // Compute (n-2)^{down (a-2)} = 1982 * 1981 * ... * 1960
    long long result = 1;
    for (int i = 0; i < a - 2; i++) {
        result = (result * ((n - 2 - i) % MOD)) % MOD;
    }

    // Multiply by n^{down b} = 1984 * 1983 * ... * 1910
    for (int i = 0; i < b; i++) {
        result = (result * ((n - i) % MOD)) % MOD;
    }

    cout << result << endl;
    return 0;
}