fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, k, cnt[200004], visited[200004], ret;
  4. vector<int> v;
  5. int main(){
  6. cin >> n >> k;
  7. visited[n] = 1;
  8. queue<int> q;
  9. q.push(n);
  10.  
  11. while(q.size()){
  12. int here = q.front();
  13. q.pop();
  14.  
  15. if(here == k){
  16. ret = visited[here];
  17. break;
  18. }
  19. for(int next : {here + 1, here - 1, here * 2}){
  20. if(next >= 0 && next <= 200000){
  21. if(!visited[next]){
  22. q.push(next);
  23. visited[next] = visited[here] + 1;
  24. cnt[next] = here;
  25. }
  26. }
  27. }
  28. }
  29.  
  30. for(int i = k; i != n; i = cnt[i]){
  31. v.push_back(i);
  32. }
  33. v.push_back(n);
  34.  
  35. cout << ret - 1 << '\n';
  36. for(int a : v){
  37. cout << a << ' ';
  38. }
  39. }
Success #stdin #stdout 0s 5324KB
stdin
5 17
stdout
4
17 16 8 4 5