Double or NOTing
시간 제한10초메모리 제한1024 MB
두 배 연산과 비트 NOT 연산만으로 이진 문자열 S를 E로 바꾸는 최소 연산 횟수를 구하고, 불가능하면 IMPOSSIBLE을 출력한다.
문제
You are given a starting non-negative integer and an ending non-negative integer . Both and are given by their binary representation (that is, they are given written in base ). Your goal is to transform into . 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 ).
For example, by using the double operation, becomes , becomes , and becomes . By using the NOT operation, becomes , becomes , becomes , becomes , becomes , and becomes . ( means the integer whose binary representation is ).
You can use these operations as many times as you want in any order. For example, you can transform to using the NOT operation first, then using the double operation twice, and then another NOT operation:
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, . test cases follow. Each consists of a single line containing two strings and , the binary representations of the starting and ending integers, respectively.
출력
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is IMPOSSIBLE if there is no way to transform into using the two operations. Otherwise, is the smallest number of operations needed to transform into .
제한
- .
- Each character of is either
0or1. - The first digit of can be
0only if the length of is . - Each character of is either
0or1. - The first digit of can be
0only if the length of is .
힌트
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:
In Sample Case #5, it is not possible to get from to with any sequence of operations.
In Sample Case #6, we do not need to perform any operations because .