#P8901. Circular Barn Cow Game

    ID: 22065 Type: Default 1000ms 256MiB

Circular Barn Cow Game

Circular Barn Cow Game

Two farmers, Farmer John and his nemesis Farmer Nhoj, play a game in a circular barn consisting of (N) rooms ((1 \le N \le 10^5)). In the (i)th room, there are initially (a_i) cows ((1 \le a_i \le 5\times10^6)). The game is played as follows:

  1. Both farmers start the game in room 1 and in every room they are together.
  2. Upon entering a room, each farmer takes an action in turn: Farmer John acts first, then Farmer Nhoj.
  3. If the room’s cow count is zero when it is a farmer’s turn to act, that farmer loses immediately.
  4. If there is at least one cow, the acting farmer must choose an integer \(P\) such that \(P = 1\) or \(P\) is a prime number with \(P \leq\) (current number of cows in the room). The farmer then removes \(P\) cows from the room.
  5. After both farmers have moved in the room (provided no one lost in between), they move together to the next room (room \(i+1\) or room 1 after room \(N\)).

When both farmers play optimally, the outcome in any room depends only on its initial cow count. In a room with \(x\) cows:
  • If \(x = 1\) or \(x\) is prime, the first mover can remove all \(x\) cows (since 1 and any prime number are allowed moves) and force an immediate win.
  • If \(x\) is composite, the first mover cannot remove all cows in one move. In such cases it turns out that the first mover can force a win if and only if \(x\) is not divisible by 4. (For example, for \(x=4, 8, 12, \ldots\) every allowed move leaves a winning position for the opponent.)

Since the farmers start in room 1 (with Farmer John to move first), the outcome of the whole game is determined solely by the cow count in room 1. Therefore, if the cow count in room 1 is either 1, prime, or composite but not divisible by 4, Farmer John wins; otherwise (i.e. a composite number divisible by 4), Farmer Nhoj wins.

inputFormat

The first line contains an integer (N) (the number of rooms).

The second line contains (N) space‐separated integers (a_1, a_2, \dots, a_N), where (a_i) is the initial number of cows in the (i)th room.

outputFormat

Output a single line: either Farmer John if Farmer John will win with optimal play, or Farmer Nhoj if Farmer Nhoj will win.

sample

3
4 6 8
Farmer Nhoj