Presidential Game
Time limit1sMemory limit512 MB
Two players alternately merge 2 to K adjacent elements, John by sum and Preston by XOR, until one value remains; decide who wins when John needs the final value odd.
- Level
Hard8 of 10
- Topics
- Game theory, Dynamic programming, Math, Bit manipulation
- Solved
- No attempts yet
Problem
Two candidates are running in this year's presidential election, John Wee and Preston Wo. Taking a break from promoting their campaigns, they decide to play a game with an array of integers. The candidates make alternating moves until a single element remains, starting with John, the current president.
In each turn, John chooses a contiguous subarray of containing between and elements, inclusive, and replaces the subarray with a single element equal to the sum of all elements in the subarray. In each turn, Preston chooses a contiguous subarray of containing between and elements, inclusive, and replaces the subarray with a single element equal to the bitwise XOR of all elements in the subarray. The bitwise XOR of an array of integers is defined as follows, where denotes the bitwise XOR operator.
Since John is candidate number 1, John wins the game if the single element left in the array is odd. Otherwise, if the single element left in the array is even, Preston wins the game. You want to predict who wins the game when both candidates play optimally.
Input
The first line contains two integers (), the length of the array and the maximum length of the subarray that can be chosen in each turn. The next line contains integers (), the array .
Output
Output in a single line the name of the winner, either John or Preston.