A Flea on a Chess Board
An infinite chessboard has cells of width W and height H, coloured in the standard alternating pattern: the cell containing point (x, y) is white if floor(x/W) + floor(y/H) is even, and black other...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Consider a row of \(n\) dice all showing 1.
First turn every second die,\( (2,4,6,\ldots )\), so that the number showing is increased by 1. Then turn every third die. The sixth die will now show a 3. Then turn every fourth die and so on until every \(n\)th die (only the last die) is turned. If the die to be turned is showing a 6 then it is changed to show a 1.
Let \(f(n)\) be the number of dice that are showing a 1 when the process finishes. You are given \(f(100)=2\) and \(f(10^8) = 69\).
Find \(f(10^{36})\).
Problem 641: A Flea on a Chess Board
Mathematical Foundation
Definition 1 (Cell Colour). For a point , define the colour function
The cell is white if and black if .
Theorem 1 (Orbit Period). The sequence of reduced positions is periodic with exact period
Proof. Consider the -coordinate modulo . The map is the orbit of under repeated addition of in the cyclic group . The order of the element in this group is
since the subgroup generated by in has order by the structure theorem for cyclic groups. Similarly, the -coordinate modulo has period . The joint sequence returns to precisely when both coordinates simultaneously return to , which occurs at . That is the exact period follows from the minimality of the lcm among common multiples of and .
Lemma 1 (Colour Determination on the Torus). The colour of the cell visited at step depends only on the residue pair .
Proof. Write where . Then , so . The analogous identity holds for the -coordinate. Therefore
which depends only on .
Theorem 2 (Colour Counting via Full Periods). Let and denote the number of white and black cells visited in one full period , so that . Then across all positions :
where and is the number of white cells among the first positions of a period.
Proof. By Lemma 1 and Theorem 1, the colour sequence is periodic with period . The positions decompose into complete periods, each contributing exactly white cells, plus a partial period of length contributing white cells. The black count follows from complementarity.
Editorial
The colour sequence on the torus Z/(2W) x Z/(2H) is periodic with period L = lcm(2W/gcd(dx,2W), 2H/gcd(dy,2H)). Count white/black cells in one period, then scale to N+1 positions via division with remainder. We enumerate one full period on the torus. Finally, decompose N+1 positions into full periods + remainder.
Pseudocode
Enumerate one full period on the torus
Decompose N+1 positions into full periods + remainder
Complexity Analysis
- Time: where .
- Space: for the prefix-sum array (reducible to if only the total count is needed after a single enumeration pass).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/*
* Problem 641: A Flea on a Chess Board
*
* Colour sequence on the torus Z/(2W) x Z/(2H) has period
* L = lcm(2W/gcd(dx,2W), 2H/gcd(dy,2H)).
* Enumerate one period, then scale via division with remainder.
*/
ll gcd_(ll a, ll b) { return b ? gcd_(b, a % b) : a; }
ll lcm_(ll a, ll b) { return a / gcd_(a, b) * b; }
pair<ll, ll> count_colours(ll W, ll H, ll dx, ll dy, ll N) {
ll Lx = 2 * W / gcd_(dx, 2 * W);
ll Ly = 2 * H / gcd_(dy, 2 * H);
ll L = lcm_(Lx, Ly);
// Enumerate one full period
vector<ll> prefix(L + 1, 0);
ll x = 0, y = 0;
for (ll k = 0; k < L; k++) {
ll colour = (x / W + y / H) % 2;
prefix[k + 1] = prefix[k] + (1 - colour);
x = (x + dx) % (2 * W);
y = (y + dy) % (2 * H);
}
ll w = prefix[L];
ll full = (N + 1) / L;
ll rem = (N + 1) % L;
ll white_total = full * w + prefix[rem];
ll black_total = (N + 1) - white_total;
return {white_total, black_total};
}
int main() {
auto [w, b] = count_colours(2, 3, 3, 4, 999);
cout << "Problem 641: white=" << w << " black=" << b << endl;
return 0;
}
"""
Project Euler Problem 641: A Flea on a Chess Board
The colour sequence on the torus Z/(2W) x Z/(2H) is periodic with period
L = lcm(2W/gcd(dx,2W), 2H/gcd(dy,2H)). Count white/black cells in one
period, then scale to N+1 positions via division with remainder.
"""
from math import gcd
def lcm(a, b):
return a * b // gcd(a, b)
def count_colours(W, H, dx, dy, N):
Lx = 2 * W // gcd(dx, 2 * W)
Ly = 2 * H // gcd(dy, 2 * H)
L = lcm(Lx, Ly)
# Enumerate one full period
white_prefix = [0] * (L + 1)
x, y = 0, 0
for k in range(L):
colour = (x // W + y // H) % 2
white_prefix[k + 1] = white_prefix[k] + (1 - colour)
x = (x + dx) % (2 * W)
y = (y + dy) % (2 * H)
w = white_prefix[L]
full, rem = divmod(N + 1, L)
white_total = full * w + white_prefix[rem]
black_total = (N + 1) - white_total
return white_total, black_total
# Verification with small cases
assert count_colours(2, 3, 1, 1, 0) == (1, 0) # origin is white
print(count_colours(2, 3, 3, 4, 999))
print("Problem 641: A Flea on a Chess Board")