fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, k, visited[500004], ret = -1;
  4. queue<int> q;
  5. int main(){
  6. cin >> n >> k;
  7. q.push(n);
  8. visited[n] = 1;
  9. int cnt = 1;
  10. while(q.size()){
  11. int here = q.front();
  12. q.pop();
  13.  
  14. if(here == k || here > 500000){
  15. ret = visited[k] - 1;
  16. break;
  17. }
  18.  
  19.  
  20. k += cnt++;
  21.  
  22. for(int next : {here + 1, here - 1, here * 2}){
  23. if(next < 0 || next > 500000 || visited[next]) continue;
  24. q.push(next);
  25. visited[next] = visited[here] + 1;
  26. }
  27. }
  28.  
  29. cout << ret << '\n';
  30. }
Success #stdin #stdout 0.02s 5564KB
stdin
250000 499999
stdout
-1