Popcount
의견: 0
\(M\)iniature \(Al\)gebraic \(Na\)tural \(R\)elay (also called MALNAR) is the latest technological advancement in the
flourishing realm of small programmable devices. You can write your own programs for this device using
MalnarScript, an esoteric programming language with the following set of features:
• Input to the program is a single n\(on-ne\)gative integer strictly less than \(2^{N}\).
• Output of the program is a single n\(on-ne\)gative integer strictly less than \(2^{N}\).
• When programming in MalnarScript you can only use one \(N-bi\)t unsigned integer variable \(A\). At
the beginning of the program, this variable holds the input and its value at the end of the program
is considered to be the program’s output.
• The source code of MalnarScript must consist of at most \(K\) commands of the form A=<ex\(pr> wh\)ich
are executed in order and each of them must consist of at most a thousand characters. The
symb\(ol
to the sa\(me
while the symb\(ol
of addition, subtraction, bitwise or, bitwise and, left shift and right shift.
Also the character A can appear at most 5 times in t\(he
In the case of overflow or underflow when performing the operations of addition and subtraction, Malnar-
Script will perform those operations modulo \(2^{N}\). For example, when \(N = 3\) the expression (\(7 + 3\)) will
evaluate to 2 and the expression (\(2 - 5\)) will evaluate to 5 in MalnarScript.
The right side of the equation in each command evaluates into a single number which will then be stored
into \(A\). In order to evaluate the right hand side expression, MalnarScript first replaces each occurrence of
\(A\) with its current value. The calculation of expression then proceeds as it would in any mathematical
expression, i.e., the parentheses take precedence. Note that the priorities of operators (in term of operation
order) are irrelevant because the final result is completely defined by placement of parentheses.
Your task is to write a program which outputs a program in MalnarScript which calculates the number of
ones in a binary representation of the input value.
Subtask
Score
Constraints
1
15
\(2 \le N \le 100\), \(K = N - 1\)
2
15
\(N = 500\), \(K = 128\)
3
35
\(1 \le N \le 40\), \(K = 7\)
4
45
\(100 \le N \le 500\), \(K = 10\)
The first line contains two integers \(N\) and \(K\) from the task description.
In the first line you should output the number of commands of the produced MalnarScript program.
In the remaining lines you should output the commands of the sought program. Each command must
be printed in a separate line and must satisfy the syntax of MalnarScript as described in the task
description.
It is important that there are no unnecessary empty lines or extra whitespace characters in the output.
Each line (including the last) must be terminated by the e\(nd-of-li\)ne character (’\n’).
2 21
A=(A-((A&2)>>1))3 52
A=((A&4)|((A&3)-((A&2)>>1)))
A=((A&3)+((A&4)>>2))Clarification of the first example:
input = 0 ⇒output = (0 −((0&2) >> 1)) = (0 −(0 >> 1)) = (0 −0) = 0
input = 1 ⇒output = (1 −((1&2) >> 1)) = (1 −(0 >> 1)) = (1 −0) = 1
input = 2 ⇒output = (2 −((2&2) >> 1)) = (2 −(2 >> 1)) = (2 −1) = 1
input = 3 ⇒output = (3 −((3&2) >> 1)) = (3 −(2 >> 1)) = (3 −1) = 2
평가 및 의견
Popcount
Log in to rate problems.
아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.
풀이 제출
Popcount