목차

Kruskal

ps
링크acmicpc.net/…
출처BOJ
문제 번호27852
문제명Kruskal
레벨플래티넘 2
분류

게임이론

시간복잡도O(nlogm)
인풋사이즈n<=200, m<=5,647,483,922
사용한 언어Python 3.11
제출기록56376KB / 1840ms
최고기록1840ms
해결날짜2024/01/26

풀이

코드

"""Solution code for "BOJ 27852. Kruskal".

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

Tags: [game theory] [primality test]
"""

import sys
from teflib import numtheory


def main():
    T = int(sys.stdin.readline())
    for _ in range(T):
        N, K = [int(x) for x in sys.stdin.readline().split()]
        a = [int(sys.stdin.readline()) for _ in range(N)]

        tot_grundy_num = 0
        for a_i in a:
            if a_i == 1:
                grundy_num = 1
            else:
                if N > 1 and numtheory.is_prime(a_i):
                    print('YES')
                    break
                closest_smaller_prime = next(
                    x for x in range(a_i - 1, -1, -1) if numtheory.is_prime(x)
                )
                diff = a_i - closest_smaller_prime
                if diff <= K:
                    print('YES')
                    break
                grundy_num = diff % (K + 1)
            tot_grundy_num ^= grundy_num
        else:
            print('YES' if tot_grundy_num > 0 else 'NO')


if __name__ == '__main__':
    main()