fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define ld long double
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define pp pop
  8. #define p push
  9. using namespace std;
  10. const int N = 2e5 + 9;
  11. int n,m,cnt_trace[N];
  12. vector<int> a[N];
  13. bool visited[N];
  14. void bfs(int x)
  15. {
  16. queue<int> q;
  17. q.p(x);
  18. visited[x] = true;
  19. while (q.size()!=0)
  20. {
  21. int y = q.front();
  22. q.pp();
  23. for(auto z:a[y])
  24. if(!visited[z])
  25. {
  26. q.p(z);
  27. visited[z] = true;
  28. cnt_trace[z] = cnt_trace[y]+1;
  29. }
  30. }
  31. }
  32. int main()
  33. {
  34. //freopen("thaotacvoiso.inp","r",stdin);
  35. //freopen("thaotacvoiso.out","w",stdout);
  36. ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  37. cin>>n>>m;
  38. if(n>=m)
  39. cout<<n-m;
  40. else
  41. {
  42. for(int i=1;i<=m+1;++i)
  43. {
  44. a[i].pb(i-1);
  45. a[i].pb(i*2);
  46. }
  47. memset(visited,false,N);
  48. cnt_trace[n] = 0;
  49. bfs(n);
  50. cout<<cnt_trace[m];
  51. }
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 8216KB
stdin
Standard input is empty
stdout
Standard output is empty