All Euler problems
Project Euler

Jack's Bean

Jack faces three plates containing beans. One bean is magical, and Jack must identify it through yes/no questions about whether subsets contain the magic bean. For plates with a, b, and c beans res...

Source sync Apr 19, 2026
Problem #0847
Level Level 38
Solved By 142
Languages C++, Python
Answer 381868244
Length 259 words
modular_arithmeticsearchgame_theory

Problem Statement

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

Jack has three plates in front of him. The giant has \(N\) beans that he distributes to the three plates. All the beans look the same, but one of them is a magic bean. Jack doesn’t know which one it is, but the giant knows.

Jack can ask the giant questions of the form: "Does this subset of the beans contain the magic bean?" In each question Jack may choose any subset of beans from a single plate, and the giant will respond truthfully.

If the three plates contain \(a\), \(b\) and \(c\) beans respectively, we let \(h(a, b, c)\) be the minimal number of questions Jack needs to ask in order to guarantee he locates the magic bean. For example, \(h(1, 2, 3) = 3\) and \(h(2, 3, 3) = 4\).

Let \(H(N)\) be the sum of \(h(a, b, c)\) over all triples of non-negative integers \(a\), \(b\), \(c\) with \(1 \leq a + b + c \leq N\).

You are given: \(H(6) = 203\) and \(H(20) = 7718\).

A repunit, \(R_n\), is a number made up with \(n\) digits all ’1’. For example, \(R_3 = 111\) and \(H(R_3) = 1634144\).

Find \(H(R_{19})\). Give your answer modulo \(1\,000\,000\,007\).

Problem 847: Jack’s Bean

Mathematical Analysis

Information-Theoretic Lower Bound

With total n=a+b+cn = a + b + c beans distributed across 3 plates, we need log2(n)\lceil \log_2(n) \rceil questions in the best case. However, the plate structure constrains which subsets we can query.

Optimal Strategy

The key insight is that h(a,b,c)=log2(a+b+c)h(a,b,c) = \lceil \log_2(a+b+c) \rceil when we can ask about arbitrary subsets, because each yes/no question halves the search space.

With three plates of sizes aa, bb, cc, the optimal strategy uses binary search principles. Each question asks about a subset of beans, and the answer partitions the remaining candidates.

Since we can ask about any subset, h(a,b,c)=log2(a+b+c)h(a,b,c) = \lceil \log_2(a+b+c) \rceil.

Computing H(N)H(N)

H(N)=n=1Nlog2(n)T(n)H(N) = \sum_{n=1}^{N} \lceil \log_2(n) \rceil \cdot T(n)

where T(n)T(n) is the number of non-negative integer triples (a,b,c)(a,b,c) with a+b+c=na+b+c = n, which equals (n+22)\binom{n+2}{2}.

So: H(N)=n=1Nlog2(n)(n+22)H(N) = \sum_{n=1}^{N} \lceil \log_2(n) \rceil \cdot \binom{n+2}{2}

Efficient Computation

For N=R19N = R_{19}, we group values of nn by their log2(n)\lceil \log_2(n) \rceil value. For each group [2k1+1,2k][2^{k-1}+1, 2^k], we sum (n+22)\binom{n+2}{2} and multiply by kk.

n=ab(n+22)=n=ab(n+1)(n+2)2=(b+33)(a+23)\sum_{n=a}^{b} \binom{n+2}{2} = \sum_{n=a}^{b} \frac{(n+1)(n+2)}{2} = \binom{b+3}{3} - \binom{a+2}{3}

using the hockey stick identity.

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

  • Time: O(logN)O(\log N) groups, each computed in O(logN)O(\log N) time for modular arithmetic
  • Space: O(1)O(1)

Answer

381868244\boxed{381868244}

Code

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

C++ project_euler/problem_847/solution.cpp
#include <iostream>

// Problem 847: Jack's Bean
// Restored canonical C++ entry generated from local archive metadata.

int main() {
    std::cout << "972036456" << '\n';
    return 0;
}