Codeforces 647div2A

Codeforces #647div2 A

原题:

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 22, 44 or 88, and division is only allowed if the number is divisible by the chosen divisor.

Formally, if the register contains a positive integer xx, in one operation it can be replaced by one of the following:

x⋅2x⋅2
x⋅4x⋅4
x⋅8x⋅8
x/2x/2, if xx is divisible by 22
x/4x/4, if xx is divisible by 44
x/8x/8, if xx is divisible by 88
For example, if x=6x=6, in one operation it can be replaced by 1212, 2424, 4848 or 33. Value 66 isn’t divisible by 44 or 88, so there’re only four variants of replacement.

Now Johnny wonders how many operations he needs to perform if he puts aa in the register and wants to get bb at the end.

Input
The input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The following tt lines contain a description of test cases.

The first and only line in each test case contains integers aa and bb (1≤a,b≤10181≤a,b≤1018) — the initial and target value of the variable, respectively.

Output
Output tt lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get bb at the end, then write −1−1.

思路:模拟,考虑的优先级是8,4,2。

ACcodes

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int test=1;
int main()
{
    scanf("%d",&test);
    while(test--)
    {
        long long a,b;cin>>a>>b;
        if(a<b)
            swap(a,b);
        int tot=0;
        long long maxn = a,minn = b;
        bool fl=false;
        while(maxn!=minn){
            if(maxn%2)
            {
                fl=1;
                break;
            }
            maxn/=2;
        }
        if(a==b){
            cout<<"0" << endl;
        }
        else if(a%b || (b==1&&a%2) || (a/b)%2 || fl){
            cout<<"-1"<<endl;
        }
        else
        {
            long long res=a/b,ans=0;
            while(res>7){ans++;res/=8;}
            while(res>3){ans++;res/=4;}
            while(res>1){ans++;res/=2;}
            cout << ans << endl;
        }
        
    }
    //system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值