QOJ.ac

QOJ

Time Limit: 10 s Memory Limit: 1024 MB Total points: 40

#12457. Double or NOTing

Statistics

给定一个起始非负整数 $S$ 和一个结束非负整数 $E$。$S$ 和 $E$ 均以二进制表示(即以 2 为基数书写)。你的目标是将 $S$ 转换为 $E$。你可以使用以下两种操作:

  • 将当前值翻倍。
  • 对当前值进行按位取反(NOT)。当前值的二进制表示不包含不必要的先导零,操作产生的任何不必要的先导零都会被丢弃。(唯一必要的先导零是 $0$ 的表示中的那个零)。

例如,使用翻倍操作,$6$ 变为 $12$,$0$ 变为 $0$,$10$ 变为 $20$。 使用取反操作,$0$ 变为 $1$,$1$ 变为 $0$,$3 = 11_2$ 变为 $0$,$14 = 1110_2$ 变为 $1$,$10 = 1010_2$ 变为 $5 = 101_2$,且 $5 = 101_2$ 变为 $2 = 10_2$。($X_2$ 表示二进制表示为 $X$ 的整数)。

你可以按任意顺序多次使用这些操作。例如,你可以通过先使用取反操作,然后使用两次翻倍操作,再使用一次取反操作,将 $S = 10001_2$ 转换为 $E = 111_2$:

$$10001_2 \xrightarrow{\text{NOT}} 1110_2 \xrightarrow{\times 2} 11100_2 \xrightarrow{\times 2} 111000_2 \xrightarrow{\text{NOT}} 111_2$$

确定完成转换所需的最少操作次数,或者说明无法完成转换。

输入格式

输入的第一行包含测试用例数量 $T$。接下来是 $T$ 个测试用例。每个测试用例由一行组成,包含两个字符串 $S$ 和 $E$,分别是起始整数和结束整数的二进制表示。

输出格式

对于每个测试用例,输出一行 Case #x: y,其中 $x$ 是测试用例编号(从 1 开始),如果无法通过这两种操作将 $S$ 转换为 $E$,则 $y$ 为 IMPOSSIBLE。否则,$y$ 是将 $S$ 转换为 $E$ 所需的最少操作次数。

数据范围

$1 \le T \le 100$。 $S$ 的每个字符均为 $0$ 或 $1$。 仅当 $S$ 的长度为 $1$ 时,$S$ 的第一位可以是 $0$。 $E$ 的每个字符均为 $0$ 或 $1$。 仅当 $E$ 的长度为 $1$ 时,$E$ 的第一位可以是 $0$。

子任务 1 $1 \le \text{length of } S \le 8$。 $1 \le \text{length of } E \le 8$。

子任务 2 $1 \le \text{length of } S \le 100$。 $1 \le \text{length of } E \le 100$。

样例

样例输入 1

6
10001 111
1011 111
1010 1011
0 1
0 101
1101011 1101011

样例输出 1

Case #1: 4
Case #2: 3
Case #3: 2
Case #4: 1
Case #5: IMPOSSIBLE
Case #6: 0

说明

样例 #1 是题目描述中展示的例子。

以下是样例 #2、#3 和 #4 的可能最优解法:

$$1011_2 \xrightarrow{\text{NOT}} 100_2 \xrightarrow{\times 2} 1000_2 \xrightarrow{\text{NOT}} 111_2$$

$$1010_2 \xrightarrow{\times 2} 10100_2 \xrightarrow{\text{NOT}} 1011_2$$

$$0_2 \xrightarrow{\text{NOT}} 1_2$$

在样例 #5 中,无法通过任何操作序列从 $0_2$ 得到 $101_2$。

在样例 #6 中,由于 $S = E$,我们不需要执行任何操作。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.