Connection Status:
Competition Arena > MinskyMysteryDiv2
SRM 529 · 2011-11-22 · by misof · Simple Math, Simulation
Class Name: MinskyMysteryDiv2
Return Type: long
Method Name: computeAnswer
Arg Types: (long long)
Problem Statement

Problem Statement

Marvin plays a simple game. The game is played with an infinite supply of marbles and five bags, labeled "bag 0" through "bag 4".

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:

    Add a marble into bag 1.
    Repeat forever:
        Add a marble into bag 1.
        Empty bag 4.
        While there are marbles in bag 0:
            While there are marbles both in bag 0 and in bag 1:
                Remove a marble from bag 0.
                Remove a marble from bag 1.
                Add a marble into bag 2.
                Add a marble into bag 3.
            End While
            Add a marble into bag 4.
            If bags 0 and 1 are both empty:
                Move all marbles from bag 3 to bag 4.
                TERMINATE THE GAME
            End If
            Move all marbles from bag 3 to bag 1.
        End While
        Move all marbles from bag 2 to bag 0.
    End Repeat

You are given a long N. Return a long containing the number of marbles that will be in bag 4 at the end of Marvin's game. If Marvin's game does not terminate for the given N, return -1 instead.

Notes

  • You may assume that the answer always fits into a long.
  • Note that N is allowed to be zero.

Constraints

  • N will be between 0 and 10^12, inclusive.
Examples
0)
2
Returns: 3
1)
3
Returns: 4
2)
4
Returns: 4
3)
15
Returns: 8
4)
0
Returns: -1

Submissions are judged against all 177 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class MinskyMysteryDiv2 with a public method long long computeAnswer(long long N) · 177 test cases · 2 s / 256 MB per case

Submitting as anonymous