fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, k, cnt[200004], visited[200004];
  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. for(int next : {here + 1, here - 1, here * 2}){
  15. if(next >= 0 && next <= 200000){
  16. if(!visited[next]){
  17. q.push(next);
  18. visited[next] = visited[here] + 1;
  19. cnt[next] = here;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. for(int i = k; i != n; i = cnt[i]){
  26. v.push_back(i);
  27. }
  28. cout << visited[k] - 1 << '\n';
  29. for(int a : v){
  30. cout << a << ' ';
  31. }
  32. }
Success #stdin #stdout 0.01s 5320KB
stdin
5 17
stdout
4
17 16 8 4