GameGame (Codeforces 1384D)
Tags: Math, games

Koa the Koala and her best friend want to play a game.The game starts with an array $$$a$$$ of length $$$n$$$ consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to $$$0$$$. Koa starts.Let's describe a move in the game:  During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player. More formally: if the current score of the player is $$$x$$$ and the chosen element is $$$y$$$, his new score will be $$$x \oplus y$$$. Here $$$\oplus$$$ denotes bitwise XOR operation. Note that after a move element $$$y$$$ is removed from $$$a$$$.  The game ends when the array is empty. At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.If both players play optimally find out whether Koa will win, lose or draw the game.

Input: Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows.The first line of each test case contains the integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of $$$a$$$.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — elements of $$$a$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case print:  WIN if Koa will win the game.  LOSE if Koa will lose the game.  DRAW if the game ends in a draw. 

Note: In testcase $$$1$$$ of the first sample we have:$$$a = [1, 2, 2]$$$. Here Koa chooses $$$1$$$, other player has to choose $$$2$$$, Koa chooses another $$$2$$$. Score for Koa is $$$1 \oplus 2 = 3$$$ and score for other player is $$$2$$$ so Koa wins.

Sample Inputs:
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2

4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4

Sample Outputs:
WIN
LOSE
DRAW

WIN
WIN
DRAW
WIN

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, arr) = (readInt(), readList())
    cnt = [0] * 40
    for num in arr:
        i = 0
        while num:
            cnt[i] += num & 1
            num >>= 1
            i += 1
    for i in range(39, -1, -1):
        if cnt[i] % 2:
            r = cnt[i] % 4
            if r == 1:
                return 'WIN'
            elif (n - cnt[i]) % 2:
                return 'WIN'
            else:
                return 'LOSE'
    return 'DRAW'
for _ in range(int(input())):
    print(solve())

Solution (Python):
def calc(nums):
    for i in range(30, -1, -1):
        count = 0
        for num in nums:
            if num & 1 << i > 0:
                count += 1
        if count % 2 != 0:
            return count
    return -1
t = int(input())
for _ in range(t):
    n = int(input())
    nums = list(map(int, input().split()))
    flag = calc(nums)
    if flag == -1:
        print('DRAW')
    elif (flag - 1) // 2 % 2 != 0 and n % 2 != 0:
        print('LOSE')
    else:
        print('WIN')

Solution (Python):
def DecimalToBinary(a):
    s = []
    while a >= 1:
        s.append(a % 2)
        a = a // 2
    return s

def find_sum(a, p, sum):
    for i in range(len(a)):
        if a[i] // 2 ** p > 0:
            for j in range(p):
                a[i] = a[i] // 2
            sum = sum + a[i] % 2
    return sum
t = int(input())
l = []
for i in range(t):
    n = int(input())
    sum = 0
    a = list(map(int, input().split()))
    xor = a[0]
    for i in range(1, n):
        xor = xor ^ a[i]
    xor = DecimalToBinary(xor)
    lenth = len(xor) - 1
    sum = find_sum(a, lenth, sum)
    if sum == 1:
        l.append('WIN')
    elif sum % 2 == 0:
        l.append('DRAW')
    elif (n - sum) % 2 == 0:
        if sum % 4 == 1:
            l.append('WIN')
        else:
            l.append('LOSE')
    else:
        l.append('WIN')
for i in l:
    print(i)