fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. uint64_t n, a, b; cin >> n >> a >> b;
  5. multiset<uint64_t> s;
  6. for (uint64_t m=1<<n;m;m /= 2) { if (a&m) s.insert(m); if (b&m) s.insert(m); }
  7. int ans = 0;
  8. while (*s.begin() != (1 << n)) {
  9. auto first = s.begin(), second = next(first);
  10. if (second != s.end() && *first == *second) {
  11. uint64_t nxt = *first + *second;
  12. s.erase(first); s.erase(second);
  13. s.insert(nxt);
  14. } else {
  15. uint64_t nxt = *first * 2;
  16. s.erase(first);
  17. s.insert(nxt);
  18. }
  19. ++ans;
  20. }
  21. if (s.size() == 1) {
  22. cout << ans << endl;
  23. } else {
  24. cout << "impossible" << endl;
  25. }
  26. }
  27.  
Success #stdin #stdout 0s 16064KB
stdin
5 8 12
stdout
4