#P4106. [HEOI2014] 逻辑翻译

[HEOI2014] 逻辑翻译

Description

In the human nervous system, each signal can be represented by 1−1 or +1+1. These signals combine to eventually form complex information such as joy, anger, sorrow, and happiness; sour, sweet, bitter, and spicy; red, yellow, green, and blue. Breakthroughs in nano-probing technology have enabled biologists to measure the complete logical function of specific regions of the brain. However, processing ultra-large data has always been a headache for Professor H.

Suppose a logical unit accepts NN signal inputs and produces a real value rr representing some meaning. Then there are 2N2^N possible cases in total. Through long-term cumulative measurements, Professor H can accurately obtain the mapping between input signals and rr:

f:{1,1}NRf :\{-1,1\}^N \to \mathbb{R}

Further research has found that the neuron’s computation can be modeled as a well-known polynomial. Since the square of an input signal value is always 11, we can uniquely represent any logic ff by a polynomial without powers consisting of 2N2^N terms.

For example x1=+1; x2=+1 -> 0

x1=+1; x2=-1 -> 1

x1=-1; x2=+1 -> 2

x1=-1; x2=-1 -> 3

can be written as

f(x1,x2)=1.50.5x2x1f(x_1,x_2)=1.5-0.5x_2-x_1

Studying the polynomial form of a logical unit is very meaningful for understanding how the brain works, so Xiao M decided to help Professor H convert all measured logic tables into polynomial form. Such a simple task should be no problem for the coding ace Xiao M, right?

Input Format

The first line is NN (1N201\le N \le20). Then there are 2N2^N lines. Each line contains one set of logical inputs and one corresponding value, representing the signs of x1...xNx_1...x_N and the corresponding rr. See the sample. The testdata guarantees that the absolute value of every logical value does not exceed 100100, and it contains no more than 22 decimal places. All logical input strings are guaranteed to be pairwise distinct.

Output Format

Up to 2N2^N lines, representing the coefficient of each term in the polynomial. If a coefficient is an integer, output it in integer form; otherwise, output it as an irreducible fraction. If a coefficient is exactly 00, omit the entire line. Separate variables and the coefficient with a space; the constant term should not have a space.

Order by the polynomial’s lexicographic order:

  1. The constant term comes first.

  2. Among non-constant terms, the term with the smaller smallest xx index comes first.

  3. If two terms share the same smallest index, remove that smallest-indexed xx and compare recursively using the same rule.

Example: 11, x1x1, x1x2x1x2, x1x2x3x1x2x3, x1x3x1x3, x2x2, x2x3x2x3, x3x3. See the sample.

2 
++ 0 
+- 1 
-+ 2 
-- 3 
 

3/2 
-1 x1 
-1/2 x2 
3 
--- -1.0
-++ -1.0
+-+ -1.0
++- -1.0
--+ 1.0
-+- 1.0
+-- 1.0
+++ 1.0

1 x1x2x3

Hint

This problem has 1010 testdata.

For the 11st testdata, N3N\le3; for the 22nd testdata, N8N\le8; for the 33rd and 44th testdata, N11N\le11; for the 55th and 66th testdata, the answer polynomial has only one nonzero term, as in Sample #2; for the 77th and 88th testdata, the answer polynomial has at most 55 nonzero terms.

For 100%100\% of the testdata, 1N20,r100,100rZ1 \le N \le 20, |r| \le 100, 100r\in \mathbb{Z}.

Friendly reminder

Please pay attention to input and output efficiency.

Translated by ChatGPT 5