# WARNING: The text below provides a guidance for a Project Euler problem.

• If you pride yourself in tackling problems entirely on your own, you are encouraged to exit this post right now.

• If you are simply looking for a hint for the problem, please visit the »Project Euler’s official forum«. You may find some hints here, but if you don’t want your problem spoiled, scroll cautiously.

• If you are looking for a readily available copy/paste answer, you will not find it here. All of the code snippets in the post have been stripped of some crucial lines (these are clearly marked). It is my aim that anyone able to replicate the answer based on such snippets will have to understand the solution conceptually. At that point, by virtue of learning something new, I believe he deserves it anyway.

• Guidance for future problems will not be published before 100 people have solved the problem and there are at least two more recent problems.



### Patterned Cylinders

In the problem number 651, we are interested in the number of colorings $f(m, a, b)$ with exactly $m$ colors of a tiled infinite cylinder whose circumference fits $b$ equally sized tiles. The colorings of the infinite cylinder are, however, restricted to the fact that they should be periodic with period $a$ (along the “infinite direction” of the cylinder). After a little bit of thought, we realize that the colorings that we are looking for are the colorings of a torus which fits $a$ tiles and $b$ tiles respectively across its two defining cycles.

Moreover, two colorings are considered equivalent if they can modified to each other via a combination of rotations and reflections. This is a pretty common theme, in which we recognize that the »Burnside’s lemma« will come handy (recall for example »Problem 626«)!

Crucial realization here is that the reflections and rotations (the symmetries) responsible for two colorings being equivalent form a group $G$. Since any element of this group acting on the colored tiles gives just a permutation of the tiles, $G$ is a subset of the symmetric group $S_{ab}$. Now, using $G$, the Burnside’s lemma gives us precisely what we need.

The »cycle index« of a subgroup $G$ of a symmetric group with degree $n$ is defined to be the following polynomial (in $x_1, x_2, \dots, x_n$):

where $j_k(g)$ is the number of cycles of length $k$ in $g$.

The number of distinct (in the sense described above) colorings of our torus with at most $m$ colors is then, by the Burnside’s lemma

The work that remains now is to find out what the cycle index of $G$ is (or how does $G$ actually look like), and then move from the value when using at most m colors to the value when using exactly m colors. Implicitly hidden in the problem statement, we first realize that we only care about the case where $a$ and $b$ are coprime (indeed, in the problem statement, all of the values $a, b$ are two consecutive Fibonacci numbers, and these are always coprime). It follows that we may apply the reflections and rotations independently on the two defining cycles of the torus, yielding the direct product of »dihedral groups« $G = D_a \times D_b$. We now proceed to find $Z( D_a \times D_b)$.

It is not hard to find in tables/textbooks the cycle index of a dihedral group:

But how do we find the cycle index of a direct product of two permutation groups? The derivation can be found, for example, in the following »paper«. We introduce a peculiar concept of (randomly denoted) $\bowtie$ multiplication of two polynomials.

Then $Z(G_1 \times G_2 \times \cdots \times G_n) = Z(G_1) \bowtie Z(G_2) \bowtie \cdots \bowtie Z(G_n)$, and, in particular:

Gluing everything together, we have found an explicit formula for the number $f'(m, a, b)$ of colourings with at most $m$ colors:

To find the the number $f(m, a, b)$ of colourings with exactly $m$ colors, we simply employ the following recursion:

What remains now is to code up the cumbersome expressions above. In Python 3, these are all of the ingredients that we will need:

• Memoization.
• Factorization of a number.
• Fibonacci numbers.
• GCD and LCM.
• Modular inverse.
• Divisors of a number.
• Binomials.
• Euler’s totient function.
• Bowtie multiplication (here, care needs to be taken to handle fractions – modular inverses).
• Burnside’s lemma
• Cycle indices of dihedral groups.
• Function for $f$.

With the above tower of implemented concepts, PyPy 3 gives us the answer in about 1 second.