#P6678. [COCI 2019/2020 #2] Popcount
[COCI 2019/2020 #2] Popcount
Description
The program requirements are as follows:
- The input of the program is a single non-negative integer strictly less than .
- The output of the program is a single non-negative integer strictly less than .
- When programming in MalnarScript, you may only use one -bit unsigned integer variable . At the beginning of the program, this variable stores the input, and the value of it at the end of the program is regarded as the output of the program.
- The MalnarScript source code may contain at most commands of the form , executed in order, and each command may contain at most characters.
The recursive definition of the symbol is as follows:
$$\texttt{In other words, the symbol can be the variable , or a value that matches the definition of , or a binary expression in parentheses, where each operand also matches the same definition of .
In the definition above, the symbol denotes a non-negative decimal integer strictly less than . The symbol can be , , , , , or , which represent addition, subtraction, bitwise OR, bitwise AND, left shift, and right shift, respectively.
In addition, the character may appear at most times in .
If overflow or underflow occurs during addition or subtraction, MalnarScript performs these operations modulo . For example, when , in MalnarScript, , and .
For each command, the expression on the right-hand side is evaluated to a number, and then stored into . To compute the right-hand-side expression, MalnarScript first replaces every occurrence of with its current value. Then it evaluates the expression like in any mathematical expression, i.e. parentheses first. Note that operator precedence (the order among operators) is irrelevant, because the final result is fully determined by the placement of parentheses.
Your task is to write a program in MalnarScript that computes the popcount of the binary representation of the input value.
Input Format
The first line contains two positive integers , with the meanings described in Description.
Output Format
In the first line, you should output the number of commands in the produced MalnarScript program.
In the remaining lines, you should output the commands of the required program. Each command must be output on its own line and must satisfy the MalnarScript syntax described in the task statement.
It is important that there are no unnecessary blank lines or extra space characters in the output. Each line (including the last line) must end with a newline (\n).
2 2
1
A=(A-((A&2)>>1))
3 5
2
A=((A&4)|((A&3)-((A&2)>>1)))
A=((A&3)+((A&4)>>2))
Hint
Constraints and Notes
| Subtask | Score | Constraints and Notes |
|---|---|---|
| , | ||
| , | ||
| , | ||
| , |
Notes
The scoring of this problem follows the original COCI problem, with full score .
Translated from COCI2019-2020 CONTEST #2 T4 Popcount.
Thanks to
In addition, because the testlib library has an issue when generating random numbers in the Luogu environment for , fixed pre-generated random numbers are used instead. For some test points with large , the Special Judge program runs for a longer time, as follows.
| Special Test Point ID | Time Limit |
|---|---|
| 15 | 3s |
| 39 | 2s |
| 40 | 3s |
| 48 |
The time limits above are only for the Special Judge program.
Translated by ChatGPT 5
京公网安备 11011102002149号