목차

동전 게임

ps
링크acmicpc.net/…
출처BOJ
문제 번호12941
문제명동전 게임
레벨플래티넘 2
분류

게임 이론

시간복잡도O(n)
인풋사이즈n<=100,000
사용한 언어Python 3.11
제출기록43088KB / 132ms
최고기록124ms
해결날짜2023/07/06

풀이

코드

"""Solution code for "BOJ 12941. 동전 게임".

- Problem link: https://www.acmicpc.net/problem/12941
- Solution link: http://www.teferi.net/ps/problems/boj/12941

Tags: [game theory]
"""

import functools
import operator


def grundy_even_k(n):
    if n <= 2:
        return n
    return (n + 1) % 2


def grundy_odd_k(n):
    if n <= 3:
        return n % 2
    if n % 2 == 1:
        return 0
    power_2 = (n & (-n)).bit_length() - 1
    if n >> power_2 == 3:
        return power_2 % 2 + 1
    return (power_2 + 1) % 2 + 1


def main():
    N, K = [int(x) for x in input().split()]  # pylint: disable=unused-variable
    a = [int(x) for x in input().split()]

    compute_grundy = grundy_odd_k if K % 2 == 1 else grundy_even_k
    grundy = functools.reduce(operator.xor, (compute_grundy(x) for x in a))

    print('Kevin' if grundy > 0 else 'Nicky')


if __name__ == '__main__':
    main()