Statistics

Problem Statement for "TallyMarksSystem"

Problem Statement

Time limit: 4 seconds.

In tally marks, any non-negative integer can be represented using zero or more symbols for the number 5, followed by between 0 and 4 symbols for the number 1.

For example, we can represent 3 as "111", 15 as "555", and 17 as "55511".


Of course, representing large numbers this way is impractical.

We will investigate what happens if we allow parentheses, addition and multiplication in the representation of our numbers. E.g., instead of "555555555555555555555555555555555555555511" it might be a better idea to represent 202 as "(55*55+1)*11" (i.e., ten times ten plus one, and all of that multiplied by two).


Given N, find and return the smallest number of tally symbols ('5's and '1's) in an expression of the above form that evaluates to N.

Definition

Class:
TallyMarksSystem
Method:
construct
Parameters:
int
Returns:
int
Method signature:
int construct(int N)
(be sure your method is public)

Notes

  • The expressions may contain an arbitrary amount of '+', '*', '(' and ')' symbols, we are only interested in minimizing the number of tally marks used.

Constraints

  • N will be between 1 and 2,000,000, inclusive.

Examples

  1. 3

    Returns: 3

    The best way to represent the number 3 is still "111". (This is no longer the only optimal way, but there is no better one.)

  2. 4

    Returns: 4

    One best way to represent 4 is "1111", another is "(1+1)*(1+1)". Both use four tally mark symbols.

  3. 1

    Returns: 1

  4. 2

    Returns: 2

  5. 30

    Returns: 3

    The best way to represent 30 is "5*51", i.e., five times six. This representation uses three tally mark symbols: '5', '5', '1'.

  6. 202

    Returns: 7

    One optimal way to represent 202 is the one shown in the statement.

  7. 1301

    Returns: 7

  8. 1692601

    Returns: 14

  9. 5

    Returns: 1

  10. 6

    Returns: 2

  11. 7

    Returns: 3

  12. 8

    Returns: 4

  13. 9

    Returns: 5

  14. 10

    Returns: 2

  15. 11

    Returns: 3

  16. 12

    Returns: 4

  17. 13

    Returns: 5

  18. 14

    Returns: 5

  19. 15

    Returns: 3

  20. 16

    Returns: 4

  21. 17

    Returns: 5

  22. 18

    Returns: 5

  23. 19

    Returns: 6

  24. 20

    Returns: 4

  25. 21

    Returns: 5

  26. 22

    Returns: 5

  27. 23

    Returns: 6

  28. 24

    Returns: 6

  29. 25

    Returns: 2

  30. 26

    Returns: 3

  31. 27

    Returns: 4

  32. 28

    Returns: 5

  33. 29

    Returns: 6

  34. 212

    Returns: 8

  35. 247

    Returns: 8

  36. 306

    Returns: 6

  37. 377

    Returns: 7

  38. 466

    Returns: 8

  39. 629

    Returns: 8

  40. 1152

    Returns: 9

  41. 1264

    Returns: 9

  42. 1290

    Returns: 9

  43. 1502

    Returns: 8

  44. 1616

    Returns: 9

  45. 1651

    Returns: 8

  46. 1793

    Returns: 10

  47. 1817

    Returns: 10

  48. 1924

    Returns: 10

  49. 1961

    Returns: 10

  50. 1972

    Returns: 11

  51. 2068

    Returns: 11

  52. 2182

    Returns: 11

  53. 2308

    Returns: 12

  54. 72160

    Returns: 12

  55. 76386

    Returns: 12

  56. 207810

    Returns: 14

  57. 265592

    Returns: 15

  58. 413407

    Returns: 13

  59. 500981

    Returns: 15

  60. 502821

    Returns: 16

  61. 510014

    Returns: 16

  62. 702698

    Returns: 17

  63. 719446

    Returns: 16

  64. 743644

    Returns: 16

  65. 755450

    Returns: 16

  66. 763328

    Returns: 18

  67. 787086

    Returns: 17

  68. 929805

    Returns: 15

  69. 1080238

    Returns: 17

  70. 1099776

    Returns: 17

  71. 1172172

    Returns: 15

  72. 1178789

    Returns: 18

  73. 1208479

    Returns: 17

  74. 1215293

    Returns: 18

  75. 1262738

    Returns: 18

  76. 1444405

    Returns: 16

  77. 1505497

    Returns: 19

  78. 1531537

    Returns: 16

  79. 1600217

    Returns: 17

  80. 1656234

    Returns: 18

  81. 1765317

    Returns: 16

  82. 1912726

    Returns: 16

  83. 1914437

    Returns: 17

  84. 1921198

    Returns: 19

  85. 1928646

    Returns: 17

  86. 1929747

    Returns: 18

  87. 1931323

    Returns: 18

  88. 1933139

    Returns: 19

  89. 1937528

    Returns: 16

  90. 1943890

    Returns: 16

  91. 1946274

    Returns: 18

  92. 1950276

    Returns: 15

  93. 1954612

    Returns: 16

  94. 1961738

    Returns: 18

  95. 1964357

    Returns: 16

  96. 1966624

    Returns: 17

  97. 1970795

    Returns: 17

  98. 1972779

    Returns: 18

  99. 1979253

    Returns: 17

  100. 1985040

    Returns: 14

  101. 1988454

    Returns: 17

  102. 1991646

    Returns: 17

  103. 1997541

    Returns: 18

  104. 9868

    Returns: 13

  105. 199923

    Returns: 16

  106. 31458

    Returns: 14

  107. 1955996

    Returns: 18

  108. 782247

    Returns: 18

  109. 1997199

    Returns: 18

  110. 392389

    Returns: 17

  111. 1999999

    Returns: 18

  112. 34646

    Returns: 14


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: