Guest User

Untitled

a guest
May 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. Marvin plays a simple game. The game is played with an infinite supply of marbles and five bags, labeled "bag 0" through "bag 4".
  2. At the beginning, Marvin takes N marbles (where N is a nonnegative integer) and places them into bag 0. The remaining four bags are left empty. Marvin then follows this simple algorithm:
  3. Add a marble into bag 1.
  4. Repeat forever:
  5. Add a marble into bag 1.
  6. Empty bag 4.
  7. While there are marbles in bag 0:
  8. While there are marbles both in bag 0 and in bag 1:
  9. Remove a marble from bag 0.
  10. Remove a marble from bag 1.
  11. Add a marble into bag 2.
  12. Add a marble into bag 3.
  13. End While
  14. Add a marble into bag 4.
  15. If bags 0 and 1 are both empty:
  16. Move all marbles from bag 3 to bag 4.
  17. TERMINATE THE GAME
  18. End If
  19. Move all marbles from bag 3 to bag 1.
  20. End While
  21. Move all marbles from bag 2 to bag 0.
  22. End Repeat
  23. You are given a long long N. Count the number of times a marble enters a bag during Marvin's game. That is, compute X+Y, where X is the number of times a marble is added to some bag, and Y is the number of times a marble is moved from one bag to another. To avoid overflows, return just the value ((X+Y) modulo 1,000,000,009). If Marvin's game does not terminate for the given N, return -1 instead.
Add Comment
Please, Sign In to add comment