Optimal schemes for combinatorial query problems with integer feedback

Abstract: A query game is a pair of a set $Q$ of queries and a set $\mathcal{F}$ of functions, or codewords $f:Q\rightarrow \mathbb{Z}.$ We think of this as a two-player game. One player, Codemaker, picks a hidden codeword $f\in \mathcal{F}$. The other player, Codebreaker, then tries to determine $f$ by asking a sequence of queries $q\in Q$, after each of which Codemaker must respond with the value $f(q)$. The goal of Codebreaker is to uniquely determine $f$ using as few queries as possible. Two classical examples of such games are coin-weighing with a spring scale, and Mastermind, which are of interest both as recreational games and for their connection to information theory.In this paper, we will present a general framework for finding short solutions to query games. As applications, we give new self-contained proofs of the query complexity of variations of the coin-weighing problems, and prove new results that the deterministic query complexity of Mastermind with $n$ positions and $k$ colors is $\Theta(n \log k/ \log n + k)$ if only black-peg information is provided, and $\Theta(n \log k / \log n + k/n)$ if both black- and white-peg information is provided. In the deterministic setting, these are the first up to constant factor optimal solutions to Mastermind known for any $k\geq n^{1-o(1)}$.

\section{Introduction}

In 1960, the following problem was posed by Shapiro and Fine \cite{SFproblemE1399}: ``\emph{Counterfeit coins weigh 9 grams and genuine coins all weigh 10 grams. One is given $n$ coins of unknown composition, and an accurate scale (not a balance). How many weighings are needed to isolate the counterfeit coins?}'' They observed that, for $n\geq 4$, fewer than $n$ weighings were needed and conjectured that, in general, $o(n)$ weighings would suffice.

In the years following this, coin-weighing became the subject of much independent work. To summarize the findings, we distinguish between \emph{static} strategies where the weighings are fixed before learning the answers, and \emph{adaptive} strategies where each weighing may depend on answers to previous weighings. In the static case, it is known that the minimum queries needed is
$$ \left(2 + \OO\left(\frac{\log \log n}{\log n}\right)\right) \frac{n}{\log_2 n},$$
where the lower bound is due to \cite{ERcoin,MoserL} and upper bounds are due to constructions given by Cantor-Mills \cite{cantor1966determination} and Lindstr\"om \cite{lindstrom1964combinatory,lindstrom1965combinatorial}. It was also shown by Erd\H{o}s and R\'enyi \cite{ERcoin} that a sequence of weighings chosen uniformly at random is likely to be able to distinguish between all configurations of fake coins, albeit at a slightly higher number of $(\log_2 9 + o(1))n/\log_2 n$ weighings. See also the last section of their paper for an overview of further early works on this topic.

For adaptive strategies, the picture is less clear. In either the static or adaptive case, lower bounds are formed by considering upper bounds on the amount of information that can be gained in one weighing, assuming the hidden configuration of fake coins is chosen uniformly at random. In the static case, the outcome of any weighing is binomially distributed and is thus concentrated on $\sqrt{n}$ values, bounding the entropy of a query by $\sim \log_2 \sqrt{n} = \frac12 \log_2 n$. In the adaptive case, no analogous guarantees for the probability distribution are known, which only leaves the general upper bound of $\log_2(n+1)$, which holds for any distribution on $\{0, 1,\dots, n\}$. Thus we can bound the minimum number $T$ of adaptive weighings according to
$$\frac{n}{\log_2 (n+1)} \leq T \leq \left(2+\OO\left(\frac{\log\log n}{\log n}\right)\right)\frac{n}{\log_2n},$$
where the upper bound is given by the static strategies. The residual gap of a factor $2$ remains an important open problem to this day. In particular, it is completely unknown if and how adaptiveness can be used to achieve a meaningful speed-up compared to the static schemes.

In order to understand better the discrepancy between static and adaptive strategies, it is natural to investigate similar games where the difference is more emphasized. A natural example is the popular board game Mastermind, as introduced commercially in the 1970s, and formalized in the mathematics literature in 1983 by Chv\'atal \cite{chvatal1983mastermind}. One player, Codemaker, assembles a hidden code word of length $n$ where each entry is one out of $k$ colors. It is then the task of the second player, Codebreaker to determine the code word by making a sequence of guesses. To each guess, Codebreaker is awarded a number of black and white pegs. For any code word $f$ and query $q$ in $[k]^n$, these are given by
$$bp(f, q) := |\{i\in[n] : f_i=q_i\}|,$$
and
$$wp(f, q) := \max_{\sigma} |\{i\in[n] : f_i=q_{\sigma(i)}\}| - bp(f, q),$$
where $\sigma$ goes over all permutations of $[n]$. In other words, the number of black pegs denotes the number of correctly guessed indices, and the number of pegs of a correct color but in an incorrect position respectively. The game is over once Codebreaker has correctly guessed the code word.

As $2$-color Mastermind and coin-weighing are essentially equivalent, see \cite{ST04}, it is natural to think of Mastermind as a direct generalization of coin-weighing. For $2\leq k \leq n^{1-\varepsilon}$, the picture for Mastermind is similar to that of coin-weighing\footnote{Unless explicitly specified otherwise, Mastermind will always refer to black-peg Mastermind. This is as the only information that can in principle be extracted from white-peg information - the number of times each color appears in the code word, has neglectibly small entropy compared to the code word as a whole in most interesting cases. Thus white-peg information will not make a meaningful difference here.}. For this parameter range it was shown by Chv\'atal that the minimum number of queries $T$ needed in either the static or adaptive case satisfies
$$ \frac{n\log_2 k}{\log_2 n+1} \leq T = \OO\left( \frac{n \log k }{\log n - \log k}\right),$$
where the lower bound is the general information-theoretic bound, and the upper bound is due to extending the randomized construction by Erd\H{os} and R\'enyi. Notably, these bounds differ only by a constant factor depending on $\varepsilon$. Moreover, static Mastermind has been of independent interest due to its equivalent formulation as the so-called metric dimension or rigidity of Hamming graphs. In this context, it has been shown by Jiang and Polyanskii \cite{smallstaticmm2} that
$$ \left(2 + \OO\left(\frac{\log \log n}{\log n}\right)\right)\frac{n}{\log_k n}$$
static guesses are optimal for any constant $k\geq 2$.

For $n=k$, the situation becomes more complex. While the general information-theoretic lower bound is simply $n$, for a long time the best (adaptive) strategies known such as the one presented by Chv\'atal and the constant factor optimizations given by Chen, Cunha and Homer \cite{CCH96}, and later Goodrich \cite{Goo09} all needed $\OO(n \log n)$ queries. It was first in 2016 that a paper by Doerr, Doerr, Sp\"ohel  and Thomas \cite{DDST16} managed to narrow this gap. Two notable results from their paper is that the optimal static strategy requires $\Theta(n \log n)$ queries, and secondly they gave an adaptive strategy that determines the code word in $\OO(n \log \log n)$ queries, thus giving the first known strategy to perform asymptotically faster than any static solutions. Finally, a recent work of the author together with Su \cite{MSup22+} resolved the problem in the randomized setting by giving a random adaptive strategy that determines the code word with high probability after $\OO(n)$ queries.

In this paper, we will take a more general approach to guessing games with integer feedback, such as coin-weighing and Mastermind. We will introduce a framework that allows us to express various guessing games, and adaptive solutions thereof in a common language. A virtue of this framework is that it allows one to separate game-specific solutions to subtasks from the general parallelized structure needed to obtain the information-theoretic speed-up. Our main technical results, Theorems \ref{thm:mainsimple} and \ref{thm:mainbounded} give sufficient conditions for games to have fast adaptive solutions.

As applications of our main results, we present a sequence of self-sufficient proofs of the existence of, up to constant, optimal deterministic adaptive strategies for various guessing games. In particular, using our results, we are able to determine the deterministic query complexity of Mastermind in both the black-peg and white-peg settings.
\begin{theorem}\label{thm:blackpegmm} Black-peg Mastermind with $n$ positions and $k\geq 2$ colors has deterministic query complexity $\Theta\left( n\frac{\log k}{\log n} + k\right).$
\end{theorem}
\begin{theorem}\label{thm:whitepegmm} White-peg Mastermind with $n$ positions and $k\geq 2$ colors has deterministic query complexity $\Theta\left(n\frac{\log k}{\log n} + k/n\right)$.
\end{theorem}
Randomized versions of these statements where strategies only need to recover the code word with high probability have already been shown by Martinsson and Su \cite{MSup22+}. In the deterministic setting, these are the first known optimal solutions for any $k\geq n^{1-o(1)}$.

It can further be remarked that, although not emphasized in our proofs, all proofs of existence of solutions given in this article are constructive and can be turned into efficient algorithms, including efficient decoding of the hidden code word. In this respect, our results also improve upon the previous strategies found through the probabilistic method by Chv\'atal in the range $2\leq k \leq n^{1-\varepsilon}$. In comparison, we remark that the algorithmic problem of determining the hidden code word from a general sequence of query-answer pairs is known to be NP-hard in both the black-peg and white-peg settings \cite{SZ06,Goo09,Vig11}.

As part of deriving the strategy for white-peg Mastermind, we further determine the query complexity of a new sparse version of the coin-weighing problem called \emph{sparse set query}, where the player is faced with a collection of $k$ coins and is told at most $n$ are counterfeit, for some parameters $k\geq n$. The problem is to determine which coins are counterfeit through few weighings, under the condition that at most $n$ coins may be placed on the scale at a time.
\begin{theorem}\label{thm:sparsesetquery}
For any $n \leq k$, the query complexity of sparse set query is\\ $\Theta\left(n \frac{\log(1+k/n)}{\log n} + k/n\right)$.
\end{theorem}

As a warm-up to proving these results, we will further derive up to constant factor optimal solutions to variations of the coin-weighing problem, and give a simplified proof for the existence of a $\OO(n)$ solution to black-peg Mastermind with $k=n$ in the case where the hidden code word is required to be a permutation (but queries may still contain repeated colors).

In a sibling paper, we will further discuss how to use this framework to find optimal solutions for Permutation Mastermind, where $k=n$ and guesses and code words are both forbidden to contain repeated colors.

The remaining part of the paper will be structured as follows. In Section \ref{sec:querygame} we will introduce the abstract framework and the two technical main results, Theorems \ref{thm:mainsimple} and \ref{thm:mainbounded} that will be used to derive the aforementioned results. In Section \ref{sec:applications} we will discuss applications of the main technical results, including deriving Theorems \ref{thm:blackpegmm}, \ref{thm:whitepegmm} and \ref{thm:sparsesetquery}. Theorems \ref{thm:mainsimple} and \ref{thm:mainbounded} will be proven in Sections \ref{sec:simpleproof} and \ref{sec:boundedproof} respectively. Finally, in Section \ref{sec:conclusion} we make some concluding remarks about our results.
\section{Query games}\label{sec:querygame}

A \emph{query game}, $A=(Q, \mathcal{F})$, is a pair of a set $Q$, and a non-empty set of functions $\mathcal{F}$ where $f:Q\rightarrow \mathbb{Z}$ for all $f\in\mathcal{F}$. We call the elements of $Q$ \emph{queries} and the elements of $\mathcal{F}$ \emph{code words}. For technical reasons, we always require that query games contain a zero-query $0\in Q$ such that $f(0)=0$ for all $f\in \mathcal{F}$.

We can think of $A=(Q, \mathcal{F})$ as a two-player game as follows. The first player, Codemaker, first chooses a code word $f\in \mathcal{F}$ without telling the other player the choice. The second player, Codebreaker, must then determine $f$ by asking a sequence of queries $q(1), q(2),\dots $, where, after each query $q(t)$, Codemaker must respond with the value of $f(q(t))$. Throughout this paper, we will assume that the sequence of queries can be made \emph{adaptively}, meaning that the choice of a query $q(t)$ may depend on the answers $f(q(1)), \dots, f(q(t-1))$ to all previous queries. The game is won after $T$ steps if $f$ is uniquely determined by Codemaker's responses to $q(1), \dots, q(T)$. That is, if, for every $f'\in \mathcal{F}\setminus\{f\}$, there exists a $1\leq t \leq T$ such that $f'(q(t))\neq f(q(t))$.

We will use upper case letters $A, B, C, \dots$ to denote query games, and bold upper case letters $\A, \B, \C, \dots$ to denote sets of query games. We will below treat two games as identical if they are \emph{isomorphic} in the sense that they only differ by a relabelling of their queries.

Given two query games $A=(Q_A, \mathcal{F}_A)$ and $B=(Q_B, \mathcal{F}_B)$, we define the \emph{sum game} $A+B$ as the game $(Q_A\times Q_B, \mathcal{F}_A\times \mathcal{F}_B)$, where we denote by $q_A+q_B$ and $f_A+f_B$ the pairs formed by $(q_A, q_B)\in Q_A \times Q_B$ and $(f_A, f_B)\in \mathcal{F}_A\times \mathcal{F}_B$ respectively, and where we define
$$(f_A + f_B)(q_A + q_B) := f_A(q_A)+f_B(q_B).$$ In other words, $A+B$ is the game in which Codemaker picks a pair of code words, one from each of $A$ and $B$. Codebreaker makes a query about $A$ and $B$ at the same time, and gets told the sum of the responses. For sets of query games $\A$ and $\B$, we define $\A+\B := \{A+B: A\in\A, B\in\B\}.$

We remark that the reason we require query games to contain zero-queries is to ensure that we can always query individual terms in a sum game $A+B$ according to $q_A+0$ and $0+q_B$ respectively.

\begin{example} A classical example of a query game is coin-weighing with $n$ coins. We can define this formally as $C_n:=(2^{[n]}, 2^{[n]})$, where we think of a code word $f\in 2^{[n]}$ as a function $2^{[n]}\rightarrow \mathbb{Z}$ by defining $f(q) := |f\cap q|.$ In other words, Codemaker presents a pile of $n$ coins, and tasks Codebreaker to determine which coins are counterfeit. In each query, Codebreaker is allowed to pick an arbitrary subset of the coins and ask how many in the given set are fake.

It can be seen that this game, up to isomorphisms, satisfies
$$C_n = n\cdot C_1,$$
where $n\cdot C_1$ denotes the $n$-fold sum $C_1+\dots+ C_1$, and, for any $1 \leq k \leq n-1$, we similarly have
$$C_{n} = C_k + C_{n-k}.$$
\end{example}

For a given game $A=(Q_A, \mathcal{F}_A)$ a \emph{strategy} is any sequence of queries $\left(q(t)\right)_{t=1}^T$, where each query $q(t)=q(t, f(q(1)), \dots, f(q(t-1)))$ is allowed to depend on the responses of the preceding queries. We refer to $T$ as the length of the strategy. We will call a strategy a \emph{solution} if it uniquely determines any hidden code word $f\in\mathcal{F}_A$.

Another special case of strategies of interest to this paper is \emph{reductions}. The idea is that instead of constructing a strategy to solve a query game $A$ directly, we can consider strategies $\left( q(t)\right)_{t=1}^T$ such that, given the corresponding sequence of answers $f(q(1)), \dots f(q(T))$, we can transform the remaining problem of determining $f$ to solving some other query game $B=(Q_B, \mathcal{F}_B)\in \B$. More precisely, suppose given the answer sequence we can conclude that the hidden code word $f$ is contained in some set $\mathcal{F}'\subseteq \mathcal{F}_A$. Then we consider the query game reduced to $B$ if each code word $f'\in \mathcal{F}'$ can be identified with a unique code word $g'\in \mathcal{F}_B$ such that $f'$ and $g'$ are identical up to relabelling and possibly discarding of elements in $Q_A$. This can be stated formally as follows.

\begin{definition}
Let $A=(Q_A, \mathcal{F}_A)$ be a query game, and $\B$ a set of query games. We say that $A$ can be reduced to $\B$ in $T$ queries, denoted $A\xrightarrow{T}\B$, if there exists a strategy $\left(q(t)\right)_{t=1}^T$ of length $T$ for $A$, with the following property. For any possible answer sequence $\mathbf{a}=(f(q(1)), \dots,f(q(T)))$, there exists a choice of a game $B=(Q_B, \mathcal{F}_B)\in\B$, depending only on $\mathbf{a}$, and maps $\varphi=\varphi_\mathbf{a}:Q_B\rightarrow Q_A$ and $\Phi=\Phi_\mathbf{a}:\mathcal{F}_B\rightarrow\mathcal{F}_A$ such that
\begin{enumerate}[(i)]
\item $f'\circ \varphi \in \mathcal{F}_B$, and
\item $\Phi(f'\circ\varphi) = f'$
\end{enumerate}
for any $f'\in\mathcal{F}_A$ consistent with the answer sequence $\mathbf{a}$.

We denote by $\A\xrightarrow{T} \B$ that $A\xrightarrow{T} \B$ for all $A\in\A$. With slight abuse of notation, we will write $A \xrightarrow{T} B$ and $\A \xrightarrow{T} B$ to denote reduction to the set $\{B\}$.
\end{definition}

Arguably, the natural way to construct reduction maps of explicit games is to first determine how to identify the code words of $A$ that are still possible with code words of $B$, and then use this to determine maps $\varphi:Q_B\rightarrow Q_A$, and $\Phi:\mathcal{F}_B\rightarrow\mathcal{F}_A$ such that $f'(\varphi(q))=g'$ and $\Phi(g')=f'$ for all identified pairs of code words $f'$ and $g'$ and all $q\in Q_B$. However, we note that the formal definition does this in another order. The map $\varphi$ first describes how to convert queries of $B$ to queries of $A$. By property $(i)$, we know that asking any query from $Q_B$ in the game $A$ with hidden code word $f$ will return answers consistent with asking the same query in $B$ with hidden code word $g:=f\circ \varphi \in \mathcal{F}_B$. By property $(ii)$, once we determine this code word $g$ in $B$, the code word of the original game is given by $\Phi(g)$. Thus, any not yet ruled out code word $f'$ of $A$ is implicitly identified with the code word $f'\circ \varphi \in \mathcal{F}_B$, where the map $\Phi$ ensures that no two code words of $A$ are paired with the same code word in $B$.

\begin{example}For any query game $A=(Q_A, \mathcal{F}_A)$, any $q\in Q_A$ can be seen as a reduction of length one to the set of games $$A\xrightarrow{1}\{ (Q_A, \{f\in \mathcal{F}_A : f(q)=a\})\},$$
where $a$ goes over all values $\{f(q) : f\in\mathcal{F}_A\}$, with $\varphi$ and $\Phi$ both being identity maps. Moreover, for any two query games $A, B$ such that $B$ has a solution $\left(q(t)\right)_{t=1}^T$, we have the reduction
$$A+B\xrightarrow{T}A,$$
formed by the strategy $0+q(t)$ for $t=1, \dots, T$, $\varphi(q):=q+0$ and $\Phi_\mathbf{a}(f):=f+f_\mathbf{a}$, where $f_\mathbf{a}$ is the unique code word in $B$ consistent with the answer sequence $\mathbf{a}$.
\end{example}

We will refer to the query game whose only query is the zero-query and whose only code word is the map $0\mapsto 0$ as the \emph{trivial game} and denote it by $\emptyset$.
\begin{proposition} Let $T, S\geq 1$ and let $\A, \B$ and $\C$ be sets of query games. The following holds:
\begin{enumerate}[(i)]
\item $\A \xrightarrow{T} \emptyset$ if and only if $\A$ can be solved in $T$ queries,
\item if $\A \xrightarrow{T} \B$ and $\B\xrightarrow{S}\C$ then the strategy formed by concatenating the respective reductions forms a reduction $\A \xrightarrow{T+S} \C$.
\end{enumerate}
\end{proposition}
\begin{proof}

$(i)$ Reduction to the trivial game means that, for any $f'\in \mathcal{F}_A$ consistent with a certain answer sequence $\mathbf{a}=(f(q(1), \dots, f(q(T)))$, we have $f'=\Phi_\mathbf{a}(f'\circ \varphi_\mathbf{a})$. But as $f' \circ \varphi_\mathbf{a} \in \mathcal{F}_\emptyset = \{0\mapsto 0\}$, it follows that $f'=\Phi_\mathbf{a}(0\mapsto 0)$ is uniquely determined by $\mathbf{a}$. Hence the hidden code word is given by $\Phi_\mathbf{a}(0\mapsto 0)$.
 Conversely, any solution can be seen as a reduction to $\emptyset$ by putting $\varphi\equiv 0$ and $\Phi_a\equiv f_\mathbf{a}$ where $f_\mathbf{a}$ denotes the unique solution consistent with answers $\mathbf{a}$.

$(ii)$ Given any $A=(Q_A, \mathcal{F}_A)\in\A$, we start by playing the strategy $\left( q(t) \right)_{t=1}^T$ from $A\xrightarrow{T}\B$ and based on the answer sequence $\mathbf{a}$ compute the corresponding game $B=(Q_B, \mathcal{F}_B)$, and the maps $\varphi:Q_B\rightarrow Q_A$ and $\Phi:\mathcal{F}_B\rightarrow\mathcal{F}_A$. We proceed by making the queries as dictated by the strategy $\left(q'(s)\right)_{s=1}
^S$ from the reduction $B\xrightarrow{S}\C$, according to $\varphi(q'(1)), \varphi(q'(2)), \dots, \varphi(q'(S))$ and based on the answer sequence $\mathbf{b}$ compute the corresponding game $C=(Q_C, \mathcal{F}_C)$, and the maps $\varphi':Q_C\rightarrow Q_B$ and $\Phi':\mathcal{F}_C\rightarrow\mathcal{F}_B.$ Note that for any choice $f\in \mathcal{F}_A$ of the hidden code word, asking any query $\varphi(q)$ for $q\in Q_B$ is consistent with asking the question $q$ in $B$ with hidden code word $g:=f\circ \varphi \in \mathcal{F}_B$, hence $\mathbf{b}$, $C$, $\varphi'$ and $\Phi'$ will all be given as if strategy $q'(s)$ was played on $B$ with hidden code word $g$.

We now argue that this is a well-defined reduction from $A$ to $\C$, as given by the game $C$ and the maps $\varphi \circ \varphi'$ and $\Phi \circ \Phi'$ as given above. Indeed, for any function $f\in \mathcal{F}_A$ consistent with the full answer sequence $(\mathbf{a}, \mathbf{b})$, that is, $f(q(t))=a(t)$ for all $1 \leq t \leq T$ and $f(\varphi(q'(s)))=b(s)$ for all $1 \leq s \leq S$, we have by the first reduction $g:=f\circ \varphi \in \mathcal{F}_B$. By assumption on $f$, we have that $g$ is consistent with answers $\mathbf{b}$ and hence $g\circ \varphi' \in \mathcal{F}_C$. It follows that $\Phi(\Phi'(f \circ \varphi \circ \varphi') )= \Phi(\Phi'(g\circ \varphi')) = \Phi(g) = \Phi(f\circ \varphi)= f,$ as desired.
\end{proof}

With these definitions at hand, we are now ready to present the main technical results of the paper. Given a sequence $\left(\A_n\right)_{n\geq 1}$ of sets of query games, it is natural to ask for a simple condition for the games to allow for short solutions. Our results give conditions for this to hold in terms of finding certain reductions. As we shall see in Section \ref{sec:applications}, such reductions appear naturally in many games.

For the first result, we say that a query game $A$ has a simple reduction to $\B$ of length $T$, $A\srightarrow{T}\B$, if there is a reduction $\left(q(t)\right)_{t=1}^T$ such that $f(q(t))\in \{0, 1\}$ for all $f\in \mathcal{F}_A$ and all $1\leq t \leq T$.
\begin{theorem}\label{thm:mainsimple} Let $\left(\A_n\right)_{n\geq 1}$ be a sequence of sets of query games over the integers. If there exists a constant $\alpha>0$ such that
$$\A_1 \srightarrow{\alpha} \emptyset,$$
and, for all $n\geq 2$
$$\A_n \srightarrow{\alpha \cdot n} \A_{\lceil n/2\rceil} + \A_{\lfloor n/2 \rfloor},$$
then $\A_n\xrightarrow{O(n)} \emptyset.$
\end{theorem}
This will be proven in Section \ref{sec:simpleproof}. Note that this approach can be seen as a variation of the classical divide and conquer method. However, if one were to do all reductions indicated above to solve $\A_n$ in sequence, this would result in a solution of length $\Theta(\alpha\cdot n \log n)$. Instead, the solutions generated by Theorem \ref{thm:mainsimple} will compress the $\Theta(\alpha \cdot n \log n)$ simple queries into $\Theta(\alpha \cdot n)$ actual queries with the same information content.

Theorem \ref{thm:mainsimple} relies on us being able to quickly identify zero-one queries to use as the basic building block to generate efficient solutions. In some query games, for instance coin-weighing and Mastermind with permutation code words, such queries appear naturally. However, in other cases, such as for instance Mastermind with general code words, identifying suitable zero-one queries fast does not seem tractable. Our second result states that the zero-one condition can be somewhat relaxed.

We say that a reduction $\left(q(t)\right)_{t\geq 1}$ from $A$ to $\B$ is (adaptively) bounded if there exists a function $b(t)=b(t, f(q(1)), \dots f(q(t-1)))$ such that $0 \leq f(q(t)) \leq b(t)$ for all $t$. We say that this reduction has \emph{weight at most $T$}, $A\brightarrow{T}\B$, if we have $$\sum_{t} \log_2(b(t)+1)^2\leq T,$$
for any choice $f$ of the hidden code word.

We have the following result.
\begin{theorem}\label{thm:mainbounded} Let $\left(\A_n\right)_{n\geq 1}$ be a sequence of sets of query games over the integers. If there exists a constant $\alpha>0$ such that
$$\A_1 \brightarrow{\alpha} \emptyset,$$
and, for all $n\geq 2$
$$ \A_n \brightarrow{\alpha\cdot n} \A_{\lceil n/2\rceil} + \A_{\lfloor n/2 \rfloor},$$
then $\A_n\xrightarrow{O(n)} \emptyset.$
\end{theorem}
This will be shown in Section \ref{sec:boundedproof}. We remark that, with a more careful analysis, $\log_2( \cdot )^2$ could be replaced by a function of smaller growth. However, as this will not make a difference for our applications, we will not elaborate on this. Furthermore, as any simple reduction is by definition also bounded, Theorem \ref{thm:mainsimple} can be seen as a direct consequence of Theorem \ref{thm:mainbounded}. Nevertheless, we will prove the two theorems independently. In part as the proof of the former is simpler, and can thus form as a warm-up for the more general latter case. But also, if one is interested in the strategies produced, the ones coming from the first result are arguably much simpler, and definitely shorter.
\section{Applications}\label{sec:applications}

In this section, we will show how Theorems \ref{thm:mainsimple} and \ref{thm:mainbounded} can be used to prove the existence of efficient solutions to various classes of query games. We believe that these theorems apply in much greater generality, and we hope that this section can serve as inspiration to find solutions to other query games. In addition to being a powerful tool, our framework also has the benefit that a deep understanding of the underlying coin-weighing schemes is not needed to build solutions.

For any query game $(Q, \mathcal{F})$, we formally define the \emph{information-theoretic} lower bound as the expression
\begin{equation}\label{eq:qgamelb}
\log_2 |\mathcal{F}| / \log_2 \left( \max_{q\in Q} |\{ f(q) : f\in \mathcal{F}\}|\right).
\end{equation}
A simple counting argument shows that any deterministic solution to $(Q, \mathcal{F})$ needs at least this many queries. We will below give multiple examples of query games where our main technical results show that the information-theoretic lower bound is sharp up to constant factors.

\subsection{Coin-weighing variations}\label{ssec:coin}

Recall that $C_n$, coin weighing with $n$ coins, is the query game $$C_n:=(2^{[n]}, 2^{[n]}),$$ where $f(q)=|f\cap q|$ for all $f, q \in 2^{[n]}$. The information-theoretic lower bound of this query game is given by $n/\log_2(n+1)$. Multiple solutions that solve this problem within a constant factor of this lower bound have been given in \cite{ERcoin,cantor1966determination,lindstrom1965combinatorial}.

We will now apply Theorem \ref{thm:mainsimple} to reprove this statement.

\begin{proposition}\label{prop:Cn} $C_n \xrightarrow{ \OO(n/\log_2 (n+1))} \emptyset.$\end{proposition}
\begin{proof}
As Theorem \ref{thm:mainsimple} always gives winning strategies of length $\OO(n)$, we aim to apply it to a sequence $\left(C_{\Theta(n \log_2 n)}\right)_{n\geq 1}$. The key condition to verify is the existence of a simple reduction of length $\OO(n)$ from coin-weighing with $\Theta(n \log_2 n)$ coins to $\Theta( (n/2)\log_2(n/2)+(n/2)\log_2(n/2))$ coins. But as $$(n/2)\log_2(n/2)+(n/2)\log_2(n/2) = n\log_2 n - n,$$
this can be done by weighing $\Theta(n)$ coins one by one, and dividing the remaining ones into two piles of appropriate size.

Let us make this formal. We define $k_n$ recursively by $k_1=1$ and, for all $n\geq 2$, $k_n = n + k_{\lceil n/2 \rceil} + k_{\lfloor n/2\rfloor}$. It can be checked that this implies that $k_n=n\left(1+\lfloor \log_2 n \rfloor\right)$. Thus by the aforementioned strategy,
$$C_{k_n} \srightarrow{n} C_{k_n - n} = C_{k_{\lceil n/2 \rceil}} + C_{k_{\lfloor n/2 \rfloor}},$$
and clearly
$$C_{k_1}=C_1 \srightarrow{1} \emptyset.$$
Hence, by Theorem \ref{thm:mainsimple}, coin-weighing with $k_n=\Theta(n \log n)$ coins can be solved in $\OO(n)$ queries.
\end{proof}

An important observation is that the above proof directly extends to a general $n$-fold sum of query games with short simple or bounded solutions.

\begin{proposition}\label{prop:Asum} Let $\A$ be any set of query games such that $\A \brightarrow{\alpha} \emptyset$. Then the $n$-fold sum $n\cdot \A$ can be solved in $\OO(\alpha\cdot n/\log (n+1) )$ queries.
\end{proposition}
\begin{proof}
Let $\B_n:=k_n\cdot \A$, where $k_n$ is as in the proof of Proposition \ref{prop:Cn}. We have that $\B_1=\A\brightarrow{\alpha}\emptyset$, and $$\B_n = k_n\cdot \A \brightarrow{\alpha\cdot n} (k_n-n)\cdot \A = (k_{\lceil n/2 \rceil} + k_{\lfloor n/2 \rfloor})\cdot\A = \B_{\lceil n/2 \rceil} + \B_{\lfloor n/2 \rfloor},$$
and it follows by Theorem \ref{thm:mainbounded} that $\B_n$, that is, an $\OO(n \log_2 n)$-fold sum of $\A$-games can be solved in $\OO(\alpha n)$ queries.
\end{proof}

In fact, we can extend this speed up also to sequences of query games which cannot be directly expressed as repeated sums of small games, but where the splitting operation can be performed in sublinear time. This can be seen as analogous to the leaf-heavy regime of the Master Theorem for divide-and-conquer recurrences.

\begin{proposition}\label{prop:leafheavy} Let $c>0$ be fixed. Let $\A_n$ be a sequence of sets of query games such that $$\A_n \brightarrow{\alpha\cdot n^{1-c}} \A_{\lceil n/2 \rceil} + \A_{\lfloor n/2 \rfloor}$$ for all $n\geq 2$ and $\A_1 \brightarrow{\alpha}\emptyset$, for some $\alpha>0$. Then $$\A_n \xrightarrow{\OO_c(\alpha \cdot n/ \log (n+1))}\emptyset.$$\end{proposition}
\begin{proof}
We begin by performing the sequence of elementary splitting operations
$$ \A_n\brightarrow{} \A_{\lceil n/2\rceil}+\A_{\lfloor n/2\rfloor}\brightarrow{}\dots \brightarrow{} \A_{\Theta(\sqrt{n})} + \dots + \A_{\Theta(\sqrt{n})},$$
until what remains is a sum of $2^k=\Theta(\sqrt{n})$ query games, each with parameter $\Theta(n/2^k) = \Theta(\sqrt{n})$, where $k\geq 0$ is an appropriately chosen integer. The total weight of this sequence of reductions is
$$\sum_{i=0}^{k-1} 2^i \cdot \alpha \cdot \Theta\left( (n/2^i)^{1-c} \right) = \Theta_c(\alpha \cdot n^{1-c} \cdot 2^{ck} ) = \Theta_c(\alpha \cdot n^{1-c/2}),$$
which means it can be done in $\OO_c(\alpha \cdot n^{1-c/2})$ queries.

We now solve the remaining games in parallel. Let $T_k$ denote the minimum weight such that $\A_k\brightarrow{T_k}\emptyset$. We have, by assumption, that $T_1\leq \alpha$ and $T_k \leq \alpha \cdot k^{1-c} + T_{\lceil k/2 \rceil}+T_{\lfloor k/2 \rfloor}$, which implies that $T_k = \OO_c(\alpha \cdot k)$. Applying Proposition \ref{prop:Asum} to the remaining game, with $n'=\Theta(\sqrt{n})$ equal to the number of terms and $\alpha' = \OO_c( \alpha \sqrt{n} )$ equal to the maximum weight needed to solve each individual term, it follows that $\A_n$ can be solved in $\OO_c( \alpha n^{1-c/2} + \alpha n /\log (n+1))=\OO_c(\alpha n/\log (n+1))$ queries, as desired.

\end{proof}

Given the above general statements at hand, we now turn to a sparse version of coin-weighing as proposed by Dja\v{c}kov \cite{djackov} and Lindstr\"om \cite{Lindstrom75}, where Codebreaker is informed on beforehand the total number, $d$, of counterfeit coins. Formally, we can describe this as the query game
\begin{equation}
C_{n,d} := \left(2^{[n]}, {[n] \choose d}\right),
\end{equation}
for any $1\leq d \leq n$, and where, again, $f(q):=|f\cap q|$ for all $q\in 2^{[n]}$ and $f\in {[n]\choose d}$.

For $d\leq n/2$, it can be seen that the information-theoretic lower bound is given by $\log_2 {n \choose d} / \log_2(d+1)$, and, by symmetry, for $n\geq n/2$ the bound is given by $\log_2 {n \choose d} / \log_2(n-d+1)$. We remark in the latter case that even though queries can return any value between $0$ and $d$, no one fixed query can span this full range when $d>n/2$.

A further generalization of sparse coin-weighing is to allow the coins to have arbitrary non-negative integer weights where the total weight of the coins is known from the start of the game. Let us denote by $C_n^W$ this version of coin-weighing with $n$ coins where the total weight of coins are $W$. Here, the information-theoretic lower bound is given by
$$\log_2 {W+n-1 \choose n-1} / \log_2(W+1)=\begin{cases} \Omega\left( \log\left(1+\frac{n}{W}\right) W / \log(W+1)\right) & \text{if }W\leq n,\\\Omega\left( \log\left(1+\frac{W}{n}\right) n / \log(W+1)\right) & \text{if }W\geq n.\end{cases}$$

Coin-weighing with exactly one fake coin is of course a very classical problem, and is canonically solved by binary search. The reader is referred to \cite{bshouty} for an overview of research in the case of $d=2$. In the general case, it was shown in 2000 by Grebinski and Kucherov \cite{GK00} that the information-theoretic lower bounds for $C_{n,d}$ and $C_n^W$ are tight up to constant factors in the full parameter range. As their construction is based on the probabilistic method, it is inherently non-constructive, and it took until 2009 before the first deterministic polynomial time solutions of the same lengths were found by Bshouty \cite{bshouty}.

We will now use Proposition \ref{prop:leafheavy} to derive optimal solutions to $C_{n,d}$ for all $n, d$ and for $C_n^W$ in the case where $W=\OO(n)$.

\begin{proposition}\label{prop:sparsecoin} Let $n, d, W\geq 1$ be such that $d\leq n$. Then
\begin{enumerate}[(i)]
\item $C_{n,d}$ can be solved in $\OO( \log {n \choose d} / \log(\min(d+1, n-d+1)))$ queries, and
\item $C_n^W$ can be solved in $\OO\left( \max(1, \log(n/W) ) W/\log(W+1)\right)$ queries.\end{enumerate}
\end{proposition}
\begin{proof}
By symmetry between real and counterfeit coins, it suffices to show $(i)$ for $d\leq n/2$, in which case the upper bound simplifies to $\OO( d \log(n/d) / \log (d+1))$. This is a special case of $(ii)$ for $W=d$. Hence, the proposition follows if we can prove $(ii)$. To do this, let $\lambda\geq 2$ and define $$\A_m^\lambda:=\{C_{n,W} : W+n/\lambda \leq 2 m\}.$$ We wish to apply Proposition \ref{prop:leafheavy} to the sequence $\left(\A_m^\lambda\right)_{m\geq 1}$.

We first claim that $\A_1^\lambda \brightarrow{\OO(\log \lambda)}\emptyset$. This is because the only non-trivial games in this set is $C_{n}^1$ for $2\leq n \leq \lambda$, which can be solved in $\lceil \log_2 n\rceil$ simple queries using binary search. Second, let $m\geq 2$ and let $C_n^W$ be any query game in $\A_m^\lambda$. Note that $W\leq 2m$ and $n \leq 2\lambda m$. To build our reduction, first determine the largest integer $0\leq i \leq n$ such that $$ i + f(\{1, 2, \dots, i\}) / \lambda \leq 2\lceil m/2\rceil.$$
As the left-hand side is increasing in $i$, this can be found in $\OO(\log n)$ queries using binary search. Given this $i$, we define $L:=\{1, 2, \dots, i\}$ and $R:=\{i+2,\dots n\}$ and note that the subgames formed by the coins in $L$ and $R$ are in $\A_{\lceil m/2 \rceil}^\lambda$ and $\A_{\lfloor m/2\rfloor}^\lambda$ respectively. Hence, by possibly performing one last query to determine the value of coin $i+1$, we have the desired reduction. As the answer to any query used here is at most $W\leq 2m$, we can bound the weight of this reduction by $\OO(\log (\lambda m) (\log m)^2) = \OO(\log \lambda) \cdot \text{polylog}(m).$

Applying Proposition \ref{prop:leafheavy} to $\A_{m}^\lambda$ with $\alpha=\OO(\log \lambda))$ and, say, $c=\frac12$, it follows that $\A_m^\lambda \xrightarrow{\OO(\log \lambda \cdot m / \log (m+1))}\emptyset.$ For any given $n, W\geq 1$, the result follows by noting that $C_n^W\in \A_W^\lambda$ where $\lambda:=\max\left(2, n/W\right)$.

\end{proof}

\subsection{Mastermind with permutation code word}

We now turn our attention to Mastermind. In this subsection, as a warm-up, we consider the simplified version of black-peg Mastermind where the number of slots and possible colors are both $n$, and where the code word chosen by Codemaker is required to be a permutation of $[n]$.

We will formalize this in the query game setting as
$$M^{\text{perm}}_n = ( (\{0\} \cup [n])^n, S_n),$$
where $S_n$ denotes the set of permutations of $[n]$, and where we interpret each $f \in \mathcal{S}([n])$ as a function from the queries to the natural numbers according to
$$f(q):= |\{i : q_i=f_i\}|.$$
We remark that the query game formulation differs slightly from the game mentioned above in that Codebreaker may guess blank at certain positions of queries by putting $q_i=0$.

This problem was previously considered by the author and Su in \cite{MSup22+} where it was shown that it can be solved (deterministically) in $\OO(n)$ queries. Indeed, as the information-theoretic lower bound is $\log n! / \log( n+1) = \Omega(n)$ queries, this is optimal up to constants. In the same paper, it was shown that black-peg Mastermind with $n=k$ has a randomized reduction to this game (Lemma 3.4, \cite{MSup22+}).

We first prove that we can simulate blank guesses by performing a few additional queries.
\begin{claim}\label{claim:szero} If $M_n^{\text{perm}}\xrightarrow{T}\emptyset$, then black-peg Mastermind with a permutation code word (without black guesses) can be solved in $T+2\lceil \log_2 n \rceil$ queries.
\end{claim}
\begin{proof}
Observe that if we can identify a query $b \in [n]^n$ that has no matching entries with the hidden code word, we can simulate guessing black by replacing each instance of $q_i=0$ by the value $b_i$. So it suffices to show that such a $b$ can be found in $2 \lceil \log_2 n\rceil$ queries.

For each $i=1, \dots, \lceil \log_2 n\rceil$, we make two queries. In the first query, we guess $1$ at all indices where the $i$:th least significant bit in its binary representation is a $0$ and color $2$ where the bit is a $1$. In the second, we reverse the colors.

We claim that one of these queries must return $0$. As the hidden code word $f$ is a permutation, there are unique indices $j_1, j_2$ such that $f_{j_1}=1$ and $f_{j_2}=2$. Considering the binary representations of $j_1$ and $j_2$, we see that we get a zero query for each $i$ such that $j_1$ and $j_2$ differ in their $i$:th least significant bit.
\end{proof}

\begin{proposition} $M_n^\text{perm} \xrightarrow{\OO(n)} \emptyset$.
\end{proposition}
\begin{proof}
We prove this using Theorem \ref{thm:mainsimple}. Note that $M_1^\text{perm}$ is trivial -- the only hidden code word is ``$1$'', so we only need to consider the reduction of $M_n^\text{perm}$ for $n\geq 2$.

Consider the strategy of length $n$ formed by, for each $1\leq t \leq n$ taking the query
$$q(t) = (t, \dots, t, 0, \dots, 0)$$
which is $t$ in the first $\lceil n/2\rceil$ entries, and $0$ everywhere else, that is, it is $0$ in the last $\lfloor n/2 \rfloor$ entries. Depending on whether color $t$ is in the left or right half of the code word, the $t$:th query will either return $1$ or $0$, so this strategy is simple. Moreover, given this information, we can treat the remaining game as a sum of two instances of Mastermind whose code words are each a permutation of the colors $t$ that returned $1$ and $0$ respectively, hence yielding a reduction to $M_{\lceil n/2 \rceil}^\text{perm}+M_{\lfloor n/2 \rfloor}^\text{perm}$.
\end{proof}

\subsection{Black-peg Mastermind}

We now turn to the proof of Theorem \ref{thm:blackpegmm}. Consider full black-peg Mastermind game with $n$ positions and $k$ colors, with no restrictions on the code word. The formalized version of this game was first considered by Chv\'atal \cite{chvatal1983mastermind}. We will here formalize black-peg Mastermind as the query game
$$M_{n,k} := ( (\{0\} \cup [k])^n, [k]^n),$$
where, same as the previous section, we define
$f(q):=|\{i:f_i=q_i\}|$.
We will write $M_n$ to denote $M_{n,n}$.

Analogous to the case of permutation code words, the formalization as a query game assume that Codebreaker has the ability to make blank guesses. We will show below how this can be simulated in the classical game by using a slightly more elaborate argument than in the last section.

We have two natural lower bounds for the number of queries needed to solve. First, the query game has the information-theoretic lower bound $n\log k / \log(n+1)$. Second, for any index $i\in [n]$ it may be that the value of $f_i$ is one of the two last colors to ever get queried in position $i$, meaning that no strategy is guaranteed to determine the code word in fewer than $k-1$ queries. We note that the former bound dominates when $k=o(n)$, the latter dominates when $k=\omega(n)$, and the bounds are comparable when $k=\Theta(n)$.

Qualitatively, our approach will be similar to the permutation code word case. The main challenge of extending the argument from the previous section is that we no longer have an a priori guarantee that certain queries must return small answers. To remedy this, we will make sure that we know how many times each color appears in each sub-game we generate, which will allow us to generate suitable bounded queries.

We first show that any solution to the game $M_{n,k}$ carries over to the classical game, that is without blank guesses.

\begin{claim}\label{claim:bzero}If $M_{n,k} \xrightarrow{T}\emptyset$, then black-peg Mastermind with $n$ slots and $k\geq 2$ colors can be solved in $T+\OO(n/\log n)$ queries.
\end{claim}
\begin{proof}
Analogous to Claim \ref{claim:szero}, it suffices to find a query $b\in [k]^n$ whose answer is $0$ in $\OO(n/\log n)$ queries. In fact, we will do something stronger here. Namely, determining for each $i\in [n]$ whether $f_i=1, f_i=2,$ or $f_i\in \{3, \dots, n\}.$

We first query the all ones string. Let us denote this by $\mathbf{1}$. After this, for any query $q\in \{1, 2\}^n$ we note that
$$f(q)-f(\mathbf{1})+|\{i\in [n] : q_i=2\}| = \sum_{i=1}^n  \mathbbm{1}_{c_i=q_i}-\mathbbm{1}_{c_i=1}+\mathbbm{1}_{q_i=2},$$
where we note that the summand in the right-hand side is always $0$ for $q_i=1$, and for $q_i=2$ it is $0$ if $f_i=1$, it is $2$ if $f_i=2$ and it is $1$ if $f_i\neq 1, 2$. Hence this problem can be seen as equivalent to coin-weighing with weights $0, 1$ and $2$. This can be solved in $\OO(n/\log n)$ queries using Proposition \ref{prop:Asum}.
\end{proof}

Given this result, in order to prove Theorem \ref{thm:blackpegmm}, it suffices to show that $M_{n,k}$ can be solved in $\OO(n \log k / \log n + k)$ queries. For this, we consider two cases. First, if $k\leq n^{1-\varepsilon}$ for some fixed $\varepsilon>0$, we can solve the game efficiently using sparse coin-weighing. This will be done in Proposition \ref{prop:mmsparse}. Second, if $k\geq n^\varepsilon$ one can observe that the prescribed upper bound simplifies to $\OO(n+k)$. In this case, the problem can be reduced to solving $M_n$ (see Observation \ref{obs:mm}). Finally, $M_n$ will be solved in Proposition  \ref{prop:mmn} through a direct application of Theorem \ref{thm:mainbounded}.

\begin{proposition}\label{prop:mmsparse}
For any $2 \leq k\leq n^{1-\varepsilon}$, the game $M_{n,k}$ can be solved in $\OO_\varepsilon(n \log k / \log n)$ queries.
\end{proposition}
\begin{proof}
For each color $c\in [k]$, we ask the all `$c$' query to determine how many times $d_c$ the color $c$ appears in the hidden code word. For each color that appears at least once, we use Proposition \ref{prop:sparsecoin} to determine the positions of color $c$ among the $n-\sum_{c'<c} d_{c'}$ positions whose color has not yet been identified.

It remains to show that the number of queries,
$$k + \sum_{c\,:\,d_c>0} \OO\left( \log {n-\sum_{c'<c} d_{c'} \choose d_c}/\log \min(d_c+1,n-d_c+1)\right)$$
has the prescribed upper bound. For this we consider three cases. First, there could be one color $c$ for which $d_c>n/2$. This will in worst case take as long as the dense coin-weighing problem, that is $\OO(n/\log n)$. Second, the contribution from colors $c$ such that $\sqrt{n/k}\leq d_c \leq n/2$ is at most $$\OO_\varepsilon\left( \sum_c \log {n-\sum_{c'<c} d_{c'} \choose d_c}/\log n\right) = \OO_\varepsilon\left( \log {n \choose d_1, d_2, \dots, d_k} / \log n\right),$$
where the multinomial coefficient can be bounded by $k^n$. Finally, the contribution from colors $c$ where $d_c\leq \sqrt{n/k}$ is at most $\OO(k\cdot \sqrt{n/k} \; polylog(n))=\OO(n^{1-\varepsilon/2}polylog(n))$, which is neglectibly small.
\end{proof}

\begin{observation}\label{obs:mm}
If $M_n \xrightarrow{T}\emptyset$, then $M_{n,k}\xrightarrow{T}\emptyset$ for any $2\leq k \leq n$ and $M_{n,k}\xrightarrow{T+k}\emptyset$ for any $k>n$.
\end{observation}
\begin{proof}
Any strategy for $n$ colors can directly be transferred to $k<n$ colors by replacing any occurrence of a color not in $[k]$ by $0$. On the other hand, if $k>n$ we can start by querying how many times each color appears in the code word in total. As the code word has length $n<k$, at most $n$ colors actually occur, and we can treat the remaining game as having at most $n$ colors.
\end{proof}

With these statements at hand, it only remains to show that $M_n$ can be solved in $\OO(n)$ queries. In doing so, we will consider a variation of $M_n$ where Codebreaker is given \emph{hints} consisting of how many times each color appears in the code word. Formally, we can write this as the set of query games
$$\MM_n:=\{ M_n(c_1, c_2, \dots) : c_1+c_2+\dots=n\},$$
where $M_n(c_1, c_2, \dots)$ denote the query game formed by restricting $M_n$ to only those code words which has $c_1$ occurrences of color $1$, $c_2$ of color $2$ and so on. For ease of notation, we will allow that $c_k>0$ for some $k>n$, though, clearly, the number of colors that appear a non-zero number of times is always at most $n$.

\begin{proposition}\label{prop:mmn}$M_n \xrightarrow{\OO(n)} \emptyset.$
\end{proposition}
\begin{proof}
Note that we have the reduction $M_n \xrightarrow{n} \MM_n$ formed by querying $(t, t, \dots, t)$ for each $1\leq t \leq n$. We now aim to use Theorem \ref{thm:mainbounded} to show that $\MM_n\xrightarrow{\OO(n)}\emptyset.$ Similar to the permutation case $\MM_1$ is trivially solved as the only possible code word is ``$1$''. So it suffices to find a bounded reduction of $\MM_n$ for $n\geq 2$.

Fix any $M_n(c_1, c_2, \dots)\in \MM_n$. To form the reduction $M_n(c_1, c_2, \dots)\brightarrow{} \MM_{\lceil n/2\rceil}+\MM_{\lfloor n/2 \rfloor}$, we make the following queries. For each $i \geq 1$ such that $c_i>0$, query $(i, \dots, i, 0, \dots, 0)$, where, similar to the previous subsection, the blocks denote the first $\lceil n/2 \rceil$ and the last $\lfloor n/2 \rfloor$ positions respectively. As we already know the total number of occurrences $c_i$ of color $i$ in the code word, we can use this as an upper bound for this query.

Given this information, we can determine the number of times $c_i'$ each color $i$ appears in the left half of the code word, and how many times $c''_i = c_i-c'_i$ it appears in the right half, which directly defines the split $M_{\lceil n/2 \rceil}(c'_1, c'_2, \dots) + M_{\lfloor n/2 \rfloor}(c''_1, c''_2, \dots) \in \MM_{\lceil n/2\rceil}+\MM_{\lfloor n/2 \rfloor}$, as desired.

Finally, in order to show that $\MM_n\xrightarrow{\OO(n)}\emptyset$, we need to show that the weight of this reduction is $\OO(n)$. We have the bound
$$\sum_{i\,:\,c_i>0} \log_2(c_i+1)^2 \leq \sum_{i\,:\,c_i>0} \OO(c_i) = \OO(n).$$
\end{proof}

\subsection{White-peg Mastermind and sparse set query}

We now turn to determining the query complexity of Mastermind when both black- and white-peg information is provided for each query. In other words, in addition to the black-peg information we are now also told the total number of pegs, which can be described as the matching entries between the code word $f$ and query $q$ one can attain by reordering the entries of $q$.

Let us first discuss a bit how the inclusion of white-peg information changes the problem. With respect to the information-theoretic lower bound, a query can now return any pair of numbers $0 \leq b+w\leq n$, hence decreasing the bound from $n\log_2 k / \log_2(n+1)$ to $n\log_2 k / \log_2{n+2 \choose 2}$. In other words, the bound decreases by roughly a factor of $2$. As a consequence, the strategy we already derived for black-peg Mastermind is optimal up to constant factors when $k=\OO(n)$ also for white-peg Mastermind.

In fact, for $k=o(n)$, the picture is even clearer. Observe that any solution to white-peg Mastermind can be turned into a solution to black-peg Mastermind by first making $k$ queries to determine the number of times each color appears in the hidden code word. As the query complexity of black-peg Mastermind is $\omega(k)$ in this range, it follows that the length of optimal solutions to black- and white-peg Mastermind only differ by a factor $\left(1+o(1)\right)$ in this range.

Instead, the regime where white pegs shine are when $k=\omega(n)$. Unlike for black-peg Mastermind where Codebreaker may be required to guess all (but one) color in each position to determine the code word, in this case Codebreaker can in principle learn about occurrences of a certain color in the code word by making queries that contain said color in \emph{any} position. This lowers the combinatorial lower bound from $\Omega(k)$ to $\Omega(k/n)$. In this section, we will show that $\OO(n+k/n)$ queries suffices to solve white-peg Mastermind. Together with Theorem \ref{thm:blackpegmm}, this determines the query complexity of white-peg Mastermind for all $n$ and $k$.

To build our solution, we will start by determining which colors are actually present in the hidden code word by observing the total number of (black and white) pegs returned from each query. In order to approach this, we will consider the game sparse set query, as described in the introduction. For any $k \geq n$, we can define this as a query game according to
$$X_{k,n} := \left( {[k]\choose \leq n}, {[k] \choose \leq n}\right),$$
where $f(q) := |f\cap q|.$ We can think of this as a variation of sparse coin-weighing with $k$ coins, where we know that at most $n$ coins are fake, and a weighing can only fit at most $n$ coins.

Analogous to Mastermind, there are two natural lower bounds to the number of queries needed to solve this. On the one hand, the information-theoretic bound is $\log \left( \sum_{i=0}^n {k \choose i}\right) / \log (n+1)$ which is $\Omega( n \log \frac{k}{n} / \log (n+1) )$ if $k\geq 2n$ and $\Omega(k/\log(n+1) )$ if $n \leq k< 2n$. On the other hand, we need at least $\lceil k/n \rceil$ queries to weigh each coin at least once.

It is not too hard to see that the game can be solved in $\OO(n/\log(n+1))$ queries when $n \leq k < 2n$. For instance, one may divide $[k]$ into two sets each with at most $n$ elements and apply any optimal scheme to the classical coin-weighing problem. So in order to solve this game optimally, it suffices to consider the case when $k \geq 2n$. We will divide this further into two subcases. First, if $k$ is polynomially smaller than $n^2$, say $2n \leq k \leq n^{2-\varepsilon}$ for some $\varepsilon>0$, then a direct application of sparse coin-weighing schemes shows that the information theoretic lower bound is sharp. This will be done in Proposition \ref{prop:nottoosparsesetquery}. Second, if $k \geq n^{2-\varepsilon}$ we can simplify the general lower bound to $\Omega_\varepsilon(n + k/n)$. As we can always pretend to add additional dummy elements to the sets, we may in this latter case always assume that $n \geq k^2$, without affecting the lower bound. We will show in Proposition \ref{prop:balancedsetquery} that $\OO(n + k/n)$ queries suffice in this case. This will conclude the proof of Theorem \ref{thm:sparsesetquery}.

\begin{proposition} \label{prop:nottoosparsesetquery} Let $\varepsilon>0$ be fixed. If $2n \leq k \leq n^{2- \varepsilon}$, then $X_{k,n}\xrightarrow{\OO_\varepsilon (n \log (k/n)/\log n)}\emptyset.$
\end{proposition}
\begin{proof}
We build our solution as follows. Partition $[k]$ into $\lceil k/n \rceil$ sets $D_1, D_2, \dots$ of size at most $n$. For each part $D_i$ we first query the full set to determine the number $d_i$ of fake coins, and then use the sparse coin-weighing scheme from Proposition \ref{prop:sparsecoin} to determine which coins are fake. The number of queries needed for this is
$$\lceil k/n \rceil + \sum_{i=1}^{\lceil k/n \rceil} \OO\left(\log{|D_i| \choose d_i}/\log (d_i+1)\right).$$

In order to bound this expression, let us distinguish terms in this sum depending on the size of $d_i$. If $d_i < n^{\varepsilon/2}$, we can bound the summand by $\OO(d_i \log n)=\OO(n^{\varepsilon/2} \log n)$, and thus the total contribution from such terms is at most $\OO((k/n) \cdot n^{\varepsilon/2} \log n) \leq \OO( n^{1-\varepsilon/2} \log n)$, which is polynomially smaller than $n$. In the case when $d_i \geq n^{\varepsilon/2}$, we have $ \log (d_i+1) = \Theta_\varepsilon(\log n)$, and thus the contribution from these terms are at most
$$\frac{1}{\log n} \OO_\varepsilon \left(\log\left( \prod_{i=1}^{\lceil k/n\rceil} {|D_i| \choose d_i}\right)\right) \leq \frac{1}{\log n} \OO_\varepsilon \left(\log{k \choose n}\right) = \OO_\varepsilon(n \log(k/n) / \log n),$$ as desired.
\end{proof}
\begin{proposition}\label{prop:balancedsetquery}
For any $k\geq n^2,$ we have $X_{k,n}\xrightarrow{\OO(k/n)} X_{n^2,n}$ and for any $n\geq 1$ we have $X_{n^2,n}\xrightarrow{\OO(n)}\emptyset$.
\end{proposition}
\begin{proof}
The first reduction can be done by querying the elements in groups of $n$ and discarding elements in sets that return $0$ until only $n^2$ elements remain.

For the solution to $X_{n^2,n}$, we will apply Theorem \ref{thm:mainbounded}. We start similarly to black-peg Mastermind by making a sequence of queries to obtain upper bounds of parts of the code word. In this case, fix a balanced $n$-partitioning $[n^2]=D_1\cup \dots \cup D_n$ and query these sets. This forms a reduction $X_{n^2,n} \xrightarrow{n} \mathbf{X}_{n^2,n}$ to the set of games where the number of elements in common between each set $D_i$ and the code word is fixed. Note that by definition $\mathbf{X}_{1^2,1}$ is trivial, so again it suffices to find a bounded splitting reduction for $n\geq 2$.

Fixing a game $X=X\vert_{f(D_1)=d_1, f(D_2)=d_2, \dots} \in \mathbf{X}_{n^2,n}$ in which the code word is restricted to satisfying $f(D_i)=d_i$ for all $i\in [n]$, we form this reduction as follows. For each $i\in[n]$ such that $d_i>0$, we partition $D_i$ into three parts $D_i=D_{i,1}\cup D_{i,2} \cup D_{i,3}$ each of size at most $\lfloor n/2 \rfloor$, and query the first two parts to determine $f(D_{i,1}), f(D_{i,2})$, and $f(D_{i,3})=f(D_i)-f(D_{i,1})-f(D_{i,2})$. Given these function values, we greedily build two lists $L$ and $R$ consisting of sets with non-zero overlap with the code word, under the condition that the total overlap between $f$ and $L$ is at most $\lceil n/2\rceil$ and the total overlap between $f$ and $R$ is at most $\lfloor n/2 \rfloor$, and a set $Z$ consisting of the union of all sets with no overlap with $f$. Note that as any set added to $L$ and $R$ contains at least one element, this implies that $|L|\leq \lceil n/2\rceil$ and $|R|\leq \lfloor n/2 \rfloor$.

This procedure will leave at most one set $D_{i,j}$ with non-trivial overlap with $f$ that does not fit in either $L$ nor $R$ -- if two such sets $D, D'$ existed, then the total overlap of $L \cup D$ and $f$ is greater than $\lceil n/2\rceil$ and the total overlap between $R\cup D'$ and $f$ is greater than $\lfloor n/2\rfloor$, which is a contradiction as the total overlap is at most $n$. In case such a set $D_{i,j}$ occurs, we resolve it by querying its elements one at the time and distribute the singletons between $L$, $R$ and $Z$.

Finally, we use the elements of $Z$ to extend the lists $L$ and $R$ until they contain $\lceil n/2 \rceil$ sets of size $\lceil n/2 \rceil$ and $\lfloor n/2 \rfloor$ sets of size $\lfloor n/2 \rfloor$ respectively. This gives us a reduction to a sum of sparse set query on $\cup_i L_i$ and $\cup_i R_i$ respectively, with $L$ and $R$ describing the hints for the sub-games.

Finally, we need to check that this reduction has weight at most $\OO(n)$. Indeed, using $d_i$ as upper bound when querying $f(D_{i,1})$ and $f( D_{i,2})$ gives a total weight of $2\sum_{i\,:\,d_i>0} \log(d_i+1)^2 = 2\sum_{i\,:\,d_i>0}\OO(d_i) = \OO(n)$. For querying singletons of the remaining set, we make $n$ queries with upper bound $1$, whose weight is clearly also $\OO(n)$, as desired.
\end{proof}

With an efficient solution to the sparse set game  at hand, we now turn to Theorem \ref{thm:whitepegmm}. As black-peg and white-peg Mastermind have the same query complexity for $k = \OO(n)$, the only case that remains to be treated is $k \geq n$, in which case the bound simplifies to $\OO(n+k/n)$.

\begin{proof}[Proof of Theorem \ref{thm:whitepegmm}.] As remarked, given Theorem \ref{thm:blackpegmm}, it remains to solve white-peg Mastermind in for $k\geq 2n$ in $\OO(n+k/n)$ queries. We will do so by simulating sparse set query to find the set of colors present in the hidden code word. Given this, we can apply the black-peg Mastermind algorithm to solve the rest in $\OO(n)$ time.

We begin by identifying a color, call it $z$, that never appears in the hidden code word. As $k>n$ we know that one such color exists. Indeed, by asking all `$i$' queries  for $i=1, 2, \dots, n+1$, at least one of these must return $0$ matches as answer.

Given this information, we next aim to determine the set $X\subset [k]$ of colors present in the hidden code word $f\in [k]^n$. Clearly $|X|\leq n$. For any set $Y\subseteq [k]$ of size at most $n$, let $q_Y \in [k]^n$ denote a query such that each color of $Y$ appears once in $q_Y$, and all remaining positions are filled up with the color $z$. The total number of black and white pegs returned for any query $q_Y$ equals $|X\cap Y|$, and thus by the preceding propositions, we can determine $X$ in $\OO( n\log (k/n)/ \log n + k/n)$ queries by simulating sparse set query.

Given this, we can apply the black-peg strategy from Theorem \ref{thm:blackpegmm} to exact code word after an additional $\OO(n)$ queries, as desired.
\end{proof}
\section{Proof of Theorem \ref{thm:mainsimple} }\label{sec:simpleproof}

We now turn to the problem of constructing fast winning strategies for query games satisfying the conditions of Theorem \ref{thm:mainsimple}. A strength of our abstract framework is that it allows us to separate game specific operations from the querying structure. As we shall see, once we have our main building blocks set up, the proof follows by a simple induction on $n$.

Before outlining our winning strategy, let us take a moment to reflect on what to aim for. The only operations allowed to us by the theorem is the splitting $$\A_k \srightarrow{\alpha\cdot k} \A_{\lceil k/2 \rceil}+\A_{\lfloor k/2\rfloor},$$  and solving any term $\A_1$ in $\alpha$ simple queries. By applying these operations sequentially, we would form a solution to $\A_n$ in $\OO(\alpha \cdot n \log n)$ simple queries, which is too slow. To speed this up, the aim is that once the game is split into sufficiently many terms we can run the reductions more efficient in parallel. Indeed, as the answer to each individual query is known to be either $0$ or $1$ this parallelism is very similar to classical coin-weighing.   A natural approach to improve this is to consider a sequence of reductions layer by layer $$\A_n \rightarrow \A_{n/2}+\A_{n/2} \rightarrow \A_{n/4}+\A_{n/4} + \A_{n/4}+\A_{n/4} \rightarrow \dots, $$
where reduction $i=1, \dots, \OO(\log n)$, can be performed in $\Theta(\alpha \cdot n/2^i \cdot 2^i/i)=\Theta(\alpha \cdot n / i)$ queries by using standard coin-weighing schemes. But as $\sum_{i=1}^{\OO(\log n)} \Theta(\alpha \cdot n /i) = \Theta(\alpha \cdot n \log \log n)$, this is still too slow. The reason why this is still not fast is that the coin-weighing paradigm gives a meaningful speedup at first when the game is already split into many terms. In fact, it only gives the necessary factor $\log n$ speedup once the game has been split into $n^{\Omega(1)}$ terms. While the layer-by-layer approach eventually attains this, it spends too much time initially to split all large (and therefore most costly to split) games at a time when no meaningful parallelism is possible. The way we remedy this is to consider an intermediate game $\PP_n$, the \emph{preprocessed game}, which is formed from $\A_n$ by recursively applying the splitting operation a fraction of terms as they appear in the game. This has the strength that, on the one hand, one can reduce $\A_n$ to $\PP_n$ using $\OO(\alpha \cdot n)$ \emph{simple} queries. On the other hand, $\PP_n$ consists of a sum of polynomially many $\A$-games, meaning we can more directly apply coin-weighing techniques.

Before formally introducing the preprocessed game, we start by stating the lemma that will allow us to run strategies in parallel. Analogous to how we can determine the value of four coins in three weighings, this lemma states that, in the setting of sums of query games we can run four strategies in parallel at the cost (i.e. number of queries) of three of them, provided one strategy is simple. By applying this recursively, we will be able to construct winning strategies. We will choose the exact definition of the preprocessed game below so that it matches this statement nicely.
\begin{lemma} \label{lma:coinspeedup} Let $T>0$ and let $\A, \B, \C, \D, \E$ be sets of query games such that $\A\xrightarrow{T} \emptyset$, $\B\xrightarrow{T} \emptyset$, $\C\xrightarrow{T} \emptyset$, $\D\srightarrow{T} \E$. Then $\A+ \B + \C + \D \xrightarrow{3T} \E$.
\end{lemma}
\begin{proof}
Given games $A, B, C,$ and $D$ from the respective sets, and strategies $\mathbf{q}_A$, $\mathbf{q}_B$, $\mathbf{q}_C$ and $\mathbf{q}_D$ as in the statement of the lemma, we define a strategy $\mathbf{q}=\left(q(t)\right)_{t=1}^{3T}$ on $A+B+C+D$ by, for each $t=1, \dots, T$, letting
\begin{align*}
q(3t-2) &:= 0+q_B(t)+q_C(t)+q_D(t)\\
q(3t-1) &:= q_A(t)+0+q_C(t)+q_D(t)\\
q(3t) &:= q_A(t)+q_B(t)+0+q_D(t),
\end{align*}
where for each $t$ and each $X\in\{A, B, C, D\}$, the subquery $q_X(t)$ is generated as if past queries have returned $f_X(q_X(1)), \dots, f_X(q_X(t-1))$ where $f=f_A+f_B+f_C+f_D$ denotes the hidden code word of the sum game.

To see that this is a well-defined strategy, we need to show that $f_X(q_X(t))$ can be uniquely determined by $f(q(1)), \dots, f(q(3t)$. To see this, note that, given the answers to the first $3t$ queries, we can compute
$$f(q(3t))+f(q(3t-1))-f(q(3t-2)) = 2\cdot f_A(q_A(t)) + f_D(q_D(t)),$$
and as $q_D$ is a simple strategy, we can determine $f_D(q_D(t))$ by considering the parity of this sum. Given this, we can compute
\begin{align*}
f(q(3t-2)) - f_D(q_D(t)) &= f_B(q_B(t)) + f_C(q_C(t)),\\
f(q(3t-1)) - f_D(q_D(t)) &= f_A(q_A(t)) + f_C(q_C(t)),\\
f(q(3t)) - f_D(q_D(t)) &= f_A(q_A(t)) + f_B(q_B(t)),
\end{align*}
from which $f_A(q_A(t)), f_B(q_B(t))$ and $f_C(q_C(t))$ can be determined by linear algebra.

Secondly, to show that $\mathbf{q}$ is indeed the desired reduction, note that as $\mathbf{q}_A$, $\mathbf{q}_B$  and $\mathbf{q}_C$ are solutions for their respective terms, the answers provided uniquely determine $f_A$, $f_B$  and $f^B$ of the code word. At the same time, the answers to $\mathbf{q}_D$ determines a game $E\in\E$ and maps $\varphi:Q_E\rightarrow Q_D$ and $\Phi:\mathcal{F}_E\rightarrow \mathcal{F}_D$. One immediately checks that the maps $q \mapsto 0+0+0+\varphi(q)$ for $q\in Q_E$ and $f' \mapsto f_A+f_B+f_C+\Phi(f')$ for $f'\in\mathcal{F}_E$ satisfy the definition of a reduction from $A+B+C+D$ to $\E$, as desired.
\end{proof}

\begin{remark}\label{remark:inductionstrategy}
The strength of Lemma \ref{lma:coinspeedup} lies in that it can be applied recursively. To illustrate this, consider any sets of games $\B_n$ such that $\B_n \srightarrow{3^n} \emptyset$ for all $n\geq 0$. We claim that this implies that
$$\B_n + 3 \cdot \B_{n-1} + 9 \cdot \B_{n-2} + \dots + 3^n \cdot \B_0 \xrightarrow{3^{n+1}} \emptyset.$$
Indeed, this can be shown by induction. For $n=0$ the statement is clearly true with room to spare, and for the induction step one may apply Lemma \ref{lma:coinspeedup} for $\A=\B=\C = \B_{n-1}+3\cdot \B_{n-2}+\dots+3^{n-1}\cdot \B_0$, $\D=\B_n$ and $\E=\emptyset$.
\end{remark}

With this lemma at hand, we are now ready to build our winning strategy. For a given sequence of sets of query games $\A_n$ as in Theorem \ref{thm:mainsimple}, we define the \emph{preprocessed} game $\PP_n$, as follows
\begin{equation}\label{eq:preproc}\PP_n:=\begin{cases} \emptyset & \text{if }n\leq 3,\\
\PP_{\lceil \lceil n/2\rceil /2 \rceil} + \PP_{\lfloor \lceil n/2\rceil /2 \rfloor} + \PP_{\lceil \lfloor n/2\rfloor /2 \rceil} + \A_{\lfloor \lfloor n/2\rfloor /2 \rfloor} & \text{if }n\geq 4.\end{cases}\end{equation}
We note that the indices of the four terms in the right-hand side is always equal to $\lceil n/4\rceil$ or $\lfloor n/4 \rfloor$. In particular, $\lceil \lceil n/2 \rceil /2\rceil$ is always equal to the former, and $\lfloor \lfloor n/2 \rfloor /2 \rfloor$ equal to the latter, and the sum of all four indices is $n$.

We now give a simple reduction from $\A_n$ to the preprocessed game. This will be used both as the first step of the solution of $\A_n$, and also as a building block for the second step when solving $\PP_n$.

\begin{claim}\label{claim:simplepreprocess} For any $n\geq 1$, $\A_n \srightarrow{8\alpha n - 7\alpha} \PP_n$.
\end{claim}
\begin{proof}[Proof by induction.] It is straightforward to check that the statement holds for $n\leq 3$. We have
\begin{align*}
\A_1 &\srightarrow{\alpha} \emptyset = \PP_1, \text{ thus }\A_1\srightarrow{\alpha} \PP_1\\
\A_2 &\srightarrow{2\alpha} \A_1 + \A_1 \srightarrow{\alpha + \alpha} \emptyset = \PP_2, \text{ thus }\A_2\srightarrow{4\alpha} \PP_2,\\
\A_3 &\srightarrow{3\alpha} \A_2 + \A_1 \srightarrow{4\alpha + \alpha} \emptyset = \PP_3, \text{ thus }\A_3\srightarrow{8 \alpha} \PP_3.
\end{align*}
For $n\geq 4$, we have the following sequence of reductions
\begin{align*}
\A_n &\srightarrow{\alpha n} \A_{\lceil n/2 \rceil} + \A_{\lfloor n/2 \rfloor} \\
&\srightarrow{\alpha \lceil n/2 \rceil + \alpha \lfloor n/2 \rfloor} \A_{\lceil \lceil n/2\rceil /2 \rceil} + \A_{\lfloor \lceil n/2\rceil /2 \rfloor} + \A_{\lceil \lfloor n/2\rfloor /2 \rceil} + \A_{\lfloor \lfloor n/2\rfloor /2 \rfloor}\\
&\srightarrow{8 \alpha \lceil 3n/4 \rceil-21\alpha} \PP_{\lceil \lceil n/2\rceil /2 \rceil} + \PP_{\lfloor \lceil n/2\rceil /2 \rfloor} + \PP_{\lceil \lfloor n/2\rfloor /2 \rceil} + \A_{\lfloor \lfloor n/2\rfloor /2 \rfloor}=\PP_n.
\end{align*}
By upper bounding $\lceil 3n/4 \rceil$ by $3n/4 + 3/4$, we see that the concatenation of the above steps forms a simple reduction from $A_n$ to $\PP_n$ in $8\alpha n - 15 \alpha \leq 8\alpha n - 7\alpha$, as desired.

\end{proof}

With this at hand, we are now ready to construct the solution to the preprocessed game.

\begin{claim}\label{claim:simplepostprocess} For any $n\geq 1$, $\PP_n \xrightarrow{8\alpha(n-1)} \emptyset$. \end{claim}
\begin{proof}[Proof by induction.] As $\PP_1=\PP_2=\PP_3=\emptyset$, the statement is trivial for $n\leq 3$. For $n\geq 4$, define $$T=\max\left(8\alpha\lceil n/4 \rceil-8\alpha, 8\alpha\lfloor n/4 \rfloor-7\alpha\right),$$ and note that, we have the reductions
$$\PP_n = \underbrace{\PP_{\lceil \lceil n/2\rceil /2 \rceil}}_{\xrightarrow{T} \emptyset} + \underbrace{\PP_{\lfloor \lceil n/2\rceil /2 \rfloor}}_{\xrightarrow{T} \emptyset} + \underbrace{\PP_{\lceil \lfloor n/2\rfloor /2 \rceil}}_{\xrightarrow{T} \emptyset} + \underbrace{\A_{\lfloor \lfloor n/2 \rfloor /2 \rfloor}}_{\srightarrow{T}\PP_{\lfloor \lfloor n/2 \rfloor /2 \rfloor}},$$
where the solutions of the first three terms follow from the induction hypothesis, and the reduction of the last term from Claim \ref{claim:simplepreprocess}.  Hence by Lemma \ref{lma:coinspeedup}, we get
$$\PP_n \xrightarrow{3T} \PP_{\lfloor n/4\rfloor} \xrightarrow{8\alpha(\lfloor n/4 \rfloor-1) } \emptyset,$$
where the last step is, again, by the induction hypothesis.

Hence, it suffices to check that the length of the concatenated strategy $3T+8\alpha(\lfloor n/4\rfloor -1)$ is always at most $8\alpha(n-1)$. Indeed
$$ 24\alpha\lceil n/4 \rceil - 24\alpha + 8\alpha (\lfloor n/4\rfloor -1) \leq 24\alpha (n+3)/4  - 24\alpha + 8\alpha ( n/4 -1) = 8\alpha n - 14 \alpha,$$
$$ 24\alpha\lfloor n/4 \rfloor - 21\alpha + 8\alpha (\lfloor n/4\rfloor -1) \leq 32 \alpha \cdot n / 4 -29 \alpha = 8\alpha n - 29 \alpha.$$

\end{proof}

To conclude the proof of Theorem \ref{thm:mainsimple} we note by Claims \ref{claim:simplepreprocess} and \ref{claim:simplepostprocess} that
$$ \A_n \srightarrow{8\alpha\cdot n-7\alpha} \PP_n \xrightarrow{8\alpha(n-1)} \emptyset,$$
which indeed combines to a solution of length $16\alpha \cdot n - 15\alpha$. \qed

\section{Proof of Theorem \ref{thm:mainbounded}}\label{sec:boundedproof}

In this section, we modify the arguments from the last section to allow for queries whose answer can be larger than one, provided sensible upper bounds on each query are known beforehand. We will construct the winning strategy in a very similar way to the previous section. We again use the preprocessed game \eqref{eq:preproc} and define our solution around it recursively as before. The main challenge is how to modify Lemma \ref{lma:coinspeedup} to allow for the simple reductions to be replaced by reductions that at least sometimes are allowed to make queries whose answers may be large.

We will approach this by showing that it is possible to replace the simple reduction in Lemma \ref{lma:coinspeedup} with a reduction $\left(q(t)\right)_{t=1}^T$ with the property that $f(q(t)) \in [0, r(t)]$ for all $1\leq t\leq T$ and for a carefully chosen (non-adaptive) function $r:\{1, 2, \dots\} \rightarrow \{0, 1, 2, \dots\}$. This comes with a few caveats. First, we can no longer allow arbitrary solutions for the first three terms, $\A, \B, \C$, but the solutions need to have additional structure. Second, the choice of the function $r(t)$ will depend on the solutions to $\A, \B$ and $\C$, so, rather, we will need to consider a set $\mathcal{R}$ and require that $r$-bounded reductions exist for all functions $r(t)$ in $\mathcal{R}$.

The set $\mathcal{R}$ we will work with is the set of functions $r(t)$ that satisfy that there exists a sequence $a_1, a_2, \dots$ of integers such that
$$r(t)=\begin{cases} 2^{2^i}-1 &\text{ if }t\equiv a_i \text{ mod }4^i\text{ for some }i\geq 1\\ 0 &\text{ otherwise}.\end{cases}$$
We say that $A$ has an $\mathcal{R}$-reduction to $\B$ in $T$ steps, $A\xrightarrow[\mathcal{R}]{T}\B$, if there exist $r(t)$-bounded reductions from $A$ to $\B$ in $T$ steps for all $r(t)\in\mathcal{R}$.

\begin{proposition}\label{prop:Rbounded}
If $A\brightarrow{T} \B$, then $A \xrightarrow[\mathcal{R}]{4T}\B$.
\end{proposition}
\begin{proof}
Given any bounded reduction $\left(q(t)\right)_{t\geq 1}$ with an adaptive upper bound $\left(b(t)\right)_{t\geq 1}$ where $\sum_t \log_2(b(t)+1)^2 \leq T$ and any $r(t)\in \mathcal{R}$ we construct an $r(t)$-bounded reduction $\left(q'(t)\right)_{t=1}^{4T}$ by appropriately padding with zeros: Initially let $s=1$. For each $t=1, 2, \dots$ until the bounded reduction terminates,  if $b(s)\leq r(t)$ we let $q'(t):=q(s)$ and increase $s$ by one. Otherwise, we let $q'(t):=0$ and do not update $s$.

As the new strategy does not change which queries are being made, it is still a reduction, and it is by construction $r(t)$-bounded. It suffices to show that this procedure always stops with $t\leq 4T$. Suppose at a time $t\geq 1$, the strategy has just made the query $q(s-1)$, and we know that $b(s) \leq 2^{2^i}-1$ for some suitable $i\geq 1$, then by definition of $\mathcal{R}$, the strategy will make the query $q(s)$ at most $4^i$ time steps later. For any $b(s)\geq 1$, we can always find such an $i\geq 1$ with $2^i \leq 2 \log_2(b(s)+1)$, or, equivalently, $4^i \leq 4 \log_2(b(s)+1)^2$. Hence, the total amount of time needed to make all queries is $4 \sum_s \log_2(b(s)+1)^2 \leq 4T$, as desired.

\end{proof}

The rest of the section will be dedicated to proving the following statement. Theorem \ref{thm:mainbounded} follows immediately by taking $\beta:=4\alpha$ and plugging in Proposition \ref{prop:Rbounded}.
\begin{theorem}\label{thm:stackable} Let $\mathcal{R}$ be as above, and let $\A_n$ be a sequence of sets of query games such that
$$\A_n \xrightarrow[\mathcal{R}]{\beta \cdot n} \A_{\lceil n/2 \rceil}+\A_{\lfloor n/2 \rfloor},$$
and $\A_1 \xrightarrow[\mathcal{R}]{\beta}\emptyset$ for some constant $\beta\geq 1$. Then $\A_n\xrightarrow{\OO(\beta \cdot n)}\emptyset.$
\end{theorem}

In order to show this, we need yet another special kind of reduction. For any function $p(t):\{1, 2, \dots\}\rightarrow\{1, 2, \dots\}$ we say that a game $A$ has a \emph{$p(t)$-predictable} reduction to $\B$ in $T$ queries, $A\xRightarrow[p]{T}\B$, if there exists a reduction such that the congruence class of $f(q(t))$ modulo $p(t)$ is uniquely determined by $f(q(1)), \dots, f(q(t-1))$ for all $1\leq t \leq T$.

In order to relate $p(t)$-predictable reductions to $r(t)$-bounded reductions, we need to introduce some additional structure. Let $\mathcal{Q}$ be the set of pairs of functions $(x(t), y(t))_{t\geq 1}$ such that
\begin{itemize}
\item $x(t)$ is a power of two and $0 \leq y(t) \leq x(t)-1$ for all $t\geq 1$, and
\item for any pair of integers $i\geq 1$ and $0\leq j \leq 2^i-1$, there exists a constant $a_{ij}$ such that
$$(x(t), y(t)) = (2^i, j) \Leftrightarrow t \equiv a_{ij}\text{ mod }4^i.$$
\end{itemize}
It is not too hard to see that such pairs of functions exist. For instance, one can imagine going through the pairs $(i, j)$ as above in lexicographical order and letting $a_{ij}$ be the smallest integer $a\geq 1$ such that $a \not\equiv a_{i'j'}$ mod $4^{i'}$ for any $(i', j') < (i, j)$. Such an $a$ always exists as the total density of integers covered by congruence classes $(i', j')<(i, j)$ is $\sum_{(i',j')<(i,j)} 4^{-i'} < 1$.

For any pair $(x(t), y(t))_{t\geq 1}\in \mathcal{Q}$ say that a reduction is $(x(t), y(t))$-bounded if it is $r(t)$-bounded for the function $r(t):= \mathbbm{1}_{y(t)=0} (2^{x(t)}-1)$. We say that a reduction is $(x(t), y(t))$-predictable if it is $p(t)$-predictable for $p(t):= 2^{y(t)}$. We will write $A \xrightarrow[\mathcal{Q}]{T} \B$ and $A \xRightarrow[\mathcal{Q}]{T} \B$ to denote that $(x(t), y(t))$-bounded reductions and $(x(t), y(t))$-predictable reductions respectively exists for all $(x(t), y(t))\in \mathcal{Q}$. Note that if $A\xrightarrow[\mathcal{R}]{T} \B$ then we also have $A\xrightarrow[\mathcal{Q}]{T} \B$.

\begin{lemma}\label{lma:Rcoinspeedup} If $\A\xRightarrow[\mathcal{Q}]{T} \emptyset$, $\B\xRightarrow[\mathcal{Q}]{T} \emptyset$, $\C\xRightarrow[\mathcal{Q}]{T} \emptyset$, and $\D \xrightarrow[\mathcal{Q}]{T} \E$, then $\A+\B+\C+\D \xRightarrow[\mathcal{Q}]{3T+6} \E$.
\end{lemma}
\begin{proof}
Let $A, B, C, D$ be query games from the respective sets, and let $(x(t), y(t))\in \mathcal{Q}$. We construct a $(x(t), y(t))$-predictable reduction from $A+B+C+D$ to $\E$ as follows.

Let $q_A(t)$, $q_B(t)$ and $q_C(t)$ denote $(x_A(t), y_A(t))$-predictable, $(x_B(t), y_B(t))$-predictable and $(x_C(t), y_C(t))$-predictable reductions where
\begin{equation}\label{eq:interlace}\begin{split}
(x_A(t), y_A(t)) &= (x(3t), y(3t)-1\text{ mod }x(3t)),\\
(x_B(t), y_B(t)) &= (x(3t+1), y(3t+1)-1\text{ mod }x(3t+1)),\\
(x_C(t), y_C(t)) &= (x(3t+2), y(3t+2)-1\text{ mod }x(3t+2)).
\end{split}\end{equation}
Here we use $y-1\text{ mod }x$ denotes $y-1$ if $y>0$ and $x-1$ if $y=0$.  It can be checked from the definition of $\mathcal{Q}$ that these sequences are contained in $\mathcal{Q}$. Let $q_D(t)$ be a $(x_{t},y_{t})$-bounded reduction from $D$ to $\E$.

We construct a reduction of length by $3(T+2)$ from $A+B+C+D$ to $\E$, by, for each $1\leq t \leq T+2$ letting
\begin{align*}
q(3t-2)&:=0+q_B(t-1)+q_C(t-2)+q_D(t)\\
q(3t-1)&:=q_A(t)+0+q_C(t-2)+q_D(t)\\
q(3t)&:=q_A(t)+q_B(t-1)+0+q_D(t),
\end{align*}
where we put $q_X(t):=0$ for $X\in\{A, B, C, D\}$ whenever $t\leq 0$ or $t>T$.

We first show that this is a well-defined reduction to $\E$. Let $f=f_A+f_B+f_C+f_d$ denote the hidden code word. Similar to Lemma \ref{lma:coinspeedup}, it suffices to prove that $f(q(1)), \dots, f(q(3t))$ uniquely determines the subqueries $f_A(q_A(t))$, $f_B(q_B(t-1)), f_C(q_C(t-2))$ and $f_D(q_D(t))$ for each $1\leq t\leq T+2$. We will show this by induction on $t$. For any given $1 \leq t \leq T+2$, we may assume that we have already determined $f_A(q_A(s))$ for all $s<t$, $f_B(q_B(s))$ for all $s<t-1$, and $f_C(q_C(s))$ for all $s<t-2$. Observe that it is sufficient to determine $f_D(q_D(t))$, as the answers to the remaining subqueries then can be extracted by linear algebra. In particular, this trivially holds for any $t$ such that $y(t)>0$ as then $f_D(q_D(t))=0$.

We will resolve the cases where $y(t)$ by considering the congruence of $t$ modulo $3$. If $t=3s$, then as $q_A(t)$ is $(x_A(t), y_A(t))$-predictable, we can determine uniquely the congruence class of $f_A(q_A(t))=f_A(q_A(3s))$ modulo $2^{y_A(s)}=2^{x(3s)-1}=2^{x(t)-1}$ from past subqueries. In particular, as
$$-f(q(3t-2)+f(q(3t-1)+f(q(3t)) = 2\cdot f_A(q_A(t)) + f_D(q_D(t)),$$
this allows us to determine uniquely $f_D(q_D(t))$ mod $2^{x(t)}$, and as $0 \leq f_D(q_D(t)) \leq 2^{x(t)}-1$, this uniquely determines $f_D(q_D(t))$.

Similarly, the cases $t=3s+1$ and $t=3s+2$ can be resolved by using the predictability of $q_B(t)$ and $q_C(t)$ together with
$$f(q(3t-2)-f(q(3t-1)+f(q(3t)) = 2\cdot f_B(q_B(t-1)) + q_D(t),$$
$$ f(q(3t-2)+f(q(3t-1)-f(q(3t)) = 2\cdot f_C(q_C(t-2)) + q_D(t).$$
We conclude that $q(t)$ is a well-defined reduction to $\E$.

Finally, we need to check that $q(t)$ is $(x(t), y(t))$-predictable, that is, $f(q(3t))$ mod $2^{y(t)}$ is uniquely determined from $f(q(1)), \dots, f(q(3t-1))$. Note that this is a trivial statement if $y(t)=0$. For any $t\geq 1$ such that $y(t)>0$ we know that $f_D(q_D(t))=0$ and again by case distinction on $t$ mod $3$, one can see that $f(q(1)), \dots f(q(3t-1))$ uniquely determine the congruence class of $f_A(q_A(t)), f_B(q_B(t-1))$ or $f_C(q_C(t-2))$ mod $2^{y(t)-1}$. The statement follows as before by linear algebra.
\end{proof}

\begin{proposition}\label{prop:Rconcat} Let $\mathcal{Q}$ be as above, let $T, S\geq 1$ and let $\A, \B$ and $\C$ be sets of query games. The following holds:
\begin{enumerate}[(i)]
\item If $\A\xrightarrow[\mathcal{Q}]{T} \B$ and $\B\xrightarrow[\mathcal{Q}]{S}\C$, then $\A\xrightarrow[\mathcal{Q}]{T+S}\C$,
\item if $\A\xRightarrow[\mathcal{Q}]{T}\B$ and $\B\xRightarrow[\mathcal{Q}]{S}\C$ and $T$ is divisible by three, then $A\xRightarrow[\mathcal{Q}]{T+S}\C.$
\end{enumerate}
\end{proposition}
\begin{proof} Let $(x(t), y(t))\in \mathcal{Q}$ be given. Observe that any linear shift $((x(t+a), y(t+a))$ is also contained in $\mathcal{Q}$. For $(i)$ we concatenate a $(x(t),y(t))$-bounded reduction from $\A$ to $\B$ with a $((x(t+T), y(t+T))$-bounded reduction from $\B$ to $\C$. For $(i)$ we concatenate a $((x(t), y(t))$-predictable strategy from $\A$ to $\B$ with a $(x(T/3+t), y(T/3+t))$-predictable strategy from $\B$ to $\C$.
\end{proof}

We now turn to proving Theorem \ref{thm:stackable}. Let $\A_n$ be any sequence of sets of query games such that
$$\A_n\xrightarrow[\mathcal{Q}]{\beta\cdot n} \A_{\lceil n/2 \rceil}+\A_{\lfloor n/2 \rfloor},$$
and $\A_1 \xrightarrow[\mathcal{Q}]{\beta} \emptyset$ for some $\beta\geq 1$. Similarly, as before, we define the preprocessed game $\PP_n$ according to
\begin{equation*}\label{eq:preproc2}\PP_n:=\begin{cases} \emptyset & \text{if }n\leq 3,\\
\PP_{\lceil \lceil n/2\rceil /2 \rceil} + \PP_{\lfloor \lceil n/2\rceil /2 \rfloor} + \PP_{\lceil \lfloor n/2\rfloor /2 \rceil} + \A_{\lfloor \lfloor n/2\rfloor /2 \rfloor} & \text{if }n\geq 4.\end{cases}\end{equation*}

The following statement is proven identically to Claim \ref{claim:simplepreprocess}, using Proposition \ref{prop:Rconcat} to concatenate the strategies.

\begin{claim}\label{claim:Rpreprocess} For any $n\geq 1$, $\A_n \xrightarrow[\mathcal{Q}]{8\beta n-7\beta}\PP_n.$\qed
\end{claim}

Secondly, we can show that $\PP_n$ has an $\mathcal{Q}$-predictable solution in $8\beta(n-1)$ time in the same way as Claim \ref{claim:simplepostprocess} but replacing Lemma \ref{lma:coinspeedup} by Lemma \ref{lma:Rcoinspeedup}. Note that the latter lemma adds $6$ additional queries to the length of the combined reduction, but the inequalities at the end of Claim \ref{claim:simplepostprocess} has room to spare for this.

\begin{claim} For any $n\geq 1,$ $\PP_n \xRightarrow[\mathcal{Q}]{8\beta(n-1)}\emptyset.$ \qed.
\end{claim}

By concatenating the reductions from the two claims above, we conclude that $A_n$ has a solution of length $8\beta n - 7 \beta + 8\beta(n-1) = 16 \beta n - 15 \beta$, which concludes the proof of Theorem \ref{thm:stackable}. \qed

\section{Conclusion}\label{sec:conclusion}

We end this paper by making some concluding remarks. As we have seen, the framework presented in Section \ref{sec:querygame} can be concisely applied to find optimal solutions to a wide range of guessing games over the integers. Hopefully, this paper can act as a guide to finding solutions to further games in the future.

For brevity, we have chosen in this paper to focus on proving that solutions exist, with little discussion on how these may look. However, it can be remarked that any solution given by Theorems \ref{thm:mainsimple} and \ref{thm:mainbounded} can be turned into an efficient algorithm, provided the elementary reductions have efficient implementations. More precisely, in order to show that a solution given by either of these theorems has a polynomial time implementation, it is sufficient to show that there is an efficient way to implement the reduction strategies $\left(q(t)\right)_{t=1}^T$, the pairs of query games $A+B$ produced by the reductions, and the maps $\varphi, \Phi$. In fact, the map $\Phi$ is only needed to ensure that decoding the code word can be done efficiently.

Also for brevity, we have not attempted to optimize constants in the solutions we have derived. For example, for black-peg Mastermind with $n$ colors and positions, the argument in the paper shows that the minimum number of queries needed lies between $n$ and $129n+o(n)$. It seems likely that the length of the optimal strategy for this problem grows as $c\cdot n$ for some small constant $c>1$. Resolving this completely may be difficult due to the long-standing remaining factor $2$ gap for adaptive coin-weighing, but it would be interesting to see how much this gap could be narrowed.

In our main technical results, Theorems \ref{thm:mainsimple} and \ref{thm:mainbounded}, we require the splitting operation to divide $\A_n$ exactly in half (up to rounding). It is natural to ask whether the same conclusion holds if we can only ensure an inexact split $$\A_n \brightarrow{\alpha \cdot n} \bigcup_{k=\lceil \varepsilon n\rceil}^{\lfloor(1-\varepsilon)n\rfloor} \left( \A_k + \A_{n-k} \right),$$
for some $0 < \varepsilon \leq \frac12$. Indeed, to show this, we can define $$\B_n:=\bigcup_{n_1+n_2+\dots = n} \left( \A_{n_1}+\A_{n_2}+\dots\right),$$
where $(n_1, n_2, \dots)$ go over all integer compositions of $n$. By iteratively applying the inexact splitting for $\A_n$ we obtain a bounded reduction $\B_n \brightarrow{\OO(\varepsilon^{-1} \alpha \cdot n)} \B_{\lceil n/2 \rceil}+\B_{\lfloor n/2 \rfloor}$, which implies $\B_n\xrightarrow{\OO(\varepsilon^{-1} \alpha \cdot n) } \emptyset$, and thus $\A_n\xrightarrow{\OO(\varepsilon^{-1} \alpha \cdot n)}\emptyset$.

Another way the main technical results can be strengthened is to consider what happens if the solution to $\A_1$ has a high weight, say $\A_1\brightarrow{\beta} \emptyset$ for some large $\beta>0$. In this case, we can still use the strategy provided by Theorem \ref{thm:mainbounded} in $\OO(\alpha\cdot n)$ queries to reduce $\A_n$ to $n\cdot \A_n$. Then using Proposition \ref{prop:Asum}, we can solve this sum using $\OO(\beta \cdot n / \log n)$ additional queries. In other words, in order to show that $\A_n\xrightarrow{\OO(n)}\emptyset$, the bound on the reduction $\A_1\brightarrow{}\emptyset$ can be relaxed from $\OO(1)$ to $\OO(\log n)$.

Finally, we note that the query game framework presented in this article is reminiscent of a communication theory problem introduced by Chang and Weldon \cite{CW79}, in which multiple independent users want to communicate by sending bits over a channel that adds up their answers. The schemes derived in this paper may be of independent interest within this line of investigation. In particular, the strategy derived in Remark \ref{remark:inductionstrategy} can be directly reinterpreted as a coding scheme in this setting where the $i$:th user transfers information at rate $\Theta(1/i)$.

Combinatorics (math.CO); Information Theory (cs.IT)