| 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()