fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cmath>
  5. #include <queue>
  6. #include <set>
  7. #include <algorithm>
  8. #define LL long long int
  9. #define pii pair<int, int>
  10. using namespace std;
  11. int n, k, t[100001];
  12. vector<int> v[100001];
  13. bool discovered[100001] = {false};
  14.  
  15. void bfs(int start){
  16. queue<int> q;
  17. discovered[start] = true;
  18. q.push(start);
  19. int f = 1;
  20. while(f){
  21. int here = q.front();
  22. q.pop();
  23. discovered[here] = true;
  24.  
  25. for(int i=0; i<v[here].size(); i++){
  26. int there = v[here][i];
  27.  
  28. if(!discovered[there]){
  29. q.push(there);
  30. discovered[there] = true;
  31. t[there] = t[here] + 1;
  32. if(there == k){
  33. cout << t[there] << "\n";
  34. f = 0;
  35. }
  36. }
  37. }
  38. }
  39.  
  40. }
  41. int main() {
  42. ios::sync_with_stdio(false);
  43. cin.tie(NULL);
  44. cout.tie(NULL);
  45.  
  46. cin >> n >> k;
  47.  
  48. for(int i=0; i<=99999; i++) v[i].push_back(i+1);
  49. for(int i=1; i<=100000; i++) v[i].push_back(i-1);
  50. for(int i=2; i<=50000; i++) v[i].push_back(2*i);
  51.  
  52. bfs(n);
  53.  
  54. }
Runtime error #stdin #stdout #stderr 0.02s 9284KB
stdin
1 1
stdout
Standard output is empty
stderr
double free or corruption (!prev)