fork download
  1. // Runs in 0.28 sec
  2.  
  3. #include <cstdint>
  4. #include <iostream>
  5. #include <unordered_map>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int main();
  10.  
  11. typedef uint_fast32_t number;
  12. typedef uint_fast32_t sequencelength;
  13.  
  14. unordered_map<number, sequencelength> t;
  15.  
  16. void arrange(number&, number&);
  17.  
  18. sequencelength getlength(number);
  19.  
  20. int main()
  21. {
  22. ios_base::sync_with_stdio(false);
  23.  
  24. number i, j;
  25. while(cin >> i >> j)
  26. {
  27. cout << i << ' ' << j << ' ';
  28.  
  29. sequencelength maxlength = 0;
  30. for(arrange(i, j); i<=j; ++i)
  31. if(getlength(i) > maxlength)
  32. maxlength = getlength(i);
  33.  
  34. cout << maxlength << '\n';
  35. }
  36.  
  37. return 0;
  38. }
  39.  
  40. sequencelength getlength(number i)
  41. {
  42. if(!t.count(i))
  43. t.insert
  44. (make_pair(i, i == 1 ? 1 : getlength(i%2 == 0 ? i/2 : 3*i+1) + 1));
  45.  
  46. return t[i];
  47. }
  48.  
  49. void arrange(number &i, number &j)
  50. {
  51. if(i > j)
  52. swap(i, j);
  53. }
Success #stdin #stdout 0s 3404KB
stdin
1 10
100 200
201 210
900 1000
stdout
1 10 20
100 200 125
201 210 89
900 1000 174