博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #647 (Div. 2) - Thanks, Algo Muse!A. Johnny and Ancient Computer(数学)---题解
阅读量:4034 次
发布时间:2019-05-24

本文共 3090 字,大约阅读时间需要 10 分钟。

来源:

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.

Input

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.

Output

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.

Example

input
10
10 5
11 44
17 21
1 1
96 3
2 128
1001 1100611139403776
1000000000000000000 1000000000000000000
7 1
10 8
output
1
1
-1
0
2
2
14
0
-1
-1

Note

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).

题意:

给你两个数a,b,问你是否存在a左移/右移后会得到b,如果存在的话,每次可以移位1,2,3位,最少移动几次。

思路:

我们只要先判断是否存在,如果b/a=2*n(n是任意数)存在的话,那么就说明能实现,然后我们可以求最小的次数了。

代码:

#include
#include
#include
using namespace std;typedef long long ll;int main(){
int t; cin >> t; ll a, b; while (t--) {
cin >> a >> b; if (b > a) swap(a, b); ll sum = 0; if (a == b) {
cout << 0 << endl; continue; } if (a % b) {
cout << -1 << endl; continue; } ll temp = a / b; while (temp % 8 == 0) {
temp /= 8; sum++; } while (temp % 4 == 0) {
sum++; temp /= 4; } while (temp % 2 == 0) {
temp /= 2; sum++; } if (temp == 1) cout << sum << endl; else cout << -1 << endl; } return 0;}

转载地址:http://yofdi.baihongyu.com/

你可能感兴趣的文章
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>