Hanpan home
Hanpan Studio home
Hanpan
·STUDIO
HanpanProblemsContestsSubmissionsRankingEvents
My problemsMy contests
StatementPlaySubmit codeLeaderboardTournamentFriendliesSubmissions
Challenge #3

Nim Game

Participant time limit
5,000 ms
Memory limit
512 MB
Submissions
113
Participants
23
Difficulty
Bronze VBronze IGold IIIRan Bronze V, Maki Bronze I, Laplace Gold III
On this page
  • Problem
  • Input
  • Example
  • Sample code

Problem

Two players take turns removing stones from several piles. The player who takes the last stone wins.

There are NNN piles, numbered from pile 111 to pile NNN starting from the left. NNN is between 111 and 101010 inclusive, and each pile initially has at least one stone. The total number of stones across all piles does not exceed 100100100. A pile keeps its number even after all of its stones are gone.

The game starts with the first player A, and A and B take turns alternately.

On your turn, choose one pile that still has stones and take one or more stones from it. You may take from only one pile at a time, and you cannot take more stones than that pile has left.

For example, if the piles currently hold [3,1,4][3, 1, 4][3,1,4] stones, taking 222 stones from pile 333 gives [3,1,2][3, 1, 2][3,1,2]. Pile 222 has only one stone, so you cannot take two from it.

Each player has a time limit of 111 second for the entire game. Only the time taken to output an action on your own turn is deducted, and the remaining time does not grow back on the next turn.

The following actions are invalid.

  • Printing a string that does not match the output format
  • Choosing a pile that does not exist or has no stones left
  • Taking fewer than one stone, or taking more stones than the chosen pile has left

A player who makes an invalid action loses immediately. An invalid action is not applied to the piles, and the opponent does not get another turn. A player also loses immediately if they fail to output an action within the time limit or if an error occurs while their program is running.

The game ends when any of the following conditions is met.

  • All piles are empty
  • A player makes an invalid action
  • A player exceeds the time limit or has a runtime error

If the game ends because all stones are gone, the player who took the last stone wins.

Design an AI that reads the number of stones left in each pile, takes the last stone, and becomes the final winner!

Input

Before the game starts, the judge gives both players the same initial piles as the following two lines.

N
stones_1 stones_2 ... stones_N
  • N is an integer between 111 and 101010 inclusive, giving the number of piles.
  • The second line gives the number of stones in each pile, stones_1, stones_2, ..., stones_N, separated by spaces.
  • Each pile has at least one stone, and the total number of stones across all piles does not exceed 100100100.
  • Piles are numbered starting from 111 at the left of the second line.

The judge communicates with the players one line at a time as follows.

Command Judge→Player (input) Player→Judge (output) Time limit (ms) Description
READY READY (FIRST | SECOND) OK 500 Tells the player whether they move first or second. FIRST is the first player A, and SECOND is the second player B.
OPP OPP, then on the next line PASS opponent_time or REMOVE heap_number take opponent_time - - Gives the opponent's previous action and remaining time. On the first player's first turn there is no opponent action, so PASS is given.
TURN TURN my_time REMOVE heap_number take my_time Gives your remaining time. Output the number of the pile to take stones from this turn and how many stones to take.
FINISH FINISH - - Announces the end of the game. The player must exit the program normally without any further output.
  • heap_number, take, my_time, and opponent_time are integers.
  • heap_number is between 111 and N inclusive and is the number of the pile to take stones from.
  • take is at least 111 and cannot exceed the number of stones currently left in the chosen pile.
  • my_time and opponent_time are given in milliseconds. my_time is your remaining time just before your action, and opponent_time is the opponent's remaining time after finishing their previous action.
  • OPP is always given as two lines. The first line is OPP, and the second line is PASS opponent_time or REMOVE heap_number take opponent_time.
  • Each player has 1,0001{,}0001,000 milliseconds for the entire game. READY response time is not included, and only the time spent responding to your own TURN is deducted.
  • The judge does not resend all piles every turn. You must track the current piles by applying your own actions and the opponent actions received via OPP to the initial state in order.
  • After every output, print a newline and then flush the buffer.
  • Print debug messages only to standard error. Do not print anything other than the specified responses to standard output.
  • If you fail to output within the time limit, you lose immediately by time limit exceeded. You also lose immediately if you print a string that does not match the output format or an invalid action, and that action is not passed to the opponent via OPP.

Example

In a match whose initial piles are [2,1][2, 1][2,1], communication can proceed as follows. The table below omits the two lines of initial state given to both players. Time values in the table are for illustration only.

First player input First player output Second player input Second player output
READY FIRST READY SECOND
OK OK
OPP
PASS 1000
TURN 1000 REMOVE 1 1
OPP
REMOVE 1 1 988
TURN 1000 REMOVE 2 1
OPP
REMOVE 2 1 991
TURN 988 REMOVE 1 1
FINISH FINISH

Sample code

  • CPP20 : main.cpp
  • PYTHON3, PYPY3 : main.py

Author

  • lll
    Bronze Vlll
PlaySubmit code
© 2026 hanpan
Privacy Policy

Tier icons are copyrighted by solved.ac. This service is not affiliated with solved.ac.