Stone of Logic
Time limit1sMemory limit256 MB
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:
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 . The meaning is as follows:
Constraints
- An extra bit must be initialized, that is, assigned a value, before it is used.
- , , and 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.