Canadian Informatics Workshop: 2025 Day 2, Problem 2
Election time is coming up in Kitchener! After having retired from her mayoral duties, this year Alanna is serving as the Chief Electoral Officer for the city. Things have been going well, but there is still one major issue—the polling stations have no software!
Thus, she's had to quickly assemble a team of programmers to program the polling stations before election day. You've been selected to be one of the programmers on the team, and have been tasked with writing a part of the polling software. Your goal is to write a program to multiply 2 numbers.
However, don't be fooled by the simplicity of this task! The polling stations are ancient, and so are only programmable in an archaic, obscure language called APL (Advanced Polling Language). APL only supports binary variables, along with the 4 binary operations AND, OR, NOT, and XOR (which can be applied on said variables). Are you up for the challenge?
Formally, you are given the constraints and
, and your goal is to
output an APL program that inputs an
-bit binary number
and an
-bit binary number
, and outputs their product
as an
-bit binary number. Moreover, as the polling station computers
are very slow, your program may also only contain up to
instructions
in total.
For the full output specification, please see the Output Specification section below.
Note on grading: The APL program that your code outputs will be graded
on multiple test cases. Each test case in turn will simulate its
execution on multiple pairs of inputs. If the APL program does
not compute the correct result on any input pair, it will be considered
incorrect for that test case. Unfortunately, the electoral committee
does not have the resources to test your program on a real polling
station.
Input Specification
The first and only line of input contains integers,
,
, and
.
The following table shows how the available 25 marks are distributed:
Marks Awarded | Bounds on |
Bounds on |
Bounds on |
---|---|---|---|
2 marks | |||
4 marks | |||
4 marks | |||
9 marks | |||
3 marks | |||
3 marks |
Output Specification
Output a valid APL program. The language specification for APL is as follows:
On the first line, output an integer (
), the number
of instructions in the program.
The next lines should each contain one gate. Each gate should be a
line in one of the following 4 forms:
AND <in1> <in2>
OR <in1> <in2>
XOR <in1> <in2>
NOT <in1>
The parameters and are variable identifiers that correspond to the input of each gate. APL supports only binary variables, each of which are identified by an integer.
Moreover, note that the instructions in your program will be executed in a sequential order. This means that the parameters and must use existing variables declared as the output in previous instructions. How variables are defined/interpreted is described below.
The first variables (variables
to
) correspond to the bits
of the first input number
from the least to most significant bit,
and the next
variables (variables
to
) correspond to the
bits of the second input number
in the same order.
Next, the output of every gate will each create a new variable starting
from the variable (e.g. the first gate outputs to the variable
, the second gate outputs to
, etc.).
Finally, the last variables (which are the outputs of the last
gates) will be interpreted as the binary representation of the
product
(where
) in order from least significant bit
to most significant bit. Consequently, if your program has less than
gates, then it will be judged as incorrect.
Sample Input
1 1 2000000
Output for Sample Input
5
OR 0 1
NOT 2
AND 3 3
NOT 4
XOR 4 4
Explanation for Sample Output
The outputted APL program computes the bitwise OR of the input numbers
and
and stores it in the output number
. Note that this
program incorrectly computes
and is for demonstration
purposes only.
Additionally, note that since and
are both
-bit numbers, the
output number
is expected to be
bits long, including any
leading zeroes.
Comments
Similar problem: https://www.acmicpc.net/problem/33445