The Algorithmics of Solitaire-Like Games

One-person solitaire-like games are explored with a view to using them in teaching algorithmic problem solving. The key to understanding solutions to such games is the identiﬁcation of invariant properties of polynomial arithmetic. We demonstrate this via three case studies: solitaire itself, tiling problems and a novel class of one-person games. The known classiﬁcation of states of the game of (peg) solitaire into 16 equivalence classes is used to introduce the relevance of polynomial arithmetic. Then we give a novel algebraic formulation of the solution to a class of tiling problems. Finally, we introduce an inﬁnite class of challenging one-person games, which we call “replacement-set games”, inspired by earlier work by Chen and Backhouse on the relation between cyclotomic polynomials and generalisations of the seven-trees-in-one type isomorphism. We present an algorithm to solve arbitrary instances of replacement-set games and we show various ways of constructing inﬁnite (solvable) classes of replacement-set games.


Introduction
Puzzles and games have been used for centuries to nurture problem-solving skills.Although often presented as isolated brain-teasers, the desire to know how to win makes games ideal examples for teaching algorithmic problem solving.
There are two concepts that are basic to all algorithms that process input values using some sort of iterative or recursive scheme: invariants and making progress.Although in principle making progress can involve quite complicated theories on well-founded relations, in practice the concept is easy for students to grasp.On the other hand, students may sometimes be given misleading information about invariants; they may be taught that invariants are (only) needed for post-hoc verification of program correctness and are very difficult to formulate.In reality, a good understanding of invariants is crucial to successful algorithm design.
In this paper, we study three types of one-person game with a view to using them as an aid to teaching algorithmic problem solving.We set the scene with two well-known examples: (peg) solitaire and a collection of tiling problems.The third type of game we study is novel.It is a class of (one-person) games played on a one-dimensional tape where the objective is to move a checker a certain distance along the tape according to a couple of replacement rules.We develop an algorithm to solve such "replacement-set" games in general and we show how to construct an infinite collection of (solvable) replacement-set games.The common theme in all three types of game is invariants of polynomial arithmetic.
We begin the paper in section 2 with a brief summary of well-known properties of the game of solitaire.These properties are derived using the algebra of polynomials in a suitably chosen semiring; it is this algebra that is the basis for the novel applications that we discuss in later sections.
Section 3 is about a class of tiling problems.In section 3.1, we show how Golomb's [1] use of colours to solve one such problem is formulated algebraically.Our solution is simpler than the algebraic formulation proposed by Mackinnon [2].The solution to the class of tiling problems is discussed in section 3.2.
The so-called "nuclear pennies" game [3] is an example of a game which, until now, has been of isolated interest.The game is based on the theorem attributed to Lawvere that "seven trees are one".That is, if T is the type of binary trees, the type T 7 (the cartesian product of T with itself 7 times) is isomorphic to T .The game involves moving a checker 6 squares to the right on a one-dimensional tape (from square 1 to square 7 ) following rules that reflect the recursive definition of unlabelled binary trees.In section 4, we formulate an infinite collection of games, each with different rules, where the goal is to move a checker a certain number of squares from its starting position on a one-dimensional tape.So far as we are aware, these games and their solution are original to this paper.The games were derived from our study of the problem: given a number n , invent an interesting type T such that T n is isomorphic to T [4].In [4], we showed that all such types are based on products of cyclotomic polynomials.The general class of "replacementset" games is formulated in section 4.2 and an algorithm to solve them is formulated in section 4.3.Methods to construct an infinite collection of solvable replacement-set games are given in section 4.4.These methods are based on the theory of products of cyclotomic polynomials some of which is new to this paper.(Properties of individual cyclotomic polynomials are well-known but properties of products of cyclotomic polynomials of the sort we are interested in do not appear to have been studied elsewhere.)The conclusion (section 5) remarks on some open problems.

Solitaire
Solitaire is a well-known game.The game begins with a number of pegs stuck in holes in a board.The holes are arranged in a grid; the shape of the grid (which varies from country to country) is not relevant to our discussion.A move, shown diagrammatically in fig. 1, replaces two pegs by one and the game is to remove all pegs bar one, leaving the peg in a designated position.In this section, we show how invariants of polynomial arithmetic are used in the analysis of moves.

Equivalent States
De Bruijn [5] shows that states in the game of solitaire can be divided into 16 equivalence classes in such a way that all moves are between equivalent states.(So the equivalence class of the state is an invariant of each move.)Here is a brief reformulation of De Bruijn's argument2 .
Suppose we assign non-negative integer coordinates (i, j) to each hole in the board.Suppose R = (A,0,1,+,•) is a semiring 3 and suppose p is an element of A .Assign to a peg at position (i, j) the weight p i+j .The total weight of a state in the game is the sum of the weights of all the pegs on the board in that state.There are four types of move in the game -vertically up and down, and horizontally left and right.A vertical-up move replaces pegs with weights p i+j+0 and p i+j+1 by a peg with weight p i+j+2 .So, if p has the property that p 0 +p 1 = p 2 , the total weight is invariant.Similarly, a horizontal-left move replaces pegs with weights p i+2+j and p i+1+j by a peg with weight p i+0+j .So, if p 2 +p 1 = p 0 , the total weight remains invariant.A similar analysis applies to the two other types of moves: if p 2 +p 1 = p 0 , the total weight is invariant under vertical-down moves and, if p 0 +p 1 = p 2 , the total weight is invariant under horizontal-right moves.
The carrier set of the field4 GF (4) has exactly 4 elements which can be named 0 , 1 , p and p 2 .(See fig. 2 for the addition and multiplication tables of GF (4) .)Moreover, these elements have the property that 1+1 = 0 , 1+p = p 2 and (hence) p 2 +p = 1 .Thus, if GF (4) is used to compute the weights of states, the weight is invariant under all moves.This divides the states into four equivalence classes, and the initial and final states in the game must be in the same equivalence class.To complete the argument, a symmetrical weighting is used: assign to a peg at position (i, j) the weight p i−j .The total weight is again the sum of the weights of all the pegs on the board in that state.(We call it a "symmetrical" weighting because it is equivalent to turning the board through 90 • .)The same analysis applies, classifying each state into one of 4 equivalence classes.
Combining the two5 , the states are divided into 4×4 equivalence classes in such a way that the equivalence class is invariant under moves.The game can be solved only if the initial and final states are in the same equivalence class.

The Solitaire Army
De Bruijn's weighting of a state in a game does not provide a sufficient condition for when it is possible to move from a given initial state to a given final state.Berlekamp et al [6, chap. 23] discuss in detail a number of problems, when they can be solved and when they cannot be solved.A tool in their analysis is the notion of a pagoda function, which computing scientists would recognise as being similar to a measure of progress.Specifically, a pagoda function is any real-valued function pag on peg-positions that has the property that if a move replaces pegs at positions r and s by a peg at position t then pag.t ≤ pag.s + pag.rA much-celebrated problem -discussed in several other books and Internet pages-which Berlekamp et al [6, chap. 23] solve using a pagoda function is the "solitaire army" problem.This is how they describe the problem.
A number of Solitaire men stand initially on one side of a straight line beyond which is an infinite empty desert.How many men do we need to send a scout just 0 , 1 , 2 , 3 , 4 or 5 paces out into the desert?
The surprising fact is that for 5 paces (or more) there is no solution!The proof [6, chap. 23] uses a weighting function similar to the one used by De Bruijn6 .Suppose peg positions are assigned cartesian coordinates so that the goal position is given the coordinates (0, 0) and the initial positions of the Solitaire men have coordinates (i, j) where i is an arbitrary integer and j ≥ n , for some given number n .Suppose we assign to a peg at position (i, j) the weight σ |i|+|j| , where σ is yet to be chosen.Note that |i|+|j| is the Manhattan distance of (i, j) from (0, 0) .The weight of any state in the game is the sum of the weights of all the pegs on the board in that state.It is easy to calculate that, if all positions (i, j) satisfying j ≥ n are filled by pegs, and 0 ≤ σ < 1 , the weight of the state is σ n ×(1+σ)×(1−σ) −2 .(Note that this requires an infinite number of pegs.) Now σ is chosen so that the weighting of pegs is a pagoda function.Specifically, choose σ = 1 2 ( √ 5 − 1) .This choice guarantees that σ 0 = σ 1 +σ 2 and σ 2 < σ 1 +σ 0 .Thus the weight of a peg that moves towards (0, 0) remains constant and the weight of a peg that moves away from (0, 0) decreases.(Of course, the weight of a peg that moves but remains at the same distance from (0, 0) does not change.)Then, for the 5 -pace problem, the goal is to reach a state with weight at least σ 0+0 (i.e. 1 ) but this is impossible because the weighting is a pagoda function -its value is never increased by a move-and any initial state with a finite number of pegs has total weight strictly less than σ 5 ×(1+σ)×(1−σ) −2 , which simplifies to 1 .
Edsger W. Dijsktra [8] discusses a similar problem (and gives a similar solution).

Tiling Problems
Tiling problems involve covering a board without overlapping with a given collection of tiles.Traditionally their solution involves (seemingly ad hoc) colouring arguments.This section is essentially about how to formulate the colouring arguments algebraically.

The Chessboard Problem
Consider the problem of tiling a chessboard with twenty-one 3×1 rectangles and one 1×1 square.Index each square of the chessboard by a pair of natural numbers (i, j) in the obvious way.For concreteness, we assume that the bottom-left corner is given the label (0, 0) .Suppose a chessboard is partially tiled by 3×1 rectangles.As in De Bruijn's analysis of solitaire, give to the square (i, j) two "weights": the forward weight is p i−j and the backward weight is p i+j , where p is a generator of the field GF (4) .Two weights are assigned to the chessboard as follows: the forward weight of the chessboard is the sum (in GF (4) ) of the forward weights of all the individual squares that are tiled, and the backward weight of the chessboard is the sum of the backward weights of all the individual squares that are tiled.
Recall (fig.2) that the elements of GF (4) are 0 , 1 , p and p 2 and that 1+1 = 0 and 1+p = p 2 .It follows that In particular, p 3 = 1 , the forward weight of square (i, j) is p (i−j) mod 3 and its backward weight is p (i+j) mod 3 .Thus the weights are identical on forward and backward diagonals of the board, respectively.(This is the explanation for our choice of nomenclature.) It is easily checked that when a 3×1 tile is placed on a chessboard, both the forward and backward weights of the chessboard do not change; they are invariants of the tiling process.(For example, if a 3×1 tile is placed horizontally on the board with leftmost square at position (i, j) , the weight p i+j ×(1+p+p 2 ) is added to the weight of the board.Because 1+p+p 2 equals 0 , adding or subtracting this weight has no effect on the total weight.)This is the basis for the choice of GF (4) in weighing squares: it is the simplest possible semiring that satisfies (1) in a non-trivial way.
The forward and backward weights of a completely tiled chessboard are 1 and p , respectively.In order to tile the chessboard completely with twenty-one 3×1 rectangles and one 1×1 square, the 1×1 square must therefore be placed on a square with forward weight 1 and backward weight p .The former are the squares (i, j) with (i−j ≡ 0) mod 3 and the latter are the squares (i, j) with (i+j ≡ 1) mod 3 .Combined with the requirement that i and j are natural numbers each of which is at most 7 , there are just 4 solutions to this pair of equations, namely (i, j) = (2, 2) , (i, j) = (5, 5) , (i, j) = (2, 5) , and (i, j) = (5, 2) .
The above argument is a simpler presentation of an "algebraic" proof given by Mackinnon [2].(Our formulation is simpler because Mackinnon takes for p a complex solution of equation (1)the field of complex numbers is, of course, much more complicated than GF (4) .)If colours -say red, white and blue-are assigned to the non-zero elements of GF (4) , the argument is essentially the same as Golomb's [1] "colouring" proof.Specifically, Golomb's proof has two components, the colouring of the squares and rotational symmetry of the board.The colouring of the squares is just the assignment of three different values to the squares; this is chosen so that the net "count" of colours on the board -whereby three differently coloured squares "count" as zero-is one.The rotational symmetry is expressed algebraically by the two weights given to squares of the board.The colouring and algebraic proofs are thus in essence identical.

The Generalisation
In order to demonstrate the effectiveness of the algebraic formulation, let us consider a generalisation.Suppose we have an m×m board, an unlimited supply of n -ominoes and one 1 -omino.(An n -omino is an n×1 board, i.e. a strip of n squares each of which is the same size as a square of the given m×m board.)In order to eliminate the trivial case, we assume that 1 < m .We prove that it is possible to cover the m×m board with the supplied n -ominoes and one 1 -omino, without overlapping, precisely when 7 An obvious necessary condition is This, however, is not equivalent.For example, it is satisfied by m = 11 and n = 8 but it is not the case that 8\(11−1) ∨ 8\(11+1) .
(3) 7 The " \ " symbol denotes the divides relation on integers (more precisely, the converse of the "is-a-multiple-of" relation).Although the notation m|n is traditional, we prefer to use an asymmetric symbol such as the backward slash to denote an asymmetric relation.As the authors of [9, p. 102] point out, vertical bars are overused and m\n gives an impression that m is the denominator of an implied ratio. 8We use a systematic notation for quantified expressions which has the form L bv: range: term .There are five components to the notation, which we explain in turn.The first component is the quantifier, in this case L .The second component is the dummy bv .The third component is the range of the dummy, a boolean-valued expression that determines a set of values of the dummy.The fourth component is the term.The final component of the notation is the angle brackets; they serve to delimit the scope of the dummy.For more details, see [10, chapter 11] and [11, chapter 8].
(We give examples of such semirings later.The subscript on the equality symbol is necessary later to avoid the confusion that can be caused by overloading.)Now let us assign to each square (i, j) the weight x i+j if it is covered and the weight 0 if it is not covered.The weight of a (partially) tiled board is defined to be the sum of the weights of the tiled squares.
On account of (3) above, the placement of an n -omino on the board does not change the weight of the board.Since there is exactly one 1 -omino on a completely covered board, a necessary condition is that Equivalently (calculation left to the reader), We show that (4) implies n\(m−1) ∨ n\(m+1) .Our calculations exploit the following immediate consequences of (3): and, hence,

Invariant. Polynomials over GF (2)
To complete our argument, we fix the semiring R to be That is, R is the set of polynomials in the indeterminate x with coefficients in GF (2) (which is conventionally denoted by GF (2)[x] ) modulo the polynomial Σi : 0 ≤ i < n : x i .Thus, in R we have the property (3).This choice of R is motivated by our goal.Note first the squaring in (4); GF (2) is the simplest example of a semiring in which squaring distributes through addition.This property is easily seen to be inherited by GF (2)[x] .That is, for all j , Σi : 0 ≤ i < j : Hence, the equality also holds in R .Also, the semiring R has 2 n−1 distinct elements since each element in R has two representations as a polynomial in GF (2)[x] with degree less than n , and there are 2 n such polynomials.(For example, 0 is represented by the two polynomials Σi : 0 ≤ i < n : 0×x i and Σi : 0 ≤ i < n : 1×x i .)In particular (cf (4)) counts the number of non-zero coefficients in a given polynomial, then for all k and all P in GF (2)[x] with degree less than n , (It is at this point that the subscript R on the equality sign becomes essential; the left and right side of the equation denote P and x k , respectively, after injection into the semiring GF (2)[x] modulo the polynomial Σi : 0 ≤ i < n : x i .) We now have: = { (6) and ( 7) In order to apply (8), we conduct a case analysis on m mod n and on n .There are three cases to consider: This is the easiest case.The degree of Σi : 0 ≤ i < m mod n : x 2i is less than n so, applying (8), we have: Suppose n = 2q .The goal is to reduce Σi : 0 ≤ i < m mod n : x 2i to a polynomial with degree less than n .This is done in the following calculation.(Equalities are in R , i.e. in The last line above is a polynomial in GF (2) with degree less than n .Hence, applying (8) to it, we have: Simplifying, using n = 2q (and symmetry of disjunction), Suppose n = 2q − 1 .Then, by a similar calculation, we have: The last line above is a polynomial in GF (2) with degree less than n .Hence, applying (8) to it, we again get: We conclude that, in all cases, (4) implies that Equivalently, n\(m−1) ∨ n\(m+1) .

Figs. 3(a) and 3(b)
show that this condition is also sufficient.In both figures, the small square identifies where the 1 -omino is placed; the remaining rectangles each have at least one side whose length is divisible by n (the side that is labelled by a length) and so can easily be tiled by n -ominoes.

Games On Cyclotomic Polynomials
In this section, we propose and solve a novel class of games played on a one-dimensional tape.We call the games replacement-set games.The general class is considered in section 4.2; the socalled "nuclear pennies game" [12] based on the "seven trees in one" property [13,14] is used in section 4.1 to introduce the solution method, which is presented in section 4.3.Section 4.4 shows how to construct an infinite collection of instances of replacement-set games.The construction is based on the theory of cyclotomic polynomials relevant parts of which are summarised in section 4.4.1.

Seven-Trees-in-One and the Nuclear Pennies Game
Consider the definition of (unlabelled) binary trees -a binary tree is an empty tree or a pair of binary trees.Let us use symbols + and × to denote disjoint union and cartesian product respectively and let 1 1 denote the unit type.The type T of binary trees can be characterised by the type isomorphism T ∼ = 1 1+T ×T .Surprisingly, it can be shown that there is an isomorphism between seven-tuples of binary trees and binary trees.That is, T 7 ∼ = T .This has been dubbed "seven trees in one" by Blass [13] who attributes the isomorphism to a remark made by Lawvere [15].(See also [16].) The isomorphism has been turned into a game with checkers called the "nuclear pennies game" [3].The game is played on an infinite one-dimensional board divided into squares.A checker is placed on one of the squares and the goal is to move the checker six squares to the right.An atomic move is to replace a checker in a square numbered n+1 by two checkers, one on each of the two adjacent squares n and n+2 , or vice-versa, two checkers, one on square n and one on square n+2 for some n , are replaced by a checker on square n+1 .(There is no limit to the number of checkers that can be placed on any one square.)The connection with seven-trees-in-one is easy to see if one views a move as replacing T n ×T by T n ×(1 1+T ×T ) or vice-versa.
The nuclear-pennies game has an easy solution if one exploits the left-right symmetry of the problem (moving a coin 6 squares to the right is the same as moving a coin 6 squares to the left).The problem is decomposed into first ensuring that there is a checker on the square 6 squares to the right of the starting square and, symmetrically, there is a checker on the square 6 squares to the left of the finishing square.
Achieving this first stage is easy.Fig. 4 shows how it is done.First, six moves are needed to ensure that a checker is added six squares to the right of the starting square.(This is shown in fig.4(a) using dots to indicate checkers on a square.A blank indicates no checker on the square.)Symmetrically (fig.4(b)), working from bottom to top, six moves are needed to ensure that a checker is added six squares to the left of the finishing square.The first and last moves make the number of checkers in the leftmost and rightmost squares equal.Then a small amount of creativity is needed to identify the two (symmetrical) moves to the (symmetrical) middle state.(In fact, no creativity is needed, as we show later.)

Replacement-Set Games
Although the nuclear-pennies game is an interesting exercise in the exploitation of symmetry in problem decomposition, it has until recently been an isolated example and appears to have attracted relatively little attention9 .Chen and Backhouse [4] posed the problem of, given an arbitrary n , is it possible to invent an "interesting" type T such that T ∼ = T n+1 .Likewise, given an arbitrary n , is it possible to invent an "interesting" nuclear-pennies-like game in which the task is to move a checker n squares to the right using a sequence of pre-defined atomic moves.They gave an affirmative solution to the first question and a partial solution to the second, both answers being based on the use of so-called cyclotomic polynomials.(The solution to the second problem is partial in the sense that games were invented for an unbounded number of values of n but not all numbers n .)In this section, we present this class of "cyclotomic" games and their solution.(The paper [4] predicts that all cyclotomic games are solvable but does not give an explicit solution.) Although our original investigation concerned type isomorphisms, in this paper we abandon that application in order to focus on the algorithmics of games with checkers.This allows us to be more inventive in the games we propose.
The games we consider are called replacement-set games.Each instance is specified by a multiset R of integers and a natural number n ; the multiset R is called the replacement set and the number n is called the displacement.The games are played on an infinite one-dimensional tape divided into squares.There is an unbounded supply of checkers each of which occupies one square.When playing the game, stacking several checkers on the same square is allowed.
Given a multiset10 R and displacement n , play begins with one checker on one of the squares.All other squares are empty.The objective is to displace the checker by n squares in a sequence of moves.There are two types of moves: expansions and contractions.An expansion involves choosing an occupied square numbered k , say.One checker on square k is removed and one checker is added on square k+i for each occurrence of element i in the multiset R .For example, if the multiset R is {|1 * (−1) , 2 * 0 , 1 * 1|} , a move would replace one checker on square k by one checker on square k−1 , two checkers on square k and one checker on square k+1 .A contraction is the reverse of an expansion.That is, if there is at least one checker on square k+i for each occurrence of element i in the multiset R , these checkers are removed and replaced by one checker on square k .Henceforth, we refer to an expansion or contraction at square k .
Table 1 lists a number of examples of solvable replacement-set games.The first row, labelled E {(3,1)},1 , is the one just discussed; it is an instance of a simple, general subclass of solvable replacement-set games.Example E {(2,8)},3 in the fourth row is an instance of the same subclass.

Name
Replacement Multiset Displacement Example D 2,3 is the so-called "nuclear-pennies game", the game based on seven-trees-inone; examples D 2,3 , D 2,5 and D 3,5 are also instances of a simple, general subclass of solvable replacement-set games.The subclasses D and E are discussed further in section 4.4.We have as yet been unable to identify a simple, general proper subclass of the solvable replacement-set games that has example M 1 as an instance.The same goes for M 2 ; " M " stands for "miscellaneous".Example E {(3,1)},1 is easy to solve: generally, it is easy to construct non-challenging examples of solvable replacement-set games like E {(3,1)},1 but harder to construct challenging examples.

Quotient Polynomial
Suppose (R, n) is a replacement-set game.Let −m be the smallest element of R .(Typically R has both positive and negative elements.So m is positive.If the elements of R all have the same sign, the game is either unsolvable or trivial.)Then we model the game by the equation: where Λ×T m is a polynomial in IN[T ] (that is, for all i , the coefficients of T i are non-zero only when 0 ≤ i and all coefficients are natural numbers).Specifically, the coefficient T i Λ of T i in Λ is the multiplicity of the element i in the multiset R .For example, the nuclear-pennies game is the replacement-set game ( {|−1 , 1|} , 6 ) and is modelled by the equation The expansions in a game modelled by (9) are to replace a checker on square k by T i Λ checkers on each of the squares i+k ; this corresponds to the use of the equation ( 9) as a left-toright replacement rule.The contractions are the reverse: if for each integer i there are at least T i Λ checkers on square i+k , remove the T i Λ checkers on the square i+k and place one checker on square k .The task is to move from an initial state in which there is just one checker on square m to a final state where there is just one checker on square m+n .
Example M 1 is modelled by the equation: Playing the game is equivalent to showing that T 3 = T 15 assuming only that addition and multiplication enjoy the type-isomorphism properties of disjoint sum and cartesian product (that is, addition and multiplication are separately commutative monoids and multiplication distributes through addition).
A necessary condition for a game based on (9) to be solvable is easily determined.Suppose we represent any state of the board by a polynomial in the indeterminate T .Then, the moves are so designed that the polynomial modulo (Λ−1)×T m is an invariant.The initial state is represented by T m+0 and the desired final state is represented by T m+n .A necessary condition is thus that Excluding trivial games where there is only ever one checker on the board, it is thus necessary that the polynomial Note that the introduction of the multiplicative factor T m ensures that all powers of T in the polynomial (Λ−1)×T m are at least 0 and, hence, that modulo computation is well-defined.This is why the factor is introduced.Where modulo computation is not relevant, we allow polynomials with negative exponents in order to model replacements more directly.
In the next section, we establish that this condition is also sufficient.The proof is constructive: we give an algorithm that solves the given replacement-set game.

An Algorithm Solving Replacement-Set Games
In this section, we assume that the replacement-set game (R, n) is given.We make extensive use (here and elsewhere) of the (1-1) correspondence between multisets S of natural numbers and polynomials Ψ in the indeterminate T given by Σi :: mult.(i,S) × T i ↔ {|i :: ( T i Ψ) * i|} (13) where mult.(i,S) is the multiplicity of i in the multiset S and T i Ψ is the coefficient of T i in Ψ .We assume that −m is the smallest element of R and that (Λ−1)×T m is a divisor of T n −1 , where Λ is the polynomial such that the coefficient T i Λ of T i is the multiplicity of the value i in the multiset R .We show how to construct an algorithm that solves the game (R, n) .
Underlying the algorithm are three datatypes: multisets of (relative) positions 11 , sequences of (relative) positions and polynomials.We switch frequently between the three datatypes, with polynomials playing the central role.As detailed in section 4.2.1, we use IN[T ] to represent the state space of replacement-set games.Specifically, a state is represented by a polynomial such that the number of checkers on square i is the coefficient of T i .We also need to map sequences of replacements to polynomials.If js is a sequence of relative positions, and Λ is the polynomial corresponding to the replacement set R , the polynomial poly.js is defined by poly.[ ] = 0 and poly.(j:js)= (Λ−1)×T j + poly.js .
Suppose s is a polynomial in T , possibly with negative exponents.(Think of s as representing a state of the game.)The sequence js is said to be a valid expansion sequence from state s if applying the sequence of expansions specified by js starting from state s is a valid sequence of replacements in the game.The sequence js is said to be a valid contraction sequence from state s if the reverse of js is a valid expansion sequence from state s − poly.js .
Occasionally our algorithm leaves the order in which replacements are executed unspecified.In such cases, the replacements are specified by a polynomial, p say.We use the terminology expanding from square j in state s according to p and contracting from square j in state s according to p .Define the ordering ≤ on IN[T ] by q ≤ p equivales ∀i :: T i q ≤ T i p .Then, an expansion from square j in state s according to p is valid if, for all q in IN[T ] such that q ≤ p , A contraction from square j in state s according to p is valid if, for all q in IN[T ] such that q ≤ p , We begin the development of the algorithm in section 4.3.1 by reviewing the solution to the nuclear pennies game.What makes the nuclear pennies game easy to solve is the fact that {−1 , 1} is a subset of the replacement set.(In fact, in the nuclear pennies game, the replacement set equals {−1 , 1} but equality is not necessary.)The structure of our algorithm, which we explain in section 4.3.2, is modelled on the solution to the nuclear pennies game.However, whereas the expansions/contractions in the nuclear pennies game are each single applications of the replacement rule, the expansions/contractions in the general algorithm typically involve a sequence of replacements.The construction of an appropriate sequence of replacements is detailed in section 4.3.3.

Reviewing Nuclear Pennies
The solution to the nuclear pennies game acts as a model for the general algorithm.Recall that the solution consisted of three phases, the "initial expansion" phase, the "linking" phase and the "final contraction" phase: see figs.4(a), 4(b) and 5.The initial expansion and final contraction phases are symmetric: in the initial expansion phase, checkers on squares 1 thru 7 are replaced in turn and, in the final contraction phase, the reverse of replacing checkers on squares 7 thru 1 in turn is executed.(This description is slightly different from earlier: for the purposes of generalisation, the expansions at square 7 and the contraction at square 1 are also included in the expansion and contraction phases.)The linking phase links the expansion and contraction phases via a middle state -which in this case is symmetrical.
The steps needed to solve the nuclear pennies game are summarised below.The linking phase comprises the two steps labelled "expand at squares 5 and 6 " and "contract at squares 2 and 3 ".The variable s denotes the state of the tape, expressed as a polynomial in T .The replacement set is expressed by the polynomial (T −1 +T 1 )×T 1 ; an expansion at square k is expressed by the addition to s of (T −1 −T 0 +T 1 )×T k and a contraction at square k is expressed by the subtraction from s of (T −1 −T 0 +T 1 )×T k .Note that the middle state is expressed in two ways, via two syntactically different but nevertheless equal polynomials.
{ s = T 1 } initial expansion phase: expand at squares 1 thru 7; } expand at squares 6 and 5; contract at squares 2 and 3; final contraction phase: contract at squares 7 thru 1 We remarked in section 4.1 that the steps to and from the middle state required a "small amount of creativity".In fact, no creativity is required.The steps are based on the (unique) factorisation of T 6 −1 by T 2 −T +1 : From this factorisation, we easily calculate that, for all γ , In the solution, γ is chosen to be Σi : corresponding to expanding at squares 1 thru 7 , or equally Σi : corresponding to contracting at squares 7 thru 1 .This choice of γ is dictated by the requirements that (a) The initial expansion and final contraction phases are both valid.
(b) In the linking phase, there are checkers on squares 6 , 5 , 2 and 3 .(This ensures that the expansions and contractions at these squares are valid.)

Algorithm Decomposition
We now consider how to generalise the solution to the nuclear pennies game to the solution of an arbitrary replacement-set game (R, n) .Recall that −m is the smallest element of R and that (12) holds, where Λ is the polynomial in IN[T ] such that the coefficient T i Λ is the multiplicity of the value i in the multiset R .We assume that 0 < m < n .(It is easy to see that if this condition is not satisfied, the game is either trivial or unsolvable.) Without loss of generality, we may also assume that gcd.R = 1 .This is because where, for all i , In a game played with a replacement set R such that gcd.R > 1 , only squares i for which i mod (gcd.R) equals m mod (gcd.R) are ever occupied by checkers.(This assumption is not used in our algorithm but it does help to simplify our presentation.Chen's forthcoming PhD thesis will include all such details.)Suppose Our algorithm supposes that α and β have been calculated.The algorithm is, of course, the long-division algorithm for polynomials, so we do not consider it further.
Rewriting (14) as an equation between polynomials in IN[T ] , 1 + α×(Λ×T m ) + β×T m = T n + α×T m + β×(Λ×T m ) , (15) and exploiting the assumption that 0 < m < n , we deduce that β and Λ×T m are monic (i.e. 1 = β[T := 0] = (Λ×T m )[T := 0] ) and α[T := 0] = 0 .(For example, in the case of the nuclear pennies game α is T 3 +T 4 and β is 1+T .) Now, for all γ in IN[T ] , The left and right sides of this equation express the "middle state": the left side is the postcondition of the expansion phase and the right side is the precondition of the contraction phase.Beginning in the state T m , the expansion phase "expands" the state initially to T m + γ×(Λ−1) and then to T m + (γ + α×T 2m )×(Λ−1) .The contraction phase does the reverse.Beginning in the state T m+n + (γ + β×T 2m )×(Λ−1) (which by ( 16) is the postcondition of the expansion phase), the state is "contracted" to T m+n + γ×(Λ−1) and finally to T m+n .Recall that the process of expanding the state from some state s to s + ω×(Λ−1) is called expanding the state according to ω .Similarly, a contraction from s + ω×(Λ−1) to s is called contracting the state according to ω .Expanding the state according to ω means executing T i ω replacements at square i , for each i .In order for this to be valid irrespective of the order in which the replacements are executed, it is sufficient that in the state s there are at least T i ω checkers on square i , for each i .(This is because a replacement at any square does not decrease the number of checkers on other squares.) As in the case of the nuclear pennies game, there are two requirements on γ : (a) The initial expansion and final contraction phases are both valid.
(b) In the linking phase, there are sufficient checkers to be able to expand the state T m + γ×(Λ−1) according to α×T 2m and to be able to expand the state T m+n + γ×(Λ−1) according to β×T 2m .
In order to meet these requirements, we choose γ to have the form where k is the maximum of the largest coefficient of α and the largest coefficient of β , and Ψ = poly.L for some sequence L that is valid starting from state T 0 and guarantees that 0 < T −1 (T 0 + Ψ×(Λ−1)) and 0 < T 1 (T 0 + Ψ×(Λ−1)) .(In effect, Ψ corresponds to a replacement set that contains {−1 , 1} .)The value of k is easily computed.So our algorithm takes the following form, where the definition of Ψ still has to be formulated.
for k times do for m ≤ i ≤ m+n (in order of increasing i ) do for j in L (in order) do expand at square i+j ; for k times do for m+n ≥ i ≥ m (in order of decreasing i ) do for j in L (in order) The expansion in the initial phase is valid for the following reasons.First, provided Ψ is chosen so that 0 < T 1 (T 0 + Ψ×(Λ−1)) , (17) each time a checker at square i+j is replaced, a checker is added at square i+j+1 ready for the next replacement specified by L ; also, provided Ψ is chosen so that a checker is added at square m when i equals m+1 ready for any subsequent iteration of executing the expansions specified by L .(Note that 0 < n so that m+1 ≤ m+n ).A similar argument justifies the validity of the contraction phase (with the roles of the two provisos on Ψ reversed).The expansions and contractions according to α×T 2m and β×T 2m are then valid because, by inspection of ( 14) and the fact that 0 < m , (For polynomial p , deg.p is the degree of p : the largest power of T that has a non-zero coefficient; similarly, cod.p is the smallest power of T that has a non-zero coefficient.)The provisos on the choice of Ψ guarantee the presence of checkers at squares m thru m+n−1 after the initial expansion phase and at squares m+1 thru m+n before the final contraction phase.
The following section documents how to construct Ψ .

The Intermediate State
In this section, we show how to construct a valid sequence of (relative) replacement positions L that begins with (relative) position 0 and guarantees (17) and (18), where Ψ = poly.L .
We begin by constructing two multisets A and B .The elements of A and B are non-zero elements of the replacement set R .That is, (The function set coerces a multiset to a set.)On termination of the algorithm, ΣA (the sum of all the elements of the multiset A ; for example, Σ{|2 * (−3) , 1 * 2 , 1 * 3|} = −1 ) is −(gcd.R) and ΣB is gcd.R .The algorithm is an adaptation of Euclid's algorithm that takes account of the presence of negative as well as positive values in R .It reduces the set R to the set {−(gcd.R) , gcd.R} by repeatedly removing the minimum value x and maximum value y in R , replacing them by either −(gcd.{x,y})or gcd.{x,y} , the choice being made in such a way as to guarantee that R always has at least one negative element and at least one positive element.Since we may assume that gcd.R = 1 , the set R is thus reduced to {−1 , 1} .
Although not formally documented above, it is important to note that all elements of A and B are non-zero.(The multiset R may contain 0 but the algorithm exploits the fact that 0 is the unit of greatest common divisor: see the initialisation of Q .)From the multisets A and B we construct a sequence L of relative positions.This list is a valid sequence of positions that guarantees that if there is a checker on some square i and expansions are made at each of the squares in L in order relative to i , on termination there will be at least one checker on squares i − gcd.R and i + gcd.R .
Any sequence that is formed from the multiset A by ordering its elements and then forming the corresponding sequence of partial sums is valid.(For example, {|2 * (−3) , 2 * 2|} might be ordered as the sequence −3 , 2 , −3 , 2 ; the sequence of partial sums is then 0 , −3 , −1 , −4 , −2 .)Moreover, starting from a state where there is a checker on square i , such a sequence of partial sums results in a checker on square i − gcd.R (because ΣA is −(gcd.R) ).This is easily proven by induction on the length of the sequence of partial sums; the basis is the assumption that there is a checker on square i and the induction step exploits the fact that set.A is a subset of set.R .Similarly, any sequence formed from the multiset B in the same way results in a checker on square i + gcd.R .The complication is combining the two sequences; it is not possible, for example, to simply join the sequences because there is no guarantee of there being a checker at relative position 0 after this checker is replaced in the first step.
We resolve the problem by splitting the multisets A and B into positive and negative elements.The head of L is 0 .Let A − denote the sub-multiset of A consisting of the negative elements of A and let A + denote the sub-multiset of A consisting of the positive elements of A .Suppose we sequence the elements of A − arbitrarily (making sure that the number of times each element appears in the sequence equals its multiplicity in A − ).Let psA − denote the sequence of partial sums of A − , omitting the head element, 0 .Note that the last element of this sequence is ΣA − .Let ΣA − +psA + denote the sequence formed by adding ΣA − to each element of the sequence of partial sums of A + , again omitting the head element 0 .Define ΣB + +psB − similarly.Then the sequence L is given by: This is a valid sequence starting from an arbitrary state T i because each of the relative positions in psA − is negative and so too is each of the relative positions in ΣA − +psA + -the last element of this sequence is ΣA which equals −(gcd.R) and it is the largest element of the sequence-; similarly, each of the relative positions in psB + is positive and so too is each of the relative positions in ΣB + +psB − .This completes the development of the algorithm.

Constructing Solvable Replacement-Set Games
The algorithm we have derived in section 4.3 would not be of any interest were it not possible to construct many examples of replacement-set games.Fortunately, there is an abundance of such examples.In this section, we show how to construct several infinite classes of replacement-set games; we believe that, except for the very simplest examples in each class, all the games would be very challenging to solve without knowledge of the theory that we have developed.
A well-known result is that, in Z Z[T ] , a polynomial is a divisor of T n −1 equivales it is a product of so-called cyclotomic polynomials.Specifically, suppose Φ.k denotes the k th cyclotomic polynomial.Then, using the symbol " \ " to denote both the divides relation on polynomials in Z Z[T ] and the divides relation on numbers Ψ \ (T n −1) ≡ ∃D : D ⊆ IN : Ψ = Πk : k∈D ∧ k\n : Φ.k (The theory of cyclotomic polynomials is very well known.Wikipedia, [9] and [17] are the sources we have used for information on cyclotomic polynomials.)For brevity, a polynomial in Z Z[T ] that can be written as Πk : k∈D : Φ.k for some nonempty set D of strictly positive natural numbers will be called a cyclotomic product.Note that Πk : k∈D : Φ.k is a divisor of T lcm.D −1 .(The function lcm returns the lowest common multiple of a set of numbers.)Note also that a cyclotomic product is uniquely defined by the set D .(This is because cyclotomic polynomials are irreducible and Z Z[T ] is a unique factorisation domain.)Given a cyclotomic product, p , we call the set D of strictly positive natural numbers such that p = Πk : k∈D : Φ.k the natural factors of p .(The adjective "natural" is used to avoid confusion with the cyclotomic polynomials Φ.k , where k∈D , which would be called the factors of p .) The construction of an infinite class of games is straightforward: it suffices to consider just two classes of cyclotomic products: We call replacement-set games derived from cyclotomic products in class (a) true replacementset games.This is because, for this class of games, a move always truly "replaces" a checker in the sense that the number of checkers on one square strictly decreases (by one) in an "expansion" move: the checker is replaced by checkers elsewhere on the tape.For this class of games, the number m is the exponent of the single negative coefficient in the cyclotomic product.
We call replacement-set games derived from cyclotomic products in class (b) monotonic replacementset games.They are instances of replacement-set games but, in an expansion move, the number of checkers on each square does not decrease.For products in class (b), the choice of m is an arbitrary number that is greater than 0 and less than the degree of the product: if Ψ is a cyclotomic product in IN[T ] , we take Λ to be 1 + Ψ×T −m .
In this section, we show how to construct an unbounded number of (solvable) true replacementset games (section 4.4.3) and an unbounded number of (solvable) monotonic games (section 4.4.2).We begin by giving a brief overview of properties of products of cyclotomic polynomials.

Properties of Cyclotomic Products
Cyclotomic polynomials are irreducible factors of T n −1 for positive natural numbers n .The properties of cyclotomic polynomials are frequently discussed in textbooks (for example [9]) and, so, we state such properties without proof.The properties of non-trivial products of cyclotomic polynomials -which is what we are interested in-do not appear to have been documented, so we are obliged to provide proofs.
The m th cyclotomic polynomial is defined by where k⊥m means that natural numbers k and m are coprime.From this definition, we have that for all positive natural numbers n , Equation ( 19) is a recurrence relation in Φ .Solving, we get the inductive definition: With the aid of Möbius function µ , which is defined by the recurrence relation: Φ can be calculated by the following formula: From the definition of µ , it is easy to show that, for all m , µ.m is either +1 , −1 or 0 .(See, for example, [9].)It is 0 if the exponent of at least one prime number in the prime factorisation of m is greater than 1 .Otherwise, m is said to be square-free and µ.m is +1 if the number of prime factors of m is even and −1 if the number of prime factors of m is odd.In general, we have Using this formula, computing the polynomial form of Φ.m , for all m , is reduced to computing the polynomial form of Φ.m for all square-free m .For square-free m , formula (21) becomes: When m ≥ 2 , the number of terms in the numerator of (23) equals the number of terms in its denominator.

Constructing Monotonic Replacement-Set Games
Monotonic replacement-set games are based on cyclotomic products that are elements of IN[T ] .In this section, we show how to construct such products.
From (20) we deduce that (Φ.1)[T := 0] = −1 and (Φ.m)[T := 0] = 1 for all m ≥ 2 .It follows that a cyclotomic product is an element of IN[T ] (i.e.all its coefficients are non-negative) only if 1 is not one of its natural factors.From ( 22) and ( 23), any non-trivial cyclotomic product that does not have 1 as a natural factor can be written in the form .
That is, if ( 27) is expanded into a product of cyclotomic polynomials with the aid of (26), no cyclotomic polynomial is repeated.Note that the test on k can be simplified to where " ∇ " is the gcd operator.Summarising, we have: In both examples, the set of pairs has just one element so there is nothing to check.Lots of other examples can be constructed.For example, given a square-free number n , let prf .ndenote the set of prime factors of n .(For example, prf .6 = {2,3} .)Then we can choose P N to be {p : p ∈ prf .n: (p, 1)} .Choose any offset f such that 0 < f < Σp : p ∈ prf .n: p−1 .Then the game (E P N ,f , n) is solvable.The replacement set R is the multiset corresponding to (Ψ+T f −f where Ψ = Πp : p ∈ prf .n: Γ.(p, 1) .For example, taking n to be 6 , the polynomial Ψ is Choosing the offset to be 2 , we get the (solvable) game The class E {(a,a n−1 )},1 was studied by the authors in the earlier paper [18].The final example illustrates that the multiplicity of several elements of the replacement set may be greater than 1 .Not every monotonic replacement-set game has the form E P N ,f for some P N and some f .The game M 2 in table 1 is an example.

Constructing True Replacement-Set Games
We now consider the construction of "true" replacement-set games.Recall that these are games derived from a cyclotomic product with one coefficient that is −1 and all other coefficients are positive.
The construction of true replacement-set games seems to be considerably more difficult than the construction of monotonic replacement-set games.(Of course, it is always possible to use a brute-force search of all cyclotomic products of appropriate degree, but we do not regard this as an acceptable construction method.)In this section, we present one class of true replacement-set games, albeit constructed in a somewhat ad hoc fashion.The class of games is formulated in the following theorem.
Lemma 29.For all distinct primes p and q , Φ.(p×q) × Γ.(p×q − p − q , 1) (30) is a cyclotomic product with one coefficient that is −1 and all other coefficients are positive.Moreover, it is a factor of T n −1 where n is the least common multiple of p×q and p×q − p − q .
Proof Since p×q is greater than p×q − p − q , we have that p×q is not a divisor of p×q − p − q .By (26), the formula (30) can be rewritten as: Πk : k = p×q ∨ (k\(p×q − p − q) ∧ k = 1) : Φ.k .
Further, by ( 19), ( 30) is a factor of T n −1 where n is the least common multiple of p×q and p×q − p − q .
Define the multiset R to be {|i: 0 ≤ i: ( T i Ψ) * (i − (p×q − p − q))|} and define n to be the least common multiple of p×q and p×q − p − q .Then (R, n) is a solvable (true) replacement-set game.
Proof Follows from lemma 29 and the algorithm given in section 4.3.

2
Examples of games constructed using theorem 31 are the games D 2,3 , D 2,5 and D 3,5 given in table 1.The game M 1 in table 1 is also a true replacement-set game but is not an instance of the class D .(It is derived from Φ.3 × Φ.12 .)

Conclusion
Games are a well-established medium for illustrating mathematical insights.They are particularly useful for developing algorithmic-problem-solving skills because games are inherently about algorithms for winning and they require little motivation.
The common theme of all our examples is the exploitation of invariant properties of polynomials.The class of tiling problems discussed in section 3 assumes that all "ominoes" are straight.Golomb [1] extends his colouring argument to other problems where the ominoes are not straight.We haven't explored these problems.It would be interesting to see whether the algebraic formulation can be extended to such problems in a uniform way.
The primary novel contribution of this paper is the class of "replacement-set games" and their solution.A vital aspect of this class is the ability to construct abundantly many challenging instances -isolated examples of games have very little pedagogical value.Although we have achieved this goal, our investigation has left several open problems.A particular challenge is to identify necessary and sufficient conditions for a cyclotomic product to be an element of IN[T ] or to have exactly one coefficient that equals −1 -conditions that admit simple algorithms to construct arbitrary instances.
We believe that, with sufficient imagination, the replacement-set games offer a rich source of examples for the further development of algorithmic-problem-solving skills.We have implemented our algorithm (in Haskell) in order to try to avoid transcription errors in the preparation of this paper but we have put no effort into trying to develop an optimal algorithm and we are well aware that our algorithm is suboptimal in many cases.For example, in our earlier conference paper [18], we developed a more efficient algorithm for a particular subclass of replacement-set games.A useful educational exercise would be to explore improved algorithms for solving particular subclasses of replacement-set games, exploiting the specific properties of the subclass.For example, the cyclotomic products defining true replacement-set games are invariably palindromic (that is, if the coefficients are written in order they form a palindrome) but we have not attempted to exploit this property in any way.It is also very likely that our algorithm can be improved even in the general case.

Figure 1 :
Figure 1: Move from left to right.Similar moves are allowed from right to left, from top to bottom, and from bottom to top.

Figure 4 :
Figure 4: Seven Trees in One -Initial and Final Phases Now the goal is to connect these two intermediate states (the bottom state in fig.4(a) and the top state in fig.4(b)).An appropriate (symmetrical) sequence of states is shown in fig. 5 .(For the reader's convenience, the last and first states in figs.4(a) and 4(b) are repeated as the top and bottom states in fig.5.) 0 1 2 3 4 5 6 7 8 ... ... ... ... ... ...

( a )
Each element of the class is a polynomial in Z Z[T ] such that exactly one of the coefficients is −1 and all other coefficients are in IN .(b) Each element of the class is a polynomial in IN[T ] .

Table 2
uses variables n , f , a , b , c and d to define several subclasses of monotonic games; concrete examples follow the description of each subclass.In each case, the requirement on f is that the replacement set contain both strictly positive and strictly negative elements.The requirement on a , b , c and d in the class E {(a,b),(c,d)},f is ∀k : k\((a×b) ∇ (c×d)) : k\b ∨ k\d .The class E {(2,2n)},1 was previously identified by Marcelo Fiore [private communication, 2010].

Table 2 :
Monotonic Replacement-Set Games