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. cout << k << ' ' << here << '\n';
  15.  
  16. if(here == k || k > 500000){
  17. ret = visited[k] - 1;
  18. break;
  19. }
  20.  
  21. k += cnt++;
  22.  
  23. for(int next : {here + 1, here - 1, here * 2}){
  24. if(next < 0 || next > 500000 || visited[next]) continue;
  25. q.push(next);
  26. visited[next] = visited[here] + 1;
  27. }
  28. }
  29.  
  30. cout << ret << '\n';
  31. }
Success #stdin #stdout 0s 5320KB
stdin
250000 499999
stdout
499999 250000
500000 250001
500002 249999
-1