#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 2n2^n.
  • The output of the program is a single non-negative integer strictly less than 2n2^n.
  • When programming in MalnarScript, you may only use one nn-bit unsigned integer variable aa. 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 kk commands of the form a = <expr>\texttt{a = <expr>}, executed in order, and each command may contain at most 10310^3 characters.

The recursive definition of the symbol <expr>\texttt{<expr>} is as follows:

$$\texttt{ = A | | ()}$$

In other words, the symbol <expr>\texttt{<expr>} can be the variable aa, or a value that matches the definition of <num>\texttt{<num>}, or a binary expression in parentheses, where each operand also matches the same definition of <expr>\texttt{<expr>}.

In the definition above, the symbol <num>\texttt{<num>} denotes a non-negative decimal integer strictly less than 2n2^n. The symbol <operator>\texttt{<operator>} can be +\texttt{+}, -\texttt{-}, |\texttt{|}, &\texttt{\&}, <<\texttt{<<}, or >>\texttt{>>}, which represent addition, subtraction, bitwise OR, bitwise AND, left shift, and right shift, respectively.

In addition, the character aa may appear at most 55 times in <expr>\texttt{<expr>}.

If overflow or underflow occurs during addition or subtraction, MalnarScript performs these operations modulo 2n2^n. For example, when n=3n = 3, in MalnarScript, 7+3=27 + 3 = 2, and 25=52 - 5 = 5.

For each command, the expression on the right-hand side is evaluated to a number, and then stored into aa. To compute the right-hand-side expression, MalnarScript first replaces every occurrence of aa 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 n,kn, k, 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
11 1515 2n1002 \le n \le 100, k=n  1k = n \ − \ 1
22 n=500n = 500, k=128k = 128
33 3535 1n401 \le n \le 40, k=7k = 7
44 4545 100n500100 \le n \le 500, k=10k = 10

Notes

The scoring of this problem follows the original COCI problem, with full score 110110.

Translated from COCI2019-2020 CONTEST #2 T4 Popcount.

Thanks to

https://www.luogu.com.cn/user/764746
ing the SPJ, which can be downloaded from the attachments of the problem.

In addition, because the testlib library has an issue when generating random numbers in the Luogu environment for N=31N = 31, fixed pre-generated random numbers are used instead. For some test points with large NN, 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