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