#P8901. [USACO22DEC] Circular Barn S
[USACO22DEC] Circular Barn S
Description
Farmer John and his archrival Farmer Nhoj play a game in a circular barn. The barn has rooms , and the -th room initially contains cows . The game proceeds as follows:
- The two farmers are always in the same room. Upon entering a room, each farmer takes one move, with Farmer John moving first. At the start of the game, they enter room .
- If the current room has zero cows, then the farmer whose turn it is loses. Otherwise, the farmer whose turn it is chooses an integer , where is or a prime number not exceeding the number of cows in the current room, and removes cows from the current room.
- After both farmers have completed their moves, they move to the next room in the circular barn. That is, if they are in room , they move to room , unless they are in room , in which case they move to room .
Determine the winner when both farmers play optimally.
Input Format
The input contains subtest cases. The first line contains . Then follow subtest cases.
For each subtest case, the first line contains , and the second line contains .
It is guaranteed that the sum of all does not exceed .
Output Format
For each subtest case, output the winner, either Farmer John or Farmer Nhoj.
5
1
4
1
9
2
2 3
2
7 10
3
4 9 4
Farmer Nhoj
Farmer John
Farmer John
Farmer John
Farmer Nhoj
Hint
Sample 1 Explanation
For the first subtest case, Farmer John can remove , , or cows from the first room. No matter how many he removes, Nhoj can remove the remaining cows, forcing FJ to lose when they loop back to the first room.
For the second subtest case, FJ can remove cows, forcing Nhoj to face the remaining cows. Now Nhoj can remove , , or cows. The situation is now similar to the first subtest case.
For the third and fourth subtest cases, FJ can immediately remove all cows from the first room, causing Nhoj to lose.
For the fifth subtest case, FJ can remove , , or cows from the first room, and then Nhoj can remove the remaining cows afterward. When they loop back to the first room, FJ will lose.
Test Point Properties
- Test points – satisfy .
- Test points – satisfy .
- Test points – have no additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号