Majority

시간 제한2초메모리 제한2048 MB

요약
주어진 n개의 불리언 입력에 대해 다수결을 출력하는, 깊이가 제한된 AND와 OR 게이트 회로를 구성한다.
난이도

어려움10점 중 9점

유형
분할 정복, 그리디, 조합론, 구현
정답자
아직 제출이 없습니다

문제

Little Cat learned Boolean circuits recently. Now he wants to construct a majority circuit.

A circuit over Boolean variables x_1,…,x_nx\_1, \ldots, x\_n is a directed acyclic graph where each node (logical gate) is either an input node labeled by a variable x_ix\_i, or an operation node labeled by a logical operation ∨\vee or ∧\wedge. There are exactly nn input nodes, one for each of the input variables x_1,…,x_nx\_1, \ldots, x\_n. 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 ∨\vee (respectively, ∧\wedge) 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 00. The in-degree of an operation node is at least 11, and can be arbitrarily large. The out-degrees are arbitrary (possibly 00).

For convenience, there are two special constant nodes TT (true) and FF (false), which always output 11 and 00, respectively.

The majority circuit Maj_n\mathit{Maj}\_n has nn inputs x_1,…,x_nx\_1, \ldots, x\_n, and it outputs 11 if at least half of inputs are 11, and outputs 00 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 nn inputs with depth at most 1414?

입력

The input contains one line with an integer nn (2≤n≤642 \le n \le 64) indicating the number of input nodes.

출력

The first line must contain an integer mm (1≤m≤5⋅1041 \le m \le 5 \cdot 10^4) representing the number of nodes labeled by ∨\vee or ∧\wedge, so there are n+m+2n + m + 2 nodes in the circuit in total. The input nodes x_1,…,x_nx\_1, \ldots, x\_n are numbered by 1,…,n1, \ldots, n. The constant true node TT is numbered by −1-1, and the constant false node FF is numbered by −2-2.

A total of mm lines must follow. The ii-th line must describe node (n+i)(n + i) in one of the following formats.

  • "OR k_ik\_i a_1a\_1 a_2a\_2 …\ldots a_k_ia\_{k\_i}" (without quotes): node (n+i)(n + i) computes the logical OR of nodes a_ja\_j where −2≤a_j<n+i-2 \le a\_j < n + i and a_j≠0a\_j \neq 0 for all 1≤j≤k_i1 \le j \le k\_i.
  • "AND k_ik\_i a_1a\_1 a_2a\_2 …\ldots a_k_ia\_{k\_i}" (without quotes): node (n+i)(n + i) computes the logical AND of nodes a_ja\_j where −2≤a_j<n+i-2 \le a\_j < n + i and a_j≠0a\_j \neq 0 for all 1≤j≤k_i1 \le j \le k\_i.

It is fine if a_u=a_va\_u = a\_v for some u≠vu \neq v. You must guarantee that ∑_i=1mk_i≤2⋅105\sum\_{i = 1}^{m} k\_i \le 2 \cdot 10^5 and that the depth of the circuit does not exceed 1414.

The output of the circuit is chosen as the output of node n+mn + m.

To check the circuit you construct, Little Cat will test your circuit for 15001500 rounds. In each round, Little Cat will generate an arbitrary input x_1,…,x_nx\_1, \ldots, x\_n (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 x_1,…,x_nx\_1, \ldots, x\_n correctly. You need to pass all the 15001500 rounds.

힌트

The sample output prints a depth-2 circuit computing Maj_4\mathit{Maj}\_4. The circuit Maj_4(x_1,x_2,x_3,x_4)\mathit{Maj}\_4 (x\_1,x\_2,x\_3,x\_4) outputs 11 if and only if at least two input nodes are 11. 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 11, 22, 33, and 44 are input nodes.
  • Nodes 55, 66, 77, 88, 99, and 1010 compute the logical AND of some input nodes.
  • Nodes 1111 and 1212 are redundant.
  • Node 1313 is the output node.
  • The constant nodes TT and FF 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 (1≤m≤5⋅1041 \le m \le 5 \cdot 10^4, ∑k_i≤2⋅105\sum k\_i \le 2 \cdot 10^5, depth≤14\mathit{depth} \le 14) are satisfied.

During the test, the following shows a possible scenario:

The input nodes are set to x_1=1x\_1 = 1, x_2=0x\_2 = 0, x_3=0x\_3 = 0, x_4=1x\_4 = 1.

Therefore, the outputs of nodes x_5,…,x_13x\_5, \ldots, x\_{13} are:

  • x_5=x_1 AND x_2=1 AND 0=0x\_5 = x\_1 \text{ AND } x\_2 = 1 \text{ AND } 0 = 0
  • x_6=x_1 AND x_3=1 AND 0=0x\_6 = x\_1 \text{ AND } x\_3 = 1 \text{ AND } 0 = 0
  • x_7=x_1 AND x_4=1 AND 1=1x\_7 = x\_1 \text{ AND } x\_4 = 1 \text{ AND } 1 = 1
  • x_8=x_2 AND x_3=0 AND 0=0x\_8 = x\_2 \text{ AND } x\_3 = 0 \text{ AND } 0 = 0
  • x_9=x_2 AND x_4=0 AND 1=0x\_9 = x\_2 \text{ AND } x\_4 = 0 \text{ AND } 1 = 0
  • x_10=x_3 AND x_4=0 AND 1=0x\_{10} = x\_3 \text{ AND } x\_4 = 0 \text{ AND } 1 = 0
  • x_11=x_5 AND x_5 AND x_6=0 AND 0 AND 0=0x\_{11} = x\_5 \text{ AND } x\_5 \text{ AND } x\_6 = 0 \text{ AND } 0 \text{ AND } 0 = 0
  • x_12=x_7=1x\_{12} = x\_7 = 1
  • x_13=x_5 OR x_6 OR x_7 OR x_8 OR x_9 OR x_10=0 OR 0 OR 1 OR 0 OR 0 OR 0=1x\_{13} = x\_5 \text{ OR } x\_6 \text{ OR } x\_7 \text{ OR } x\_8 \text{ OR } x\_9 \text{ OR } x\_{10} = 0 \text{ OR } 0 \text{ OR } 1 \text{ OR } 0 \text{ OR } 0 \text{ OR } 0 = 1.

The output of the circuit is x_13=1x\_{13} = 1, which is the majority of 1,0,0,1\\{1, 0, 0, 1\\}.

예제1

  1. 예제 1

    입력
    4
    
    예상 출력
    9
    AND 2 1 2
    AND 2 1 3
    AND 2 1 4
    AND 2 2 3
    AND 2 2 4
    AND 2 3 4
    AND 3 5 5 6
    AND 1 7
    OR 6 5 6 7 8 9 10