Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. freopen("space_in.txt", "r", stdin);
  8. freopen("space_out.txt", "w", stdout);
  9.  
  10. int a, b;
  11. scanf("%d %d", &a, &b);
  12.  
  13. map <int, int> dist;
  14.  
  15. queue <int> q;
  16. q.push(a);
  17.  
  18. while (!q.empty())
  19. {
  20. int k = q.front();
  21. q.pop();
  22.  
  23. if (k % 2 != 0)
  24. {
  25. if ((dist.count(2 * k) == 0 || dist[2 * k] > dist[k] + 1) && (2 * k <= b))
  26. {
  27. dist[2 * k] = dist[k] + 1;
  28. q.push(2 * k);
  29. }
  30. }
  31. else
  32. {
  33. if ((dist.count(2 * k + 1) == 0 || dist[2 * k + 1] > dist[k] + 1) && (2 * k + 1 <= b))
  34. {
  35. dist[2 * k + 1] = dist[k] + 1;
  36. q.push(2 * k + 1);
  37. }
  38. }
  39.  
  40. }
  41.  
  42. printf("%d", dist[b]);
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement