Proof Generator#1401

Proof Generator

1 second
64 megabytes
Very hard

A logical formula has the syntax shown in figure 1(a), where a variable stands for a truth value. The formula (\textbf{+ F_1 \ldots F_n}) stands for the logical disjunction of the formulae \textbf{F_i}, (\textbf{* F_1 \ldots F_n}) denotes the logical conjunction of the formulae \textbf{F_i}, and is the negation of . If a formula has the particular syntax given in figure 1(b), we say that the formula is in ACM Normal Form (ACMNF).

A formula is converted to ACMNF using the rewriting rules given below, where represents a formula, stands for a non-empty sequence of formulae, and and denote possibly empty sequences of formulae. Applying a rewriting rule \textbf{q} \rightarrow \textbf{r} on a formula means to substitute by a part of that matches the pattern , as shown in figure 2. The conversion terminates when no rewriting rule can be applied. The conversion terminates for any formula, and the result is unique regardless of which rules are applied to which parts of the formula and in which order.

A set of axioms is represented as a list (\textbf{V_1} \textbf{V_2} \ldots \textbf{V_n}) of false values. A proof of a formula according to a set of axioms is a term from the ACMNF of such that the term is true according to . For instance, the terms and are the proofs of the formula according to the axioms .

The problem is to write a proof generator that for a given formula , a set of axioms , and numbers k_1, \ldots, k_n, outputs the next proofs of in the order in which they appear in the ACMNF of . If the proofs are exhausted, the generator continues from the first proof of . For example, generating the first proof of the formula according to the axioms yields . Generating three more proofs produces , , and . If the ACMNF of a formula contains similar terms, as in the last example in figure 3, these terms are considered distinct.

Input

Write a proof generator that reads sets of data from the standard input. The content of a data set is \textbf{F}\ \textbf{A}\ \textbf{k_1}\ \ldots\ \textbf{k_n}\ 0, where n > 0, is a formula, is a set of axioms, and \textbf{k_1}, \ldots, \textbf{k_n} are long integers different from . For each \textbf{k_i}, i=1, \ldots, n, the program generates the next |\textbf{k_i}| proofs of and, if \textbf{k_i} > 0, prints these proofs to the standard output.

Output

Each printed proof starts at the beginning of a line, and there are no white spaces between the characters of the proof. The generated proofs are not printed if \textbf{k_i} < 0. White spaces are freely used in the input. A formula has at most characters, and an ACMNF term is at most characters long, not counting white spaces. Input data terminate with an end of file and are correct.

Examples

Example 1

Loading
Loading