Majority
시간 제한2초메모리 제한2048 MB
주어진 n개의 불리언 입력에 대해 다수결을 출력하는, 깊이가 제한된 AND와 OR 게이트 회로를 구성한다.
문제
Little Cat learned Boolean circuits recently. Now he wants to construct a majority circuit.
A circuit over Boolean variables is a directed acyclic graph where each node (logical gate) is either an input node labeled by a variable , or an operation node labeled by a logical operation or . There are exactly input nodes, one for each of the input variables . Additionally, a single node is chosen as the output of the circuit.
Each node computes an output: the input nodes (labeled by a variable) output exactly the variable written on them, and nodes labeled by (respectively, ) output the logical OR (respectively, AND) of all incoming nodes. Note that logical NOT nodes are forbidden. See the example and notes for better understanding.
The in-degree of an input node is . The in-degree of an operation node is at least , and can be arbitrarily large. The out-degrees are arbitrary (possibly ).
For convenience, there are two special constant nodes (true) and (false), which always output and , respectively.
The majority circuit has inputs , and it outputs if at least half of inputs are , and outputs otherwise. Formally, \mathit{Maj}\_n(x\_1, \ldots, x\_n) = \left\[2 \sum\_{i = 1}^n x\_i \ge n\right].
Define the depth of a circuit as the length of the longest (directed) path in the circuit, that is, the number of edges of the longest path.
Could you help Little Cat to construct a majority circuit over inputs with depth at most ?
입력
The input contains one line with an integer () indicating the number of input nodes.
출력
The first line must contain an integer () representing the number of nodes labeled by or , so there are nodes in the circuit in total. The input nodes are numbered by . The constant true node is numbered by , and the constant false node is numbered by .
A total of lines must follow. The -th line must describe node in one of the following formats.
- "
OR" (without quotes): node computes the logical OR of nodes where and for all . - "
AND" (without quotes): node computes the logical AND of nodes where and for all .
It is fine if for some . You must guarantee that and that the depth of the circuit does not exceed .
The output of the circuit is chosen as the output of node .
To check the circuit you construct, Little Cat will test your circuit for rounds. In each round, Little Cat will generate an arbitrary input (he won't say how exactly) and test your circuit with that input. You pass this round if your circuit outputs the majority of the input correctly. You need to pass all the rounds.
힌트

The sample output prints a depth-2 circuit computing . The circuit outputs if and only if at least two input nodes are . Thus you can compute the logical AND of every pair of input nodes and output the logical OR of these ANDs.
Here are some notes on the circuit nodes:
- Nodes , , , and are input nodes.
- Nodes , , , , , and compute the logical AND of some input nodes.
- Nodes and are redundant.
- Node is the output node.
- The constant nodes and are not drawn in the figure.
Here, the existence of redundant nodes will not affect the validity of the circuit as long as the constraints (, , ) are satisfied.
During the test, the following shows a possible scenario:
The input nodes are set to , , , .
Therefore, the outputs of nodes are:
- .
The output of the circuit is , which is the majority of .