#P8523. [IOI 2021] 位移寄存器
[IOI 2021] 位移寄存器
Background
Abusing the judging system for this problem will result in an account ban.
Due to technical limitations, please do not submit this problem using C++ 14 (GCC 9).
This is an interactive problem. You only need to implement the function required in the code.
Your code does not need to include any extra header files, and you do not need to implement the main function.
At the beginning of your code, you need to add the following code:
#include<vector>
void append_move(int t, int y);
void append_store(int t, std::vector<bool> v);
void append_and(int t, int x, int y);
void append_or(int t, int x, int y);
void append_xor(int t, int x, int y);
void append_not(int t, int x);
void append_left(int t, int x, int p);
void append_right(int t, int x, int p);
void append_add(int t, int x, int y);
void append_print(int t);
Problem Description
Engineer Christopher is developing a new computer processor.
This processor can access different -bit storage units (in this problem, and ). They are called registers, indexed from to . We denote these registers as . Each register is an array of bits, indexed from (the rightmost bit) to (the leftmost bit). For all () and (), we denote the -th bit of register as .
For any bit sequence (of some length ), its integer value is $2^0 \cdot d_0 + 2^1 \cdot d_1 + \cdots + 2^{l - 1} \cdot d_{l - 1}$. We say the integer stored in a register is the integer value of the bit sequence in the register, i.e., it is $2^0 \cdot r[i][0] + 2^1 \cdot r[i][1] + \cdots + 2^{b - 1} \cdot r[i][b - 1]$.
The processor has types of instructions that can be used to modify the bits in registers. Each instruction operates on one or more registers and stores its output into one of the registers. Below, we use to denote an operation that changes the value of to become . The operations of each instruction type are described as follows.
: Copy the bit array in register to register . For all (), set .
: Set register to , where is a -bit array. For all (), set .
: Compute the bitwise AND of registers and , and store the result in register . For all (), set if both and are , otherwise set .
: Compute the bitwise OR of registers and , and store the result in register . For all (), set if at least one of and is , otherwise set .
: Compute the bitwise XOR of registers and , and store the result in register . For all (), set if exactly one of and is , otherwise set .
: Compute the bitwise NOT of register , and store the result in register . For all (), set .
: Shift all bits in register left by positions, and store the result in register . The result of shifting register left by is a -bit array . For all (), if then , otherwise . For all (), set .
: Shift all bits in register right by positions, and store the result in register . The result of shifting register right by is a -bit array . For all (), if then , otherwise . For all (), set .
: Add the integer values in registers and , and store the result in register . The addition is done modulo . More formally, let be the integer value in register before the operation, and be the integer value in register before the operation. Let be the integer value in register after the operation. If , set the bits in so that . Otherwise, set the bits in so that .
Christopher wants you to solve two tasks using this new processor. The task type is given by an integer . For all task types, you need to create a program that is a sequence of the instructions defined above.
The program input consists of integers , and each integer has bits, i.e., (). Before the program is executed, all input numbers are stored in register in order, so that for all (), the integer value of the -bit sequence $r[0][i \cdot k], r[0][i \cdot k + 1], \ldots , r[0][(i + 1) \cdot k - 1]$ equals . Note that . All other bits in register (with indices between and , inclusive), as well as all bits in all other registers, are initialized to .
Executing a program means executing its instructions in order. After the last instruction is executed, the program output is computed from the final values of the bits in register . Specifically, the output is a sequence of integers , where for each (), is the integer value of the sequence of bits in register from to . Note that after the program finishes, the remaining bits in register (with indices at least ), as well as all bits in other registers, may have arbitrary values.
The first task () is to find the minimum among the input integers . Specifically, must be the minimum of . The values of can be arbitrary.
The second task () is to sort the input integers in non-decreasing order. Specifically, for all (), should equal the -th smallest integer among (that is, is the smallest input integer).
Please help Christopher write programs to solve these tasks. Each program may contain at most instructions.
Input Format
You need to implement the following function:
void construct_instructions(int s, int n, int k, int q)
- : the task type.
- : the number of integers in the input.
- : the number of bits in each input integer.
- : the maximum allowed number of instructions.
This function will be called exactly once, and it should create a sequence of instructions for the required task.
This function should call one or more of the following functions to create the instruction sequence:
void append_move(int t, int y)
void append_store(int t, std::vector<bool> v)
void append_and(int t, int x, int y)
void append_or(int t, int x, int y)
void append_xor(int t, int x, int y)
void append_not(int t, int x)
void append_left(int t, int x, int p)
void append_right(int t, int x, int p)
void append_add(int t, int x, int y)
- Each function appends one instruction , , , , , , , , or , respectively.
- For all relevant instructions, , , and must be at least and at most .
- For all relevant instructions, , , and do not have to be pairwise distinct.
- For the
leftandrightinstructions, must be at least and at most . - For the
storeinstruction, the length of must be .
You may also call the following function to help test your solution:
void append_print(int t)
- When judging your solution, all calls to this function will be ignored.
- In the sample evaluator, this function appends a operation to the program.
- When the sample evaluator executes a program and encounters a operation, it prints the -bit integers formed by the first bits of register .
- must satisfy .
- Any call to this function will not be counted in the number of instructions you create.
After appending the last instruction, construct_instructions should return. Then, the program you created will be judged on a number of test cases, where each test case provides input data consisting of -bit integers . If the output produced by your program for the given input data satisfies the following conditions, your solution will be considered accepted for that test case:
- If , should be the minimum of .
- If , for all (), should be the -th smallest integer among .
When judging your solution, one of the following error messages may be given:
Invalid index: the register index given as , , or in some function call is invalid (possibly negative).Value to store is not b bits long: the length of provided toappend_storeis not equal to .Invalid shift value: the value of provided toappend_leftorappend_rightis not between and (inclusive).Too many instructions: your function attempts to append more than instructions.
0 2 1 1000
0 0
0 1
1 0
1 1
-1
move 1 0
right 1 1 1
and 0 0 1
0
0
0
1
1 2 1 1000
0 0
0 1
1 0
1 1
-1
move 1 0
right 1 1 1
and 2 0 1
or 3 0 1
left 3 3 1
or 0 2 3
0 0
0 1
0 1
1 1
Hint
For all testdata:
- .
- .
- .
- .
- .
- .
- (for all ).
| Subtasks | Score | Special constraints |
|---|---|---|
| , , , . | ||
| , , , . | ||
| , . | ||
| , . | ||
| , , . | ||
| , . |
Thanks to
Translated by ChatGPT 5