题目链接:https://codeforces.com/contest/1362/problem/A
有一个正整数 $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