Statistics

Problem Statement for "MultiplyXPlusOne"

Problem Statement

There is a card and at the beginning there is a number s on it, in each step you can do one of this operation:
  • Suppose the number on this card is x, change it into 2x+1.
  • Suppose the number on this card is x, change it into 3x+1.
Please compute and return the minimal number of operations to change the number of your card into t. If that is impossible, return -1.

Definition

Class:
MultiplyXPlusOne
Method:
minimalSteps
Parameters:
int, int
Returns:
int
Method signature:
int minimalSteps(int s, int t)
(be sure your method is public)

Constraints

  • s will be between 0 and 1,000,000, inclusive.
  • t will be between 0 and 1,000,000, inclusive.

Examples

  1. 1

    22

    Returns: 3

    First we use operation 1, the number will become 2*1+1 = 3. Then we use operation 1 again, we get 3*2+1 = 7. Last we use operation 2, it will be 7*3+1 = 22.

  2. 1

    31

    Returns: 3

    Although we can get it by 1, 3, 7, 15, 31, we could use less steps by 1, 3, 10, 31.

  3. 100

    99

    Returns: -1

    We can't decrease the number, so it is impossible to get 99 from 100.

  4. 55555

    1000000

    Returns: 3

  5. 1

    1

    Returns: 0

  6. 1

    4844

    Returns: -1

  7. 10

    18929

    Returns: -1

  8. 9

    712211

    Returns: -1

  9. 4

    10

    Returns: -1

  10. 4

    83198

    Returns: -1

  11. 92

    897

    Returns: -1

  12. 9

    911

    Returns: -1

  13. 888

    119731

    Returns: -1

  14. 38

    99

    Returns: -1

  15. 46

    656

    Returns: -1

  16. 941533

    964384

    Returns: -1

  17. 9

    8558

    Returns: -1

  18. 286

    21229

    Returns: -1

  19. 53

    86497

    Returns: -1

  20. 29969

    648178

    Returns: -1

  21. 65

    5201

    Returns: -1

  22. 10

    341381

    Returns: -1

  23. 4069

    8529

    Returns: -1

  24. 3

    35845

    Returns: -1

  25. 2748

    827341

    Returns: -1

  26. 5548

    448938

    Returns: -1

  27. 577

    978660

    Returns: -1

  28. 8

    2075

    Returns: -1

  29. 2

    2859

    Returns: -1

  30. 2

    445885

    Returns: -1

  31. 3083

    7521

    Returns: -1

  32. 53

    7346

    Returns: -1

  33. 58

    4265

    Returns: -1

  34. 17

    54

    Returns: -1

  35. 1234

    9529

    Returns: -1

  36. 7

    1791

    Returns: -1

  37. 650

    708

    Returns: -1

  38. 31533

    83

    Returns: -1

  39. 66521

    93

    Returns: -1

  40. 946312

    20825

    Returns: -1

  41. 9590

    714920

    Returns: -1

  42. 71746

    664

    Returns: -1

  43. 2715

    36

    Returns: -1

  44. 187

    149707

    Returns: -1

  45. 710

    198

    Returns: -1

  46. 71

    4016

    Returns: -1

  47. 31286

    781

    Returns: -1

  48. 6735

    29

    Returns: -1

  49. 63

    90

    Returns: -1

  50. 240632

    8595

    Returns: -1

  51. 24242

    1

    Returns: -1

  52. 44283

    757512

    Returns: -1

  53. 280

    565707

    Returns: -1

  54. 31853

    3047

    Returns: -1

  55. 198085

    7

    Returns: -1

  56. 6

    722443

    Returns: 12

  57. 985

    851518

    Returns: 8

  58. 20675

    372163

    Returns: 3

  59. 99779

    399119

    Returns: 2

  60. 7211

    519259

    Returns: 5

  61. 93

    545481

    Returns: 9

  62. 38734

    464818

    Returns: 3

  63. 275497

    826492

    Returns: 1

  64. 4

    444007

    Returns: 13

  65. 276826

    553653

    Returns: 1

  66. 1

    728272

    Returns: 13

  67. 725

    626950

    Returns: 8

  68. 1919

    621921

    Returns: 6

  69. 36

    378879

    Returns: 11

  70. 5

    513691

    Returns: 13

  71. 333543

    333543

    Returns: 0

  72. 61223

    734686

    Returns: 3

  73. 68020

    612184

    Returns: 2

  74. 2

    342783

    Returns: 14

  75. 270105

    810316

    Returns: 1

  76. 1

    5

    Returns: -1

  77. 4

    22

    Returns: -1

  78. 1

    2

    Returns: -1


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: