#P13034. [GCJ 2021 #1C] Double or NOTing

    ID: 12823 Type: RemoteJudge 10000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>图论2021KMP 算法Google Code Jam

[GCJ 2021 #1C] Double or NOTing

题目描述

You are given a starting non-negative integer S\textbf{S} and an ending non-negative integer E\textbf{E}. Both S\textbf{S} and E\textbf{E} are given by their binary representation (that is, they are given written in base 2). Your goal is to transform S\textbf{S} into E\textbf{E}. The following two operations are available to you:

  • Double your current value.
  • Take the bitwise NOT of your current value. The binary representation of your current value is taken without unnecessary leading zeroes, and any unnecessary leading zeroes produced by the operation are dropped. (The only necessary leading zero is the one in the representation of 0).

For example, by using the double operation, 6 becomes 12, 0 becomes 0, and 10 becomes 20. By using the NOT operation, 0 becomes 1, 1 becomes 0, 3=1123 = 11_2 becomes 0, 14=1110214 = 1110_2 becomes 1, 10=1010210 = 1010_2 becomes 5=10125 = 101_2, and 5=10125 = 101_2 becomes 2=1022 = 10_2. (X2X_2 means the integer whose binary representation is XX).

You can use these operations as many times as you want in any order. For example, you can transform S=100012\textbf{S} = 10001_2 to E=1112\textbf{E} = 111_2 using the NOT operation first, then using the double operation twice, and then another NOT operation:

$$10001_2 \xrightarrow{\text{NOT}} 1110_2 \xrightarrow{\times2} 11100_2 \xrightarrow{\times2} 111000_2 \xrightarrow{\text{NOT}} 111_2. $$

Determine the smallest number of operations needed to complete the transformation, or say it is impossible to do so.

输入格式

The first line of the input gives the number of test cases, T\textbf{T}. T\textbf{T} test cases follow. Each consists of a single line containing two strings S\textbf{S} and E\textbf{E}, the binary representations of the starting and ending integers, respectively.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is IMPOSSIBLE if there is no way to transform S\textbf{S} into E\textbf{E} using the two operations. Otherwise, yy is the smallest number of operations needed to transform S\textbf{S} into E\textbf{E}.

6
10001 111
1011 111
1010 1011
0 1
0 101
1101011 1101011
Case #1: 4
Case #2: 3
Case #3: 2
Case #4: 1
Case #5: IMPOSSIBLE
Case #6: 0

提示

Sample Explanation

Sample Case #1 is the example shown in the main part of the statement.

These are possible optimal ways of solving Sample Cases #2, #3, and #4, respectively:

$$1011_2 \xrightarrow{\text{NOT}} 100_2 \xrightarrow{\times2} 1000_2 \xrightarrow{\text{NOT}} 111_2, $$$$1010_2 \xrightarrow{\times2} 10100_2 \xrightarrow{\text{NOT}} 1011_2, $$

and

02NOT12.0_2 \xrightarrow{\text{NOT}} 1_2.

In Sample Case #5, it is not possible to get from 020_2 to 1012101_2 with any sequence of operations.

In Sample Case #6, we do not need to perform any operations because S=E\textbf{S} = \textbf{E}.

Limits

  • 1T1001 \leq \textbf{T} \leq 100.
  • Each character of S\textbf{S} is either 0 or 1.
  • The first digit of S\textbf{S} can be 0 only if the length of S\textbf{S} is 1.
  • Each character of E\textbf{E} is either 0 or 1.
  • The first digit of E\textbf{E} can be 0 only if the length of E\textbf{E} is 1.

Test Set 1 (14 Pts, Visible Verdict)

  • 1the length of S81 \leq \text{the length of } \textbf{S} \leq 8.
  • 1the length of E81 \leq \text{the length of } \textbf{E} \leq 8.

Test Set 2 (26 Pts, Hidden Verdict)

  • 1the length of S1001 \leq \text{the length of } \textbf{S} \leq 100.
  • 1the length of E1001 \leq \text{the length of } \textbf{E} \leq 100.