Swapping Counters
A horizontal row comprises 2n+1 squares. A set of n red counters occupies the leftmost n squares and a set of n blue counters occupies the rightmost n squares, with a single empty square in the mid...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A horizontal row comprising of $2n + 1$ squares has $n$ red counters placed at one end and $n$ blue counters at the other end, being separated by a single empty square in the centre. For example, when $n = 3$.

A counter can move from one square to the next (slide) or can jump over another counter (hop) as long as the square next to that counter is unoccupied.

Let $M(n)$ represent the minimum number of moves/actions to completely reverse the positions of the coloured counters; that is, move all the red counters to the right and all the blue counters to the left.
It can be verified $M(3) = 15$, which also happens to be a triangle number.
If we create a sequence based on the values of $n$ for which $M(n)$ is a triangle number then the first five terms would be:
$1$, $3$, $10$, $22$, and $63$, and their sum would be $99$.
Find the sum of the first forty terms of this sequence.s
Problem 321: Swapping Counters
Solution
Reduction to a Pell-type equation
Proposition 1. The positive integer satisfies the requirement that is triangular if and only if there exists a positive integer such that
Theorem 1. The equation with is equivalent to the generalised Pell equation
under the substitution , .
Proof. Multiplying both sides of by 8 yields
Completing the square on each side:
Setting and gives . Since forces and forces (with odd), the correspondence is bijective: every admissible pair yields a solution with and odd, and conversely every such solution yields and .
Enumeration of solutions
Lemma 1 (Fundamental solutions). The equation has exactly two classes of solutions, with fundamental representatives and .
Proof. Checking : , so is a solution. Checking : , so is a solution. For we have , and one verifies that the next solutions in each class (generated by the recurrence below) have and respectively, confirming no intermediate solutions exist.
Theorem 2 (Recurrence for solutions). If is a solution of , then so is
Moreover, within each family the -values satisfy the linear recurrence , and consequently the -values satisfy
Proof. The fundamental solution of the associated Pell equation is . In the ring , multiplication by the unit maps solutions of to solutions, since the norm is multiplicative:
Explicitly, , giving the stated formulas.
For the second-order recurrence, applying the map twice yields . Substituting and (from ), we obtain
Since , substituting gives , whence .
Two families of valid -values
Applying the recurrence :
- Family A (from , i.e., ):
- Family B (from , i.e., ):
Discarding from Family A and merging both families in sorted order, the first 40 positive values are collected and summed.
Editorial
We seek positive integers n such that M(n) = n(n+2) is a triangular number. The substitution x = 2k+1, y = n+1 transforms n(n+2) = k(k+1)/2 into the generalised Pell equation x^2 - 8y^2 = -7, whose solutions fall into two families, each satisfying n_{i+2} = 6 n_{i+1} - n_i + 4.
Pseudocode
A = [0, 3]
B = [1, 10]
For i from 2 to 24:
A.append(6 * A[i-1] - A[i-2] + 4)
B.append(6 * B[i-1] - B[i-2] + 4)
values = sorted(A[1:] + B)
Return sum(values[:40])
Complexity
- Time: where . Each family requires terms, each computed in arithmetic operations.
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
/*
* Problem 321: Swapping Counters
*
* n(n+2) = k(k+1)/2 <==> x^2 - 8y^2 = -7 (x = 2k+1, y = n+1).
* Two families of solutions with recurrence n_{i+2} = 6 n_{i+1} - n_i + 4.
* Family A seeds: n = 0, 3. Family B seeds: n = 1, 10.
*/
int main() {
vector<long long> vals;
// Family A (skip n = 0)
long long a0 = 0, a1 = 3;
vals.push_back(a1);
for (int i = 2; i < 25; i++) {
long long a2 = 6 * a1 - a0 + 4;
vals.push_back(a2);
a0 = a1;
a1 = a2;
}
// Family B
long long b0 = 1, b1 = 10;
vals.push_back(b0);
vals.push_back(b1);
for (int i = 2; i < 25; i++) {
long long b2 = 6 * b1 - b0 + 4;
vals.push_back(b2);
b0 = b1;
b1 = b2;
}
sort(vals.begin(), vals.end());
long long ans = 0;
for (int i = 0; i < 40; i++)
ans += vals[i];
cout << ans << endl;
return 0;
}
"""
Problem 321: Swapping Counters
We seek positive integers n such that M(n) = n(n+2) is a triangular number.
The substitution x = 2k+1, y = n+1 transforms n(n+2) = k(k+1)/2 into the
generalised Pell equation x^2 - 8y^2 = -7, whose solutions fall into two
families, each satisfying n_{i+2} = 6 n_{i+1} - n_i + 4.
"""
def solve():
# Family A seeds: n = 0, 3 (from fundamental solution (1,1))
# Family B seeds: n = 1, 10 (from fundamental solution (5,2))
a_prev, a_cur = 0, 3
b_prev, b_cur = 1, 10
vals = [a_cur, b_prev, b_cur] # skip a_prev = 0
for _ in range(23):
a_prev, a_cur = a_cur, 6 * a_cur - a_prev + 4
b_prev, b_cur = b_cur, 6 * b_cur - b_prev + 4
vals.append(a_cur)
vals.append(b_cur)
vals.sort()
print(sum(vals[:40]))
if __name__ == "__main__":
solve()