All Euler problems
Project Euler

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...

Source sync Apr 19, 2026
Problem #0641
Level Level 21
Solved By 583
Languages C++, Python
Answer 793525366
Length 381 words
number_theorymodular_arithmeticsequence

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 (x,y)R2(x,y) \in \mathbb{R}^2, define the colour function

χ(x,y)=(x/W+y/H)mod2.\chi(x,y) = \bigl(\lfloor x/W \rfloor + \lfloor y/H \rfloor\bigr) \bmod 2.

The cell is white if χ=0\chi = 0 and black if χ=1\chi = 1.

Theorem 1 (Orbit Period). The sequence of reduced positions (kdxmod2W,  kdymod2H)k0(k\,dx \bmod 2W,\; k\,dy \bmod 2H)_{k \ge 0} is periodic with exact period

L=lcm ⁣(2Wgcd(dx,2W),  2Hgcd(dy,2H)).L = \operatorname{lcm}\!\left(\frac{2W}{\gcd(dx, 2W)},\; \frac{2H}{\gcd(dy, 2H)}\right).

Proof. Consider the xx-coordinate modulo 2W2W. The map kkdxmod2Wk \mapsto k\,dx \bmod 2W is the orbit of 00 under repeated addition of dxdx in the cyclic group Z/2WZ\mathbb{Z}/2W\mathbb{Z}. The order of the element dxdx in this group is

Lx=2Wgcd(dx,2W),L_x = \frac{2W}{\gcd(dx, 2W)},

since the subgroup generated by dxdx in Z/2WZ\mathbb{Z}/2W\mathbb{Z} has order 2W/gcd(dx,2W)2W / \gcd(dx, 2W) by the structure theorem for cyclic groups. Similarly, the yy-coordinate modulo 2H2H has period Ly=2H/gcd(dy,2H)L_y = 2H / \gcd(dy, 2H). The joint sequence (xkmod2W,ykmod2H)(x_k \bmod 2W, y_k \bmod 2H) returns to (0,0)(0,0) precisely when both coordinates simultaneously return to 00, which occurs at k=lcm(Lx,Ly)=Lk = \operatorname{lcm}(L_x, L_y) = L. That LL is the exact period follows from the minimality of the lcm among common multiples of LxL_x and LyL_y. \square

Lemma 1 (Colour Determination on the Torus). The colour of the cell visited at step kk depends only on the residue pair (kdxmod2W,  kdymod2H)(k\,dx \bmod 2W,\; k\,dy \bmod 2H).

Proof. Write kdx=2Wqx+rxk\,dx = 2Wq_x + r_x where rx=kdxmod2Wr_x = k\,dx \bmod 2W. Then kdx/W=2qx+rx/W\lfloor k\,dx / W \rfloor = 2q_x + \lfloor r_x / W \rfloor, so kdx/Wmod2=rx/Wmod2\lfloor k\,dx / W \rfloor \bmod 2 = \lfloor r_x / W \rfloor \bmod 2. The analogous identity holds for the yy-coordinate. Therefore

χ(kdx,kdy)=(rx/W+ry/H)mod2,\chi(k\,dx, k\,dy) = \bigl(\lfloor r_x / W \rfloor + \lfloor r_y / H \rfloor\bigr) \bmod 2,

which depends only on (rx,ry)(r_x, r_y). \square

Theorem 2 (Colour Counting via Full Periods). Let ww and bb denote the number of white and black cells visited in one full period {0,1,,L1}\{0, 1, \ldots, L-1\}, so that w+b=Lw + b = L. Then across all N+1N+1 positions {0,1,,N}\{0, 1, \ldots, N\}:

WN=N+1Lw  +  wr,BN=(N+1)WN,W_N = \left\lfloor \frac{N+1}{L} \right\rfloor w \;+\; w_r, \qquad B_N = (N+1) - W_N,

where r=(N+1)modLr = (N+1) \bmod L and wrw_r is the number of white cells among the first rr positions of a period.

Proof. By Lemma 1 and Theorem 1, the colour sequence (χ0,χ1,)(\chi_0, \chi_1, \ldots) is periodic with period LL. The N+1N+1 positions {0,,N}\{0, \ldots, N\} decompose into (N+1)/L\lfloor (N+1)/L \rfloor complete periods, each contributing exactly ww white cells, plus a partial period of length r=(N+1)modLr = (N+1) \bmod L contributing wrw_r white cells. The black count follows from complementarity. \square

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: O(L)O(L) where L=lcm(Lx,Ly)4WH/(gcd(dx,2W)gcd(dy,2H))L = \operatorname{lcm}(L_x, L_y) \le 4WH / (\gcd(dx,2W) \cdot \gcd(dy,2H)).
  • Space: O(L)O(L) for the prefix-sum array (reducible to O(1)O(1) if only the total count is needed after a single enumeration pass).

Answer

793525366\boxed{793525366}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_641/solution.cpp
#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;
}