If , heat from the left once with power .
If , heat once from each side with power . The -th piece from the left gains from the first operation and from the second operation, so every piece finishes at a cooking level of .
Thus, if , print an operation count of and the operation . Otherwise, print an operation count of and the operations and . Both the time and space complexities for one test case are .
Proof
With no operations, every piece has a cooking level of . Since the final level must be positive, at least one operation is always necessary.
If , a single operation with power brings the only piece to a cooking level of . Therefore, the minimum number of operations is .
Now suppose . After just one operation, the first and second pieces from the chosen side have cooking levels and , respectively. Since , these levels differ. Therefore, one operation cannot make all cooking levels equal, and at least two operations are necessary.
In subtask 1, always holds, so print the single operation . Both the time and space complexities are .
In subtask 2, exhaustive search is also possible. For each operation, try both directions and every power from to . First check all candidates consisting of one operation. If none works, check all candidates consisting of two operations. Calculate the final cooking level of each piece one at a time and check that it equals the positive level of the first piece. The optimal construction above always uses powers at most and at most two operations, so this search range contains an optimal answer. The time complexity is , and the space complexity is .
Heating from the left with power increases the level of piece by . The same piece is the -th piece from the right, so heating from the right with the same power increases its level by . After these two operations, its cooking level is .
This level is independent of the position and is positive because . The heating power is also within the allowed range. The construction satisfies the conditions in two operations, and at least two are necessary, so it is optimal.
For the full constraints, use the direct construction described above.
Solution written by GPT6