#P10402. Achieving the Target with Limited Operations
Achieving the Target with Limited Operations
Achieving the Target with Limited Operations
You are given an integer sequence \(a_1, a_2, \ldots, a_n\) of length \(n\) and a counter \(x\) which is initially \(0\). You are also given an integer \(k\) representing the maximum number of operations you can perform on \(x\), and a target value \(c\). Your goal is to transform \(x\) so that its final value \(x_{final}\) satisfies \(|x_{final} - c| < 10^{-4}\) while following the rules below.
There are five types of operations available. Each operation counts toward the limit of \(k\) operations. Moreover, each element \(a_i\) must be used exactly once in one of the following three operations on \(x\>:
add i
: Increase \(x\) by \(a_i\). After this, \(a_i\) cannot be used again.sub i
: Decrease \(x\) by \(a_i\). After this, \(a_i\) cannot be used again.mul i
: Multiply \(x\) by \(a_i\). After this, \(a_i\) cannot be used again.
In addition, you may perform the following extra operations (each counted in the total \(k\) operations):
sqrt i
: Replace \(a_i\) by \(\sqrt{a_i}\). You may apply the square root operation to each \(a_i\) at most once.pow f
: Replace \(x\) by \(x^f\), where \(f\) is a floating-point number.
At all times during the process, the values of \(x\) and every \(a_i\) must remain within \(10^{10}\) (i.e. not exceed \(10^{10}\) in absolute value). It is guaranteed that there is a solution. If multiple solutions exist, you may output any one.
Hint on a simple solution:
- If \(c = 0\), you can simply perform the
mul
operation on every \(a_i\) because \(0 \times a_i = 0\) for all \(i\). - If \(c \neq 0\), one strategy is to perform the
add
operation on every \(a_i\), so that \(x = a_1 + a_2 + \cdots + a_n = S\). If \(S = c\) (within precision), you are done. Otherwise, you can perform one extrapow f
operation such that
[ f = \frac{\ln(c)}{\ln(S)}\quad\text{, so that}\quad x^f = S^f = c. ]
This simple scheme requires either exactly \(n\) or \(n+1\) operations, which is within the limit \(k\) guaranteed by the input.
inputFormat
The first line of input contains three numbers: an integer \(n\) (the length of the sequence), an integer \(k\) (the maximum permitted operations), and a floating-point number \(c\) (the target value).
The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) representing the sequence. It is guaranteed that a solution exists.
outputFormat
First, output an integer \(m\) representing the number of operations performed (\(m \le k\)). Then output \(m\) lines, each describing an operation. Operations involving a sequence element use 1-based indexing. The allowed operations are exactly as described in the problem statement.
Your final sequence of operations must ensure that the final value of \(x\) satisfies \(|x - c| < 10^{-4}\).
sample
3 4 10
2 3 5
3
add 1
add 2
add 3
</p>