A Bookworm Climbs up the Polynomial Hierarchy: Meta-Restoration Complexity in Arithmetic Puzzles
Abstract
In arithmetic puzzles, a partially specified arithmetic expression must be completed to make the computation valid. Arithmetical restoration puzzles require filling in missing digits, while cryptarithms involve assigning digits to letters. The Japanese term mushikui-zan (“bookwormed arithmetic”) commonly refers to arithmetical restorations, where we imagine the missing digits have been eaten by a bookworm. Puzzle creator Yousuke Ikeda proposed a new type of puzzle in which a previously designed bookwormed arithmetic with multiplication – known to have a unique solution – has itself been “bookwormed”, that is, partially erased. The goal is to restore the specified blanks so that the resulting bookwormed puzzle again has a unique solution. We further generalize this framework: for each , we define level- puzzles as those in which type- blanks must be filled to make the resulting level- puzzle uniquely solvable. We study the level- versions of the Boolean satisfiability problem, and show that they form a hierarchy of -complete decision problems, tightly matching the levels of the polynomial hierarchy. As applications, we show that the level- arithmetical restoration problem with multiplication is -complete, as is the level- cryptarithm problem. On the positive side, we show that level-2 arithmetical restoration puzzles with addition are solvable in polynomial time.
Keywords and phrases:
arithmetical restoration, cryptarithms, polynomial hierarchy, uniqueness quantifier, puzzle complexityCopyright and License:
2012 ACM Subject Classification:
Theory of computation Problems, reductions and completeness ; Theory of computation Complexity classesEditor:
John IaconoSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
The relationship between puzzles and computational complexity has long attracted attention from both recreational and theoretical perspectives. Since Martin Gardner introduced Conway’s Game of Life to the public, various puzzles have been explored through the lens of computational hardness [7]. Among them, arithmetic puzzles offer a natural interface between everyday logic and formal algorithmic reasoning.
Two prominent types of arithmetic puzzles are cryptarithms and arithmetical restorations. A cryptarithm is a puzzle in which each digit is uniquely replaced by a letter, as in the classic example “SEND + MORE = MONEY” shown in Figure 1. These puzzles have been studied under various names such as alphametics and have been featured in mathematical recreations for over a century [3].
An arithmetical restoration, on the other hand, requires filling in missing digits in partially erased expressions, as in “The Solitary Seven” shown in Figure 2 [8]. These puzzles, also known as figure logic or missing figure puzzles, emphasize reconstructing a valid equation rather than deciphering encoded symbols.
The computational complexity of these puzzles has been studied. In 1987, Eppstein [2] proved that solving cryptarithms with addition is NP-complete. De Bondt [1] showed that they are in fact ASP-complete [9], meaning there is a parsimonious reduction from any NP search problem such that the bijection between solutions to the instances can be computed in polynomial time. Finally, Matsui [6] showed that arithmetical restorations with multiplication are also ASP-complete via a reduction from Positive 1-in-3SAT, which was shown to be ASP-complete in [4].
In 2024, Japanese puzzle creator Yousuke Ikeda proposed a novel extension of the arithmetical restoration puzzle. He was inspired by the Japanese term mushikui-zan (literally “bookwormed arithmetic”), commonly used to describe arithmetical restorations in Japan; we imagine that a bookworm ate some of the digits in an arithmetic expression, and we are trying to figure out what they were. Ikeda’s extension shown in Figure 3 is a simple but powerful idea: take an instance of bookwormed arithmetic that is already uniquely solvable, and then bookworm it again – that is, erase part of the puzzle itself once more. The new challenge is to determine how to restore these higher-level bookwormed blanks so that the resulting puzzle, still with the original bookwormed blanks, is again uniquely solvable.
This idea can be generalized naturally to a recursive hierarchy of puzzles (Figure 4). Suppose the erased puzzle was itself a uniquely solvable instance of a prior restoration: this yields a meta-level restoration problem. At level 1, the task is to fill in missing digits in a partially erased arithmetic puzzle, i.e., to solve a standard bookwormed arithmetic. At level 2, some parts of a level-1 puzzle have been bookwormed again, and the goal is to assign digits to these higher-level blanks so that the resulting level-1 puzzle becomes uniquely solvable. This nesting continues: at level , the solver must assign digits to type- blanks so that the resulting type- puzzle admits a unique solution, and so on, all the way down to the base level. Only blanks of the highest type are solved at each level; lower-type blanks remain as part of the sub-instance being reconstructed.
Our main results classify the computational complexity of meta-level puzzles across various settings.
-
1.
We define a notion of “locally parsimonious reductions” between search problems, and prove that these yield reductions between the meta-level versions of these problems.
-
2.
In the Boolean satisfiability setting, we show that the level- SAT problem is complete for the class . The level- SAT problem is to decide the truth of quantified Boolean formulae of the form
where each denotes a vector of Boolean variables, and denotes the “exists and is unique” quantifier.
-
3.
In the arithmetical restoration setting, we show the following partial classification according to which arithmetic operation is involved:
Theorem 1 (Meta-level restorations with multiplication/division).
Let . The level- meta-restoration problem with multiplication or division is -complete.
Theorem 2 (Meta-level restorations with addition/subtraction).
The level-2 meta-restoration problem with addition or subtraction is solvable in polynomial time.
As part of showing the latter result, we give a polynomial-time algorithm to determine whether an arithmetical restoration puzzle has a unique solution.
-
4.
For cryptarithms, we show computational hardness with any of the four arithmetic operations:
Theorem 3 (Meta-level cryptarithms).
Let . The level- meta-cryptarithm problem with addition, subtraction, multiplication, or division is -complete.
Along the way to showing these results, we give parsimonious reductions from multiplication to division in arithmetical restorations, and from addition to subtraction, multiplication, or division in cryptarithms. This extends Matsui’s [6] and de Bondt’s [1] results to show ASP-completeness of the corresponding puzzle types.
2 Meta-Level Problems
2.1 Definitions
We define a framework for meta-level problems as follows.
Let be a domain, and let be a set of variables. An assignment is a function assigning each variable in a value in . We can think of a subset as a solution set to a search problem over the variables , with each variable taking values in .
The level-1 problem corresponding to is just the ordinary decision problem “Is nonempty?”. The level- problem for is specified by assigning each variable in an integer between 1 and called its type. The question is whether it is possible to assign values to the type- variables such that the resulting level- problem has a unique solution.
To analyze the computational complexity of such problems, we will consider problems specified by binary strings.
Definition 4.
A search problem over a domain is a function that assigns to each string a solution set , where the set of variables is .111We make the set of variables explicit in this definition, but the point is that the number of variables is polynomially related to the size of the instance.
Note that the domain will commonly be infinite: for example, in arithmetic restoration the domain is since each variable represents a digit in the base specified by the problem instance.
Definition 5.
Let be a search problem and be an integer. The level- problem is the language of strings such that the formula
is satisfied, where encodes a function from to , and is the list of variables of type .
2.2 Reductions between Meta-Level Versions of Constraint Satisfaction Problems
Now we define a type of reductions between search problems which will turn out to lift to reductions between the level- versions of those problems.
Definition 6.
Let and be solution sets over the domains and respectively. We say that locally reproduces if there is a bijection with the following properties:
First, for each variable there must exist a corresponding set of variables , and an injection where is the set of all possible values taken by the variable as part of a solution . The sets are required to be disjoint from each other.
Second, the bijection must satisfy the equation for every assignment and each pair of variables .
The definition states that the values of the variables in depend only on the value of in . Another phrasing is that the function is the product of the functions .
More generally, let , and write . Define the sets by and ; these are the sets of feasible partial assignments to (respectively, ) which can be extended to an assignment in (respectively, ). We write for the product function .
Lemma 7.
The function is in fact a bijection .
Proof.
We prove that always outputs an element of , that it is injective, and that it is surjective on .
First, consider an arbitrary element of ; it can be written as for some . Then is equal to , and the latter is in because . So always outputs an element of .
Second, is injective because it is the product of the injective functions .
Third, consider an arbitrary element of ; it can be written as for some . Then , so is surjective onto .
The purpose of Definition 6 is to prove the following:
Lemma 8.
Let and be solution sets, and be a function assigning types to the variables in . Suppose locally reproduces , so there exist as in Definition 6.
Assign types to the variables in according to the function
Now fix , and let and be the sets of variables in and (respectively) of type strictly greater than .
Suppose is a feasible partial assignment to . Then the equivalences
and
both hold, where is the list of variables in of type , is the list of variables in of type , and denotes combination of assignments to disjoint sets of variables.
Proof.
The proof is by induction on . We write and for the sets of type- variables in and respectively. Note that by definition of we have except when .
Let , and fix a feasible partial assignment to . We exhibit a bijection between the sets and . The bijection is given by , whose inverse is . So these sets have the same size, which means the formulae in the lemma statement are equivalent.
Now let , and fix a feasible partial assignment to . Suppose is a partial assignment to such that is feasible. Then by the inductive hypothesis, the formula
| (1) |
is equivalent to
Since , it follows using Lemma 7 that is a bijection between partial assignments satisfying the formula (1) and partial assignments to satisfying
In particular, the number of such is equal to the number of such , which proves the lemma statement.
Using this lemma, we can define our desired reductions between search problems.
Definition 9.
Let and be search problems. A locally parsimonious reduction from to is a function such that locally reproduces for each , and the sets can be computed in polynomial time.
Many existing parsimonious reductions are in fact locally parsimonious. For instance, this is true for the reduction from SAT to 3SAT given in [9], since every SAT variable is given a single corresponding 3SAT variable. The same holds for the reduction from SAT to 1-in-3SAT.
Theorem 10.
Let and be search problems over the domains and respectively, and be a locally parsimonious reduction from to . Then there is a polynomial-time reduction from the level- problem to the level- problem.
Proof.
Let be an instance of the level- problem. We define the output of the reduction to be where is defined as in Lemma 8. It follows immediately from Lemma 8, that is a YES instance if and only if is.
Combined with the results of the next section, Theorem 10 can be used to show -hardness for the level- version of a problem by finding a locally parsimonious reduction from SAT (or 3SAT, or 1-in-3SAT) to that problem. Similarly, containment in can be shown by finding a locally parsimonious reduction in the other direction.
3 -Completeness of Level- SAT
In this section, we prove that level- SAT is -complete.
Theorem 11 (Level- SAT is -complete).
For every , the satisfiability problem for formulae of the form , where each denotes a vector of Boolean variables, is -complete.
Proof of Theorem 11.
We claim that any formula of the form , where are vectors of Boolean variables and is free, can be transformed in polynomial time into two equivalent Boolean formulae with blocks of alternating quantifiers: one starting with and the other starting with . We refer to these two desired forms as the “ form” and the “ form” respectively.
The proof is by induction on . The key observation is that each uniqueness quantifier can be rewritten using standard quantifiers in two different ways:
| ( rewriting) | |||
| ( rewriting) |
Let . By the inductive hypothesis, we can rewrite in both form and form. To write in form, we apply the rewriting above. We then replace the positive occurrence of with its form and the negative occurrence of with its form. After applying De Morgan’s laws and the equivalences
to move all quantifiers to the beginning, the result is a formula in form. Finding the form is analogous, but we use the rewriting and make the opposite choices for . These rewriting steps take polynomial time,222The time taken and the size of the output formula are both exponential in the constant , but polynomial in the size of the input formula. so the claim is proved. It follows from this that satisfiability of formulae of the form is contained in , simply by rewriting in form.
It remains to show -hardness. Consider any formula of the form where are vectors of Boolean variables and is free. An induction on shows that this formula is equivalent to
where each is a single Boolean variable.
Proof by induction:
Since satisfiability of formulae of the form is -complete, it follows from the above that satisfiability of formulae of the form is -complete also, by rewriting the rest of the formula after the first quantifier.
4 Arithmetic Puzzles: Definitions
4.1 Arithmetical Restorations
In order to formalize arithmetical restorations, we focus here on addition, which is the primary case where tractability arises. Other operations (subtraction, multiplication, and division) are treated analogously but lead to different complexity classifications, as discussed later.
An instance consists of two -digit addends and their sum in base , possibly of length or . To streamline notation, we include two boundary columns: and with , resulting in columns in total: (see Figure 6). Each internal column () consists of a triple , where and are the digits to be added, and is the corresponding digit of the sum.
Let be the variables in . For an assignment , let denote the resulting table after substitution. An assignment is a solution if satisfies the addition rule with proper carry propagation. Evaluation proceeds from the least significant digit (rightmost column) to the most significant digit, propagating carries according to:
4.2 Cryptarithms
Cryptarithms are puzzles in which digits are replaced by letters. The goal is to assign distinct digits to letters so that the resulting arithmetic expression is valid. We focus on multiplication; other operations follow analogous definitions. An instance consists of:
-
1.
a multiplicand of length ,
-
2.
a multiplier of length ,
-
3.
partial products of length with , where , and
-
4.
a final product of length .
Let be the set of letters used in . A digit assignment induces a concrete puzzle via substitution. An assignment is a solution if:
-
1.
all arithmetic operations in are correct in base , and
-
2.
all digits assigned are pairwise distinct.
5 Arithmetic Puzzles: Hardness
5.1 Parsimonious Reductions Across Arithmetic Operations
We now show that arithmetical restoration and cryptarithm puzzles involving subtraction, multiplication, and division are all ASP-complete.
Lemma 12 (ASP-completeness across operations).
Arithmetical restorations with multiplication or division are ASP-complete. Cryptarithms with subtraction, multiplication, or division are also ASP-complete. Thus the corresponding counting problems are #P-complete.
To prove Lemma 12, we systematically construct parsimonious reductions between puzzle types and operations. These reductions preserve the number of solutions, allowing us to transfer the known hardness of addition and multiplication to subtraction and division. We divide the proof into four parts, each showing a parsimonious reduction between two operations.
Reduction from Multiplication to Division in Restorations.
Matsui [6] showed that the arithmetical restoration problem with multiplication is ASP-complete via a reduction from Monotone 1-in-3SAT. The instance consists of digit sequences , , , and arranged to represent a multiplication table.
Figure 8 illustrates the structure of .
To reduce to division, we construct a new instance that reuses these sequences in a division layout. We add auxiliary rows to account for intermediate subtractions. Each subtraction step in the division corresponds to a row in the multiplication layout, preserving the structure of the solution.
Fixing the divisor and quotient uniquely determines all subtraction steps, so the number of solutions is preserved. Thus, the reduction is parsimonious.
Reduction from Addition to Subtraction in Cryptarithms.
Let be the cryptarithm with addition derived from a Positive 1-in-3SAT instance. It consists of three rows:.
We construct by rearranging the rows as and changing the operator to subtraction. This transformation preserves the solution space, due to the symmetry between carry and borrow behavior in digit gadgets.
Reduction from Addition to Multiplication in Cryptarithms.
We embed the structure of into a multiplication layout to form . Fixed digits and are introduced to maintain constant semantics. The region corresponding to is preserved within the multiplicative table layout.
This transformation embeds the original constraint structure without ambiguity, ensuring a one-to-one correspondence of solutions.
Reduction from Subtraction to Division in Cryptarithms.
Finally we reduce cryptarithm subtraction to division by embedding the instance into a division layout. We reuse the row configuration to form a long division table , fixing and as constants.
The core structure is preserved inside a designated region, and all auxiliary digits are placed disjointly to avoid interference. The transformation is again parsimonious.
This completes the proof of Lemma 12.
5.2 Meta-Level Complexity
We now complete the classification of meta-level puzzles by proving Theorem 1 and Theorem 3, which concern the complexity of multiplicative meta-restorations and meta-cryptarithms, respectively.
We accomplish this by applying the results of Section 2.2 to the preceding parsimonious reductions, allowing us to obtain reductions between the meta-level versions of the problems. Indeed, from the structure of the reductions it is clear that every variable of the input instance corresponds to a single variable of the output instance, so the reductions are in fact locally parsimonious. The same is true for the reductions of [4, 6, 1] used to show ASP-completeness of arithmetical restorations with multiplication and cryptarithms with addition. By Theorems 10 and 11, these reductions show that the preceding problems are -hard. We can also represent arithmetic restoration and cryptarithms with SAT formulae in a locally parsimonious way, showing containment in .
Therefore, the meta-restoration and meta-cryptarithm problems are -complete for all .
6 Arithmetic Puzzles: Algorithms
6.1 Polynomial-Time Algorithms for Arithmetical Restorations with Addition and Subtraction
We prove the following result, which provides a polynomial-time decision algorithm for restorations with addition and subtraction:
Lemma 13 (Additive restorations).
Determining whether a given arithmetical restoration puzzle with addition and subtraction admits a unique solution is in .
The basic idea is a dynamic programming approach that focuses on the carry-over values between columns. We first define the corresponding subproblem.
Definition 14 (Extended column feasibility).
Given a column and two carry values , we say that is feasible with carries if there exists an assignment to the variables in that is consistent with incoming carry and outgoing carry .
Lemma 15.
An arithmetical restoration has a solution if and only if there exists a sequence such that, for each , the column is feasible with carries .
Proof.
- ()
-
Suppose has a solution . Let be the carry sequence induced by , where is the carry entering column and the carry leaving it. For each , the restriction of to makes the column feasible with . Hence the condition holds.
- ()
-
Conversely, suppose there exists such that each column admits an assignment consistent with . Since the concern disjoint sets of variables, their union forms a global assignment. By construction, every column constraint is satisfied with the designated carries , so is a solution of .
We now develop a column-wise feasibility check for given carry-in and carry-out values, which forms the basis of our algorithm.
Lemma 16.
For a column and carry values , we can determine in time whether is feasible with carries .
Proof.
Fix a column . For each entry in row , define
From the carry definition, we require: . The left-hand side is a linear expression in at most three variables ranging over , and the right-hand side is constant. The number of variable entries is at most three, and we can compute the range of the left-hand side in constant time by bounding it between , where if is a variable and otherwise. A feasible assignment exists if and only if the right-hand side lies within this range.
Using this feasibility check, we now describe a polynomial-time algorithm that verifies the existence of a consistent carry sequence across all columns. We construct a directed graph representing feasible carry transitions:
-
1.
For each and each , define a vertex .
-
2.
Add an edge from to if column admits a feasible assignment with carry-in and carry-out , as determined by Lemma 16.
The total number of vertices is and the number of edges is at most , since each transition layer has at most 4 feasible carry transitions. Each feasibility check runs in time, and the overall graph construction takes time. Finally, we perform a reachability test from and to or . This can be done in time. Thus, the entire algorithm runs in time, proving Lemma 13.
6.2 Polynomial-Time Algorithm for Level-2 Additive Restorations
We now establish Theorem 2 by presenting a polynomial-time algorithm for solving level-2 meta-restorations involving addition or subtraction.
The core idea is to extend the level-1 restoration algorithm, which checks the consistency of a partially erased addition table, to also verify uniqueness – a requirement at level 2. To this end, we construct a directed graph that captures all valid and unique carry-over transitions across columns.
Specifically, let each vertex represent the carry value at column index (from right to left). We add an edge if there exists an assignment to the type-2 entries of column such that there exists a unique way to fill in the type-1 entries of column such that:
-
1.
the incoming carry is and the outgoing carry is ,
-
2.
the assignment respects the roles of the cells (type-0: fixed, type-1: blank at level 1, type-2: blank at level 2), and
-
3.
the assignment is consistent with addition modulo .
We can then decide solvability by checking whether this graph contains a valid path from the leftmost vertex (typically ) to the rightmost vertex (typically , where is the number of columns).
The following lemma underpins the tractability of the edge construction step:
Lemma 17.
Fix a column with given carry-in and carry-out . We can determine in time whether there exists an assignment to the type-2 variables in (if any) such that the values of all type-1 variables in are uniquely determined and the column is consistent.
Proof.
Fix a column with carry-in and carry-out .
Let denote the three entries of column . Each is interpreted as follows: if it is of type 0, then is the fixed digit given in the puzzle; if it is of type 1 or type 2, then denotes the corresponding variable to be assigned. For each row , define:
From the semantics of additive restoration with carries, we require:
Let , which is fully determined by type-0 entries and carry values.
We distinguish cases according to the presence/absence of type-1 and type-2 entries in the column:
-
(1)
All of are not type-2. In this case, it suffices to check whether the type-1 variables admit a unique assignment, which can be done by a straightforward extension of Lemma 16.
-
(2)
All of are not type-1. In this case, there are no type-1 variables, so uniqueness holds vacuously. We only need to check whether there exists an assignment to the type-2 variables that makes the column consistent. This can be done by treating all type-2 variables as type-1 variables and applying Lemma 16.
-
(3)
Exactly one of is a type-1 variable. In this case, equation (6.2) becomes The right-hand side can only take a restricted range of values: if is the type-1 variable, then it ranges over ; if or is the type-1 variable, then it ranges over . Thus the feasibility reduces to checking whether one can assign values to at most two type-2 variables among so that the left-hand side falls into the corresponding range. This can be decided in polynomial time by the same idea as in Lemma 16.
-
(4)
Exactly two of are type-1 variables. In this case, equation (6.2) becomes There are two subcases:
-
(i)
and both are type-1. Then the equation reduces to . The pair is uniquely determined only if the right-hand side is or , yielding or .
-
(ii)
(without loss of generality) and are type-1. Then the equation becomes . The pair is uniquely determined only if the right-hand side is or , which forces or .
Thus the decision reduces to checking whether the unique type-2 variable among can be assigned so that the left-hand side matches one of these special values. This check can be performed in polynomial time by the same idea as in Lemma 16.
-
(i)
By applying Lemma 17 to each candidate edge, we can construct the graph in polynomial time. Reachability in this graph determines whether a globally consistent and unique assignment exists, completing the algorithm for level-2 restorations.
7 Open Questions
We conclude with some open questions.
-
1.
In this paper we explored the complexity of formulae involving nested quantifiers. What can be said about other similar types of quantifiers? For instance, we could consider quantifiers such as “there exist exactly two satisfying ” and “exactly half of all satisfy ”.
-
2.
Are there natural examples of parsimonious reductions that are not locally parsimonious?
-
3.
Regarding the hardness of meta-level additive restorations: While we have shown that level-2 additive restorations admit a polynomial-time algorithm, the computational complexity of level- additive or subtractive restorations for remains open. Do such problems climb higher in the polynomial hierarchy, or can they still be solved efficiently?
References
- [1] Michael de Bondt. On the ASP-completeness of cryptarithms. Technical Report 0419, Department of Mathematics, Radboud University, Nijmegen, Netherlands, November 2004. URL: https://hdl.handle.net/2066/60283.
- [2] David Eppstein. On the NP-completeness of cryptarithms. ACM SIGACT News, 18(3):38–40, 1987. doi:10.1145/24658.24662.
- [3] Kozaburo Fujimura. Puzzles and Problems (in Japanese). Diamond Sha Co., Ltd., 1969.
- [4] Harry B. Hunt, Madhav V. Marathe, Venkatesh Radhakrishnan, and Richard E. Stearns. The complexity of planar counting problems. SIAM Journal on Computing, 27(4):1142–1167, 1998. doi:10.1137/S0097539793304601.
- [5] Yousuke Ikeda. X post on July 28, 2024. https://x.com/ikeikey/status/1817448938322202776. Accessed: 2024-09-21.
- [6] Tomomi Matsui. NP-completeness of arithmetical restorations. Journal of Information Processing, 21(3):402–404, July 2013. doi:10.2197/IPSJJIP.21.402.
- [7] Ryuhei Uehara. Computational complexity of puzzles and related topics. Interdisciplinary Information Sciences, 29(2):1–23, 2023. doi:10.4036/iis.2022.R.06.
- [8] Ryuhei Uehara. Algorithms for Puzzles (in Japanese). Nippon Hyoron Sha Co., Ltd., 2024.
- [9] Takayuki Yato and Takahiro Seta. Complexity and completeness of finding another solution and its application to puzzles. IEICE Transactions on Fundamentals of Electronics, Communications, and Computer Sciences, E86-A(5):1052–1060, 2003. Also IPSJ SIG Notes 2002-AL-87-2, 2002. URL: http://ci.nii.ac.jp/naid/110003221178/en/.
