CF1362A.Johnny and Ancient Computer
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Johnny has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can multiply or divide your number by 2, 4 or 8, and division is only allowed if the number is divisible by the chosen divisor.
Formally, if the register contains a positive integer x, in one operation it can be replaced by one of the following:
- x⋅2
- x⋅4
- x⋅8
- x/2, if x is divisible by 2
- x/4, if x is divisible by 4
- x/8, if x is divisible by 8
For example, if x=6, in one operation it can be replaced by 12, 24, 48 or 3. Value 6 isn't divisible by 4 or 8, so there're only four variants of replacement.
Now Johnny wonders how many operations he needs to perform if he puts a in the register and wants to get b at the end.
Johnny 最近发现了一台古老且损坏的计算机。这台机器只有一个寄存器,只能存入一个变量。每次操作中,你可以将该变量的二进制表示向左或向右最多移动三位。若右移会截去某些为 1 的位,则该右移操作是被禁止的。因此,实际上,每次操作可以将当前数乘以 2、4 或 8,或者在满足整除条件的前提下除以 2、4 或 8。
形式化地,若寄存器中当前存放的是正整数 x,则一次操作可将其替换为以下之一:
- x⋅2
- x⋅4
- x⋅8
- x/2,当且仅当 x 能被 2 整除
- x/4,当且仅当 x 能被 4 整除
- x/8,当且仅当 x 能被 8 整除
例如,若 x=6,则一次操作可将其替换为 12、24、48 或 3。由于 6 不能被 4 或 8 整除,故仅有四种可能的替换方式。
现在 Johnny 想知道:若他将 a 放入寄存器,并希望最终得到 b,最少需要执行多少次操作?
输入格式
The input consists of multiple test cases. The first line contains an integer t (1≤t≤1000) — the number of test cases. The following t lines contain a description of test cases.
The first and only line in each test case contains integers a and b (1≤a,b≤1018) — the initial and target value of the variable, respectively.
输入包含多个测试用例。第一行包含一个整数 t(1≤t≤1000),表示测试用例的数量。接下来的 t 行分别描述各个测试用例。
每个测试用例仅有一行,包含两个整数 a 和 b(1≤a,b≤1018),分别表示变量的初始值和目标值。
输出格式
Output t lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get b at the end, then write −1.
输出 t 行,每行应包含一个整数,表示 Johnny 所需执行的最少操作次数。如果 Johnny 无法最终得到 b,则输出 −1。
输入输出样例
输入#1
10 10 5 11 44 17 21 1 1 96 3 2 128 1001 1100611139403776 1000000000000000000 1000000000000000000 7 1 10 8
输出#1
1 1 -1 0 2 2 14 0 -1 -1
说明/提示
In the first test case, Johnny can reach 5 from 10 by using the shift to the right by one (i.e. divide by 2).
In the second test case, Johnny can reach 44 from 11 by using the shift to the left by two (i.e. multiply by 4).
In the third test case, it is impossible for Johnny to reach 21 from 17.
In the fourth test case, initial and target values are equal, so Johnny has to do 0 operations.
In the fifth test case, Johnny can reach 3 from 96 by using two shifts to the right: one by 2, and another by 3 (i.e. divide by 4 and by 8).
在第一个测试用例中,Johnny 可以通过对 10 向右移位 1 位(即除以 2)得到 5。
在第二个测试用例中,Johnny 可以通过对 11 向左移位 2 位(即乘以 4)得到 44。
在第三个测试用例中,Johnny 无法从 17 得到 21。
在第四个测试用例中,初始值与目标值相等,因此 Johnny 需要执行 0 次操作。
在第五个测试用例中,Johnny 可以通过对 96 进行两次向右移位(一次移位 2 位,另一次移位 3 位,即分别除以 4 和 8)得到 3。
输入解题思路,AI测评打分。不知道怎么写?