codeforces A. Johnny and Ancient Computer

codeforces A. Johnny and Ancient Computer_第1张图片

题目

题意:

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

思路:

我们只要先判断是否存在,如果 b / a = 2 k b/a=2^k b/a=2k存在的话,那么就说明肯定能通过移位实现,然后我们每次进行除 8 8 8操作,这样每次就可以除最大,更快的得到 a a a。(条件:b>a)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
int xxxxxxxxx = 1;
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
inline void debug(ll x) {cout << xxxxxxxxx++ << " " << x << endl;}
inline void debugs(string s) {cout << s << endl;}
int main() {
    int t;
    read(t);
    while (t--) {
        ll a, b;
        read(a), read(b);
        if (a > b) swap(a, b);
        if (a == b) {
            printf("0\n");
            continue;
        }
        ll x = b / a;
        if (b % a == 0 && (x & (x - 1)) == 0) {
            ll ans = 0;
            while (b > a) {
                b /= 8;
                ans++;
            }
            printf("%lld\n", ans);
        } else printf("-1\n");
    }
    return 0;
}


你可能感兴趣的:(codeforces)