Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void add(int &a)
- {
- a = (a + 1) % 1000000;
- }
- int main()
- {
- int n, m, q[(int)4e6 + 5], h, t;
- vector<int> a(4e5, 1e9);
- cin >> n >> m;
- a[n] = h = t = 0;
- q[h] = n;
- add(h);
- for (;;)
- {
- for (; t != h; add(t))
- {
- int temp = q[t];
- if (temp == m) return cout << a[m], 0;
- if (temp * 2 < 1e5 && a[temp * 2] > a[temp] + 1)
- {
- a[temp * 2] = a[temp] + 1;
- q[h] = temp * 2;
- add(h);
- }
- if (temp > 1 && a[temp - 1] > a[temp] + 1)
- {
- a[temp - 1] = a[temp] + 1;
- q[h] = temp - 1;
- add(h);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement