Intruder Outsmarting

시간 제한5초메모리 제한1024 MB

요약
각 바퀴의 시작 값이 주어질 때, 수열을 회문으로 만들기 위해 필요한 최소 +D/-D 이동 횟수를 구하거나 불가능을 판정한다.
난이도

보통10점 중 6점

유형
수학, 그리디, 정수론, 구현
정답자
아직 제출이 없습니다

문제

Amiria is a cautious internet user, and as such, she is setting up two-factor authentication for her accounts. She is using a special type of security key as an extra precaution to outsmart any intruders that may want to take it. Amiria's security key requires a code to activate. To enter the code, one must place it on wheels with numbers, similar to code padlocks.

Amiria's security key has a sequence of W\mathbf{W} wheels. Each wheel has the numbers 11 through N\mathbf{N} printed in order. By one wheel rotation, the user can move the currently shown integer either to the next or the previous integer. Numbers on the wheel wrap around. This means the number after N\mathbf{N} is 11 and the number before 11 is N\mathbf{N}.

There is no hidden password. To activate Amiria's security key, a person needs to move the wheels such that the sequence of numbers shown is palindromic. That is, the sequence of numbers is the same when read from left to right and from right to left. To slow down intruders, Amiria rigged the security key such that the wheels only rotate in increments of D\mathbf{D}. That is, on a move, a wheel that is currently showing xx can be made to show x−Dx - \mathbf{D} or x+Dx + \mathbf{D}, applying the proper wraparound. That is, if x−D<1x - \mathbf{D} \lt 1 the actual number shown after the operation is x−D+Nx - \mathbf{D} + \mathbf{N}, and if x+D>Nx + \mathbf{D} \gt \mathbf{N} the actual number shown is x+D−Nx + \mathbf{D} - \mathbf{N}.

Amiria wants to check how much this system would slow down an intruder trying to use her security key. Given the number of wheels and the number currently shown on each wheel, find the minimum number of operations needed to make the sequence of shown numbers palindromic, or report that it is impossible to do so.

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case consists of two lines. The first line of a test case contains 33 integers W\mathbf{W}, N\mathbf{N}, and D\mathbf{D}: the number of wheels in Amiria's security key, the number of integers shown in each of those wheels, and the fixed increment that Amiria rigged for every wheel. The second line of a test case contains W\mathbf{W} integers X_1,X_2,…,X_W\mathbf{X\_1}, \mathbf{X\_2}, \dots, \mathbf{X\_W}, where X_i\mathbf{X\_i} is the number currently shown in the ii-th wheel from left to right.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum number of operations needed to make the sequence of shown numbers palindromic. If there is no way to make the sequence of shown numbers palindromic through the allowed operations, yy must be IMPOSSIBLE instead.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 1≤D≤N−11 \le \mathbf{D} \le \mathbf{N} - 1.
  • 1≤X_i≤N1 \le \mathbf{X\_i} \le \mathbf{N}, for all ii.

힌트

In Sample Case #1, the sequence can be made 5,4,5,4,55\\, 4\\, 5\\, 4\\, 5, which is palindromic, with 33 operations by using one addition operation on the first and fourth wheels, and one subtraction operation on the fifth wheel. There is no way to make the sequence palindromic with fewer moves.

In Sample Case #2 the sequence is already palindromic, so we do not need any operations.

In Sample Case #3, both numbers would need to be equal for the sequence to be palindromic. Since wheel values can only move by 22 and both current numbers have different parity, that cannot be done.

예제1

  1. 예제 1

    입력
    3
    5 5 4
    1 4 5 5 4
    3 4 2
    3 4 3
    2 4 2
    1 4
    
    예상 출력
    Case #1: 3
    Case #2: 0
    Case #3: IMPOSSIBLE