아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Double or NOTing

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

요약
두 배 연산과 비트 NOT 연산만으로 이진 문자열 S를 E로 바꾸는 최소 연산 횟수를 구하고, 불가능하면 IMPOSSIBLE을 출력한다.
난이도

보통10점 중 6점

유형
비트 연산, BFS, 수학
정답자
아직 제출이 없습니다

문제

You are given a starting non-negative integer SS and an ending non-negative integer EE. Both SS and EE are given by their binary representation (that is, they are given written in base 22). Your goal is to transform SS into EE. 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 00).

For example, by using the double operation, 66 becomes 1212, 00 becomes 00, and 1010 becomes 2020. By using the NOT operation, 00 becomes 11, 11 becomes 00, 3=11_23=11\_2 becomes 00, 14=1110_214=1110\_2 becomes 11, 10=1010_210=1010\_2 becomes 5=101_25=101\_2, and 5=101_25=101\_2 becomes 2=10_22=10\_2. (X_2X\_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=10001_2S=10001\_2 to E=111_2E=111\_2 using the NOT operation first, then using the double operation twice, and then another NOT operation:

10001_2⟹NOT1110_2⟹×211100_2⟹×2111000_2⟹NOT111_2.10001\_2 \overset{\text{NOT}}{\Longrightarrow} 1110\_2 \overset{\times 2}{\Longrightarrow} 11100\_2 \overset{\times 2}{\Longrightarrow} 111000\_2 \overset{\text{NOT}}{\Longrightarrow}111\_2\text{.}

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, TT. TT test cases follow. Each consists of a single line containing two strings SS and EE, 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 SS into EE using the two operations. Otherwise, yy is the smallest number of operations needed to transform SS into EE.

제한

  • 1≤T≤1001 \le T \le 100.
  • Each character of SS is either 0 or 1.
  • The first digit of SS can be 0 only if the length of SS is 11.
  • Each character of EE is either 0 or 1.
  • The first digit of EE can be 0 only if the length of EE is 11.

힌트

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⟹NOT100_2⟹×21000_2⟹NOT111_2,1011\_2 \overset{\text{NOT}}{\Longrightarrow} 100\_2 \overset{\times 2}{\Longrightarrow} 1000\_2 \overset{\text{NOT}}{\Longrightarrow}111\_2\text{,}

1010_2⟹×210100_2⟹NOT1011_2, and1010\_2 \overset{\times 2}{\Longrightarrow} 10100\_2 \overset{\text{NOT}}{\Longrightarrow} 1011\_2\text{, and}

0_2⟹NOT1_2.0\_2 \overset{\text{NOT}}{\Longrightarrow} 1\_2\text{.}

In Sample Case #5, it is not possible to get from 0_20\_2 to 101_2101\_2 with any sequence of operations.

In Sample Case #6, we do not need to perform any operations because S=ES=E.

예제1

  1. 예제 1

    입력
    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