Codeforces Round #647 (Div. 2) A. Johnny and Ancient Computer (思维)

题目链接
题意:
给你一个a和b,现在a只能乘2,乘4,乘8,或者除2,除4,除8,前提是必须整除,问现在最少多少次,a能转化成b。

思路:
1.如果a>b,那么肯定是除,那么我们何不反过来,现在我们让b乘2,让其对于a。

2.如果a

3.那么我们如何判断呢?我们可以看到4和8都是2的倍数,所以我们一直乘2就是了,看是否能相等。

4.最后再把一共乘2的次数,先除3表示乘8的次数 ,然后再除2表示乘4的次数 ,最后再加起来即可

AC代码

#include 
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 5e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t;
    cin >> t;
    while(t--)
    {
        ll a,b,sum = 0,ans = 0;
        cin >> a >> b;
        if(a > b) swap(a,b);
        while(a < b)
        {
            a *= 2;
            sum++;
        }
        ll i = sum/3;//乘8的次数
        sum = sum - i*3;
        ll j = sum/2;//乘4的次数
        sum = sum - j*2;//剩下的就是乘2的次数
        a != b ? cout << -1 << endl : cout << sum+i+j << endl;
    }
}

你可能感兴趣的:(codeforces,思维)