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:
- Selects a non-empty pit with a positive label.
- Notes the label x of the selected pit.
- Picks up all the stones from the selected pit.
- 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:
- Obviously, if the player performed a move that caused them to lose the game, the whole game is now over and the player lost.
- If after the move all stones are now in pit number zero, the player won the game.
- If during the move the last stone was placed into a pit with a positive label, the turn is over.
- 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
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
Returns: 200
3
Returns: 210
4
Returns: 3100
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.
40
Returns: 642512344
Remember to compute all hashes and also their final sum modulo 10^9 + 7.
10000000
Returns: 99864477
9368937
Returns: 467752071
9065562
Returns: 284627935
9451082
Returns: 534814629
9580533
Returns: 10761417
9475708
Returns: 506976167
9598517
Returns: 767438548
9360154
Returns: 625343992
9269932
Returns: 870431617
9537336
Returns: 487125148
9405334
Returns: 316927160
9411477
Returns: 14591836
9645462
Returns: 433211318
9042895
Returns: 189293845
9833704
Returns: 982089846
9438863
Returns: 34550876
9690620
Returns: 517968414
9024809
Returns: 994627323
9107432
Returns: 196236005
9000656
Returns: 775156187
9276621
Returns: 663013133
9988439
Returns: 6794421
9830728
Returns: 388503556
9504944
Returns: 415632907
9759019
Returns: 231260408
9573243
Returns: 824464719
6
Returns: 42000
1945526
Returns: 413312203
755
Returns: 869709195
450406
Returns: 917950006
97261
Returns: 239287700
13
Returns: 6420010
13793
Returns: 241279384
729791
Returns: 728172589
548
Returns: 121884518
106
Returns: 686058474
55
Returns: 426130408
1852998
Returns: 480046303
394702
Returns: 984773932
46
Returns: 531432721
5886
Returns: 687053120
159946
Returns: 829274015
55905
Returns: 619700465
3026
Returns: 251359433
798584
Returns: 267175672
8849
Returns: 752007091
212915
Returns: 80103923
1877953
Returns: 331561758
1476
Returns: 408172032
2575032
Returns: 664981299
5931469
Returns: 858073785