Problem

Source: Czech-Polish-Slovak 2004 Q6

Tags: greatest common divisor, number theory proposed, number theory



On the table there are $k \ge 3$ heaps of $1, 2, \dots , k$ stones. In the first step, we choose any three of the heaps, merge them into a single new heap, and remove $1$ stone from this new heap. Thereafter, in the $i$-th step ($i \ge 2$) we merge some three heaps containing more than $i$ stones in total and remove $i$ stones from the new heap. Assume that after a number of steps a single heap of $p$ stones remains on the table. Show that the number $p$ is a perfect square if and only if so are both $2k + 2$ and $3k + 1$. Find the least $k$ with this property.