Statistics

Problem Statement for "LastDigit"

Problem Statement

Limak chose a positive integer X and wrote it on a blackboard. After that, every day he erased the last digit of the current number. He stopped when he erased all digits.

After the process was over, Limak computed the sum of all numbers that appeared on the blackboard, including the original number X.

For example, if the original number was 509, the numbers that appeared on the blackboard were the numbers 509, 50, and 5. Their sum is 564.

You are given a long S. Limak wants you to find a number X such that the above process produces the sum S. It can be shown that for any positive S there is at most one valid X. If there is a valid X, find and return it. Otherwise, return -1.

Definition

Class:
LastDigit
Method:
findX
Parameters:
long
Returns:
long
Method signature:
long findX(long S)
(be sure your method is public)

Constraints

  • S will be between 1 and 10^18, inclusive.

Examples

  1. 564

    Returns: 509

    This is the example from the problem statement. The value X = 509 produces the sum 564, as shown above.

  2. 565

    Returns: -1

    There is no X for which the sum would be 565.

  3. 3000

    Returns: 2701

    You can check that 2701 + 270 + 27 + 2 = 3000.

  4. 137174210616796

    Returns: 123456789555123

  5. 837592744927492746

    Returns: -1

  6. 999999999999999999

    Returns: 900000000000000000

  7. 1367758989

    Returns: 1230983094

  8. 45865856567567

    Returns: 41279270910816

  9. 679674734645

    Returns: 611707261185

  10. 58491090301390

    Returns: 52641981271257

  11. 10

    Returns: -1

  12. 1

    Returns: 1

  13. 2

    Returns: 2

  14. 3

    Returns: 3

  15. 4

    Returns: 4

  16. 5

    Returns: 5

  17. 6

    Returns: 6

  18. 7

    Returns: 7

  19. 8

    Returns: 8

  20. 9

    Returns: 9

  21. 10

    Returns: -1

  22. 11

    Returns: 10

  23. 1000000000000000000

    Returns: 900000000000000001

  24. 99999999999999999

    Returns: 90000000000000000

  25. 99999999999999998

    Returns: -1

  26. 99999999999999997

    Returns: -1

  27. 99999999999999996

    Returns: -1

  28. 99999999999999995

    Returns: -1

  29. 99999999999999994

    Returns: -1

  30. 99913239999999

    Returns: 89921916000004

  31. 43242341352

    Returns: 38918107221

  32. 141234124325

    Returns: 127110711897

  33. 42523423532

    Returns: 38271081183

  34. 10000234234

    Returns: 9000210813

  35. 88776443108868985

    Returns: 79898798797982099

  36. 333333333333333315

    Returns: 299999999999999999

  37. 26

    Returns: 24

  38. 472956193893217459

    Returns: 425660574503895721


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: