Unlock the Padlock
시간 제한30초메모리 제한1024 MB
크기 D인 N개의 다이얼을 모두 0으로 만들기 위해 필요한 중첩 범위 회전의 최소 횟수를 구한다.
문제
Imagine you have a padlock, which is a combination lock consisting of dials, set initially to a random combination. The dials of the padlock are of size , which means that they can have values between and , inclusive, and can be rotated upwards or downwards. They are also ordered from left to right, with the leftmost and rightmost dials at positions and , respectively. The padlock can be unlocked by setting the values of all its dials to .
You can perform zero or more operations of this kind:
- Pick any range such that and rotate all the dials in together, upwards or downwards. Rotating up increases the value of each dial in the range by , and rotating down decreases its value by . Note that a dial with value becomes when increased (rotated up) and a dial with value becomes when decreased (rotated down).
The series of operations must satisfy the following condition:
- The range chosen in the -th operation needs to be completely contained within the range chosen in the -th operation; that is, . The initial range () can be chosen arbitrarily.
Example of a valid sequence of operations to unlock a padlock with initial combination :
- Rotate range downwards.
- Rotate range downwards.
- Rotate range downwards.
The following are some operations that cannot be performed:
- Rotating range after , because is not completely contained in (does not satisfy where and ).
- Rotating range after .
The goal for you is to output the minimum number of valid operations needed to make all dials in the padlock set to .
입력
The first line of the input contains the number of test cases, . test cases follow.
Each test case consists of two lines.
The first line of each test case contains two integers and , representing the number of dials in the padlock and the size of the dials, respectively.
The second line of each test case contains integers , where the -th integer represents the value of the -th dial in the initial combination of the padlock.
출력
For each test case, output one line containing Case #x: y, where is the test case number (starting from ) and is the minimum number of operations needed to unlock the padlock as described in the statement.
제한
- .
- , for all .