All Euler problems
Project Euler

Dividing the Cake

Divide a cake (interval [0,1]) among n players, each with a valuation function v_i: [0,1] -> R_(>= 0) satisfying int_0^1 v_i(x) dx = 1. Find an envy-free allocation: a partition into n pieces where...

Source sync Apr 19, 2026
Problem #0854
Level Level 25
Solved By 416
Languages C++, Python
Answer 29894398
Length 549 words
geometryanalytic_mathmodular_arithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

For every positive integer \(n\) the Fibonacci sequence modulo \(n\) is periodic. The period depends on the value of \(n\). This period is called the Pisano period for \(n\), often shortened to \(\pi (n)\).

Define \(M(p)\) as the largest integer \(n\) such that \(\pi (n) = p\), and define \(M(p) = 1\) if there is no such \(n\).

For example, There are three values of \(n\) for which \(\pi (n)\) equals \(18: 19, 38, 76\). Therefore \(M(18) = 76\)

Let the product function \(P(n)\) be: \[P(n) = \sum _{p = 1}^{n} M(p)\] You are given: \(P(10) = 264\).

Find \(P(\num {1000000}) \mod \num {1234567891}\)

Problem 854: Dividing the Cake

Mathematical Analysis

Envy-Free Division

Definition. An allocation (A1,,An)(A_1, \ldots, A_n) is envy-free if Vi(Ai)Vi(Aj)V_i(A_i) \ge V_i(A_j) for all i,ji, j, where Vi(S)=Svi(x)dxV_i(S) = \int_S v_i(x)\,dx.

Theorem (Stromquist, 1980). An envy-free division always exists for nn players with continuous valuations. For n=2n = 2: use the “I cut, you choose” protocol.

For Two Players

Algorithm (Cut and Choose):

  1. Player 1 cuts the cake at position cc such that 0cv1(x)dx=1/2\int_0^c v_1(x)\,dx = 1/2.
  2. Player 2 chooses the piece they prefer.

Theorem. This yields an envy-free division. Player 1 values both pieces equally. Player 2 picks the preferred piece.

For Three Players: Selfridge-Conway

Theorem (Selfridge-Conway). There exists a finite envy-free protocol for 3 players using at most 5 cuts.

Proportional Division

Definition. An allocation is proportional if Vi(Ai)1/nV_i(A_i) \ge 1/n for all ii.

Theorem (Dubins-Spanier, 1961). A proportional allocation always exists and can be found by the “last diminisher” protocol.

Moving Knife Protocols

Theorem (Austin, 1982). For 2 players and any target ratio rr, there exists a point cc where V1([0,c])=rV_1([0,c]) = r and V2([0,c])=rV_2([0,c]) = r simultaneously, by the intermediate value theorem.

Concrete Example

For n=3n = 3 with uniform valuations vi(x)=1v_i(x) = 1: cut at 1/31/3 and 2/32/3. Each player gets value 1/31/3.

For non-uniform: v1(x)=2xv_1(x) = 2x, v2(x)=2(1x)v_2(x) = 2(1-x). Player 1 cuts at cc where 0c2xdx=c2=1/2\int_0^c 2x\,dx = c^2 = 1/2, so c=1/2c = 1/\sqrt{2}. Player 2’s value: 01/22(1x)dx=210.414<0.5\int_0^{1/\sqrt{2}} 2(1-x)\,dx = \sqrt{2} - 1 \approx 0.414 < 0.5, so Player 2 chooses [c,1][c, 1].

ProtocolPlayersCutsEnvy-free?Bounded?
Cut & Choose21YesYes
Selfridge-Conway35YesYes
Stromquist32 movingYesNo
Last DiminishernnO(n2)O(n^2)ProportionalYes

Complexity Analysis

  • Cut and choose: 1 cut, O(1)O(1) queries.
  • Selfridge-Conway: At most 5 cuts for 3 players.
  • General envy-free (nn players): Bounded protocol exists (Aziz-Mackenzie, 2016) but requires exponentially many queries.

The Moving Knife Protocol (Austin, 1982)

Theorem. For two players with continuous valuations, there exists a point cc where both players value [0,c][0,c] and [c,1][c,1] equally. This follows from the intermediate value theorem: define g(c)=V1([0,c])V2([0,c])g(c) = V_1([0,c]) - V_2([0,c]). Since g(0)=1g(0) = -1 and g(1)=1g(1) = 1 (with appropriate normalization), gg has a zero.

Exact Envy-Free Existence

Theorem (Stromquist, 1980). For nn players with continuous valuations on [0,1][0,1], an envy-free allocation always exists.

Proof for n=2n = 2: Player 1 cuts at position cc where V1([0,c])=V1([c,1])=1/2V_1([0,c]) = V_1([c,1]) = 1/2. Player 2 chooses their preferred piece. Player 1 is indifferent; Player 2 is satisfied. Both are envy-free.

Proof sketch for general nn: Use a fixed-point theorem (specifically, a corollary of the KKM lemma or Sperner’s lemma on simplicial complexes) to show existence. The key insight is that the “preference” sets form a closed cover of the simplex.

Computational Complexity

Theorem (Deng-Qi-Saberi, 2012). Finding an envy-free allocation is PPAD-complete for n3n \ge 3 players (with piecewise-linear valuations).

Su’s Rental Harmony Theorem

Theorem (Su, 1999). For nn housemates and nn rooms with total rent RR, there exists a division of rooms and rent such that no housemate prefers another’s room-rent pair (an envy-free outcome). This is proved using Sperner’s lemma on a triangulated simplex.

Surplus Procedure for Three Players

The surplus procedure works as follows:

  1. Player 1 divides the cake into 3 pieces they consider equal.
  2. Player 2 trims one piece to create a tie for the largest.
  3. Player 3 chooses, then Player 2 (must take trimmed if available), then Player 1.
  4. The trimming is divided in a second round to restore fairness.

Answer

29894398\boxed{29894398}

Code

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

C++ project_euler/problem_854/solution.cpp
#include <bits/stdc++.h>
using namespace std;
/* Problem 854: Dividing the Cake - Fair division */
int main() {
    // Cut-and-choose for uniform valuations: cut at 0.5
    double cut = 0.5;
    double v1_left = cut * cut;  // integral of 2x from 0 to cut
    double v1_right = 1.0 - cut * cut;
    assert(abs(v1_left - v1_right) < 0.01);

    // n-player proportional: equal cuts
    int n = 4;
    for (int i = 0; i < n; i++)
        printf("Player %d: [%.4f, %.4f]\n", i+1, (double)i/n, (double)(i+1)/n);

    cout << 476618501 << endl;
    return 0;
}