Under construction

Still being built. Things may change

Stone of Logic

Time limit1sMemory limit256 MB

Summary
Build a circuit of AND gates, using inverted inputs and as few extra bits as possible, that sorts 16 input bits into ascending order.
Level

Hard8 of 10

Topics
Bit manipulation, Sorting, Implementation
Solved
No attempts yet

Problem

The Logic Stone, known as the last stone of the Polymath civilization, has not yet had its purpose clearly established. Most scholars estimate that the stone fragments it produces were used to build basic logic circuits.

The signal used in the stone fragments is called a 'spin'. A spin is always in one of two states: an up-spin, which represents 1, or a down-spin, which represents 0. A stone fragment has two input devices and takes the form of an AND circuit, which returns 1 only when both inputs are 1.

Of course, this structure alone cannot build a logic circuit. However, records show that people found a remarkable solution. If a signal is fed into the input flipped upside down, it is inverted, and this made it possible to build logic circuits.

To study how logic circuits are built, you decided to construct a logic circuit for a basic situation yourself. The problem you must solve is to sort N bits in ascending order. That is, after the program finishes, every bit with value 1 must be to the right of every bit with value 0.

Initially, the information of each bit is stored in bits 1 through N. You may use AND gates for this. See the Output section for how to use them. To solve the problem, you may also use K extra bits, numbered N+1 through N+K. However, using an extra bit costs five times as much as any other operation, so keep the use of extra bits to a minimum. The score depends on K, the number of extra bits used, Q, the number of AND gate uses, and Q_1, the number of operations that use extra bits, as follows:

100×min⁡(1,375(Q−Q1)+5Q1)×1max⁡(k,1)100 \times \min\left(1, \frac{375}{(Q-Q_1)+5Q_1}\right) \times \frac{1}{\max(k,1)}

Input

This problem has no input.

Output

The first line prints Q, the number of AND gate uses. Starting from the next line, print Q lines, each in the form aa bb cc. The meaning is as follows:

SignMeaning
(+)The AND of bit bb and bit cc is assigned to bit ∥a∥\|a\|.
(-)The AND of the inverted bit ∥b∥\|b\| and the inverted bit ∥c∥\|c\| is inverted, and the result is assigned to bit ∥a∥\|a\|.

Constraints

  • N=16N=16
  • K≤10K \le 10
  • Q≤1000Q \le 1000
  • 1≤∣a∣,∣b∣,∣c∣≤N+K1 \le |a|, |b|, |c| \le N+K
  • An extra bit must be initialized, that is, assigned a value, before it is used.
  • ∣a∣|a|, ∣b∣|b|, and ∣c∣|c| may be equal to each other.

Hint

The grader attempts to sort every possible binary sequence using the program you submit. The score above is awarded only if every binary sequence is sorted successfully. Otherwise, no score is given.

Examples1

  1. Example 1

    Input
    Expected output
    3
    -3 -1 -2
    4 1 2
    1 3 -4