首页 > 其他 > 详细

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

时间:2020-06-05 23:49:12      阅读:108      评论:0      收藏:0      [点我收藏+]

题目链接:https://codeforces.com/contest/1362/problem/A

题意

有一个正整数 $a$,可选择的操作如下:

  • $a \times 2$
  • $a \times 4$
  • $a \times 8$
  • $a\ /\ 2$,如果 $2$ 整除 $a$
  • $a\ /\ 4$,如果 $4$ 整除 $a$
  • $a\ /\ 8$,如果 $8$ 整除 $a$

问能否由 $a$ 得到正整数 $b$,以及最少的操作次数。

题解

贪心模拟即可。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std;

void solve() {
    ll a, b; cin >> a >> b;
    if (a > b) swap(a, b);
    if (b % a != 0)
        cout << -1 << "\n";
    else {
        ll c = b / a;
        int t1 = 0, t2 = 0, t3 = 0;
        while (c % 8 == 0) c /= 8, ++t1;
        while (c % 4 == 0) c /= 4, ++t2;
        while (c % 2 == 0) c /= 2, ++t3;
        if (c == 1)
            cout << t1 + t2 + t3 << "\n";
        else
            cout << -1 << "\n";
    }
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

 

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

原文:https://www.cnblogs.com/Kanoon/p/13052778.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!