fork(1) download
  1. #include <stdio.h>
  2.  
  3. int cycle_length(int n);
  4. int max_cycle_length (int low, int high);
  5.  
  6. int CYCLE_LENGTHS[1000001];
  7.  
  8. int main(int argc, char *argv[]) {
  9. int low, high, max_length;
  10.  
  11. while (scanf("%d %d", &low, &high) != EOF) {
  12. int max_length = max_cycle_length(low, high);
  13. printf("%d %d %d\n", low, high, max_length);
  14. }
  15. }
  16.  
  17. int max_cycle_length (int low, int high) {
  18. int max_length = 0;
  19.  
  20. if (high < low) {
  21. int temp = high;
  22. high = low;
  23. low = temp;
  24. }
  25.  
  26. int i;
  27. for (i = low; i <= high; ++i) {
  28. int current = cycle_length(i);
  29. if (current > max_length) max_length = current;
  30. }
  31.  
  32. return max_length;
  33. }
  34.  
  35. int cycle_length (int n) {
  36. if (CYCLE_LENGTHS[n] != 0) {
  37. return CYCLE_LENGTHS[n];
  38. }
  39.  
  40. long long nL = n;
  41. int cycles = 1;
  42.  
  43. while (nL != 1) {
  44. if ((nL % 2) == 0) nL = nL/2;
  45. else nL = (3*nL)+1;
  46. ++cycles;
  47. }
  48.  
  49. CYCLE_LENGTHS[n] = cycles;
  50. return cycles;
  51. }
Success #stdin #stdout 0.84s 5628KB
stdin
1 10
10 1
100 200
201 210
900 1000
1000 900
999999 999990
1 1000000
1 1000000
stdout
1 10 20
10 1 20
100 200 125
201 210 89
900 1000 174
1000 900 174
999999 999990 259
1 1000000 525
1 1000000 525