Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- freopen("space_in.txt", "r", stdin);
- freopen("space_out.txt", "w", stdout);
- int a, b;
- scanf("%d %d", &a, &b);
- map <int, int> dist;
- queue <int> q;
- q.push(a);
- while (!q.empty())
- {
- int k = q.front();
- q.pop();
- if (k % 2 != 0)
- {
- if ((dist.count(2 * k) == 0 || dist[2 * k] > dist[k] + 1) && (2 * k <= b))
- {
- dist[2 * k] = dist[k] + 1;
- q.push(2 * k);
- }
- }
- else
- {
- if ((dist.count(2 * k + 1) == 0 || dist[2 * k + 1] > dist[k] + 1) && (2 * k + 1 <= b))
- {
- dist[2 * k + 1] = dist[k] + 1;
- q.push(2 * k + 1);
- }
- }
- }
- printf("%d", dist[b]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement