Statistics

Problem Statement for "StoneSolitaire"

Problem Statement

Stone solitaire is a single-player game played on a specially-crafted game board using some mutually indistinguishable stones.

The game board consists of a sequence of pits, sequentially numbered using non-negative integers. Each pit may contain arbitrarily many stones.

The goal of the game is to move all stones into pit number zero.


In a move the player performs the following steps:

  1. Selects a non-empty pit with a positive label.
  2. Notes the label x of the selected pit.
  3. Picks up all the stones from the selected pit.
  4. Redistributes the stones into pits with the next lower numbers (x-1, x-2, ...), placing exactly one stone into each pit.

If the player runs out of available pits while still trying to redistribute some stones, the player immediately loses the game.


In a turn the player starts by making a move. The move can have four possible outcomes:

  1. Obviously, if the player performed a move that caused them to lose the game, the whole game is now over and the player lost.
  2. If after the move all stones are now in pit number zero, the player won the game.
  3. If during the move the last stone was placed into a pit with a positive label, the turn is over.
  4. If during the move the last stone was placed exactly into pit number zero, the player gets to continue the turn by making another move.

The last rule applies to all moves made during the turn. Hence, if a player can find a suitable sequence of moves, their turn may consist of arbitrarily many moves.


For example, suppose the player starts a turn in a position where there are fifteen stones: one in pit 1, one in pit 2, three in pit 3, and ten in pit 7.

In the first move the player can choose any of the pits 1, 2, 3, and 7. These choices will have the following effect:

  • If the player chooses pit 1, they pick up the one stone from there and place it into pit 0. The move ends, and as the last stone was placed into pit 0, they get another move within the same turn.
  • If the player chooses pit 2, they pick up the one stone from there and place it into pit 1. The move ends and then the turn ends.
  • If the player chooses pit 3, they pick up the three stones from there. Then, they place one stone into pit 2, one into pit 1, and the final one into pit 0. In this case the player also gets to make another move this turn.
  • If the player chooses pit 7, they pick up the ten stones from there. Then, they place seven of those stones into pits 6 through 0. The player now needs to place three more stones but there are no more pits left, so the player loses the game.

A position is winning if pit number zero is empty and there is a way to win the game in a single turn.

The hash of a position in which pit x contains C[x] stones, for all x, is the sum of C[x]*10^x over all x.


Given N, find all positions that are winning and consist of exactly N stones. Return the sum of their hashes, modulo 10^9 + 7.

Definition

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

Notes

  • It can be shown that for each N the number of winning positions is finite and therefore the return value is always well-defined.

Constraints

  • N will be between 1 and 10^7, inclusive.

Examples

  1. 1

    Returns: 10

    Obviously, the only winning position with a single stone is the position in which the stone is in pit number 1. The way to win it is to perform a move in which we pick up the stone from pit 1 and place it into pit 0. The hash of this position is 1 * 10^1 = 10.

  2. 2

    Returns: 200

  3. 3

    Returns: 210

  4. 4

    Returns: 3100

  5. 5

    Returns: 3110

    There happens to be one winning position with five stones: the one depicted below. stones: __ __ 1 __ 1 __ 3 __ __ ... \___/ \___/ \___/ \___/ \___/ pit #: 0 1 2 3 4 Its hash is 1*10 + 1*100 + 3*1000 = 3110.

  6. 40

    Returns: 642512344

    Remember to compute all hashes and also their final sum modulo 10^9 + 7.

  7. 10000000

    Returns: 99864477

  8. 9368937

    Returns: 467752071

  9. 9065562

    Returns: 284627935

  10. 9451082

    Returns: 534814629

  11. 9580533

    Returns: 10761417

  12. 9475708

    Returns: 506976167

  13. 9598517

    Returns: 767438548

  14. 9360154

    Returns: 625343992

  15. 9269932

    Returns: 870431617

  16. 9537336

    Returns: 487125148

  17. 9405334

    Returns: 316927160

  18. 9411477

    Returns: 14591836

  19. 9645462

    Returns: 433211318

  20. 9042895

    Returns: 189293845

  21. 9833704

    Returns: 982089846

  22. 9438863

    Returns: 34550876

  23. 9690620

    Returns: 517968414

  24. 9024809

    Returns: 994627323

  25. 9107432

    Returns: 196236005

  26. 9000656

    Returns: 775156187

  27. 9276621

    Returns: 663013133

  28. 9988439

    Returns: 6794421

  29. 9830728

    Returns: 388503556

  30. 9504944

    Returns: 415632907

  31. 9759019

    Returns: 231260408

  32. 9573243

    Returns: 824464719

  33. 6

    Returns: 42000

  34. 1945526

    Returns: 413312203

  35. 755

    Returns: 869709195

  36. 450406

    Returns: 917950006

  37. 97261

    Returns: 239287700

  38. 13

    Returns: 6420010

  39. 13793

    Returns: 241279384

  40. 729791

    Returns: 728172589

  41. 548

    Returns: 121884518

  42. 106

    Returns: 686058474

  43. 55

    Returns: 426130408

  44. 1852998

    Returns: 480046303

  45. 394702

    Returns: 984773932

  46. 46

    Returns: 531432721

  47. 5886

    Returns: 687053120

  48. 159946

    Returns: 829274015

  49. 55905

    Returns: 619700465

  50. 3026

    Returns: 251359433

  51. 798584

    Returns: 267175672

  52. 8849

    Returns: 752007091

  53. 212915

    Returns: 80103923

  54. 1877953

    Returns: 331561758

  55. 1476

    Returns: 408172032

  56. 2575032

    Returns: 664981299

  57. 5931469

    Returns: 858073785


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: