結果

問題 No.1538 引きこもりさんは引き算が得意。
ユーザー lam6er
提出日時 2025-04-16 00:47:33
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,355 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 82,076 KB
実行使用メモリ 471,344 KB
最終ジャッジ日時 2025-04-16 00:50:50
合計ジャッジ時間 6,379 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34 TLE * 1 -- * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import combinations
from collections import deque

n, k = map(int, input().split())
a = list(map(int, input().split()))

visited = set()
q = deque()
initial = tuple(sorted(a))
q.append(initial)
visited.add(initial)

found = False

while q:
    current = q.popleft()
    if k in current:
        found = True
        break
    m = len(current)
    if m < 2:
        continue
    # Generate all possible pairs
    for i in range(m):
        for j in range(i+1, m):
            x = current[i]
            y = current[j]
            # Generate new numbers
            new_num1 = x - y
            new_num2 = y - x
            # Create new lists
            new_list1 = list(current)
            del new_list1[j], new_list1[i]
            new_list1.append(new_num1)
            new_list1_sorted = tuple(sorted(new_list1))
            if new_list1_sorted not in visited:
                visited.add(new_list1_sorted)
                q.append(new_list1_sorted)
            # For the second possibility
            new_list2 = list(current)
            del new_list2[j], new_list2[i]
            new_list2.append(new_num2)
            new_list2_sorted = tuple(sorted(new_list2))
            if new_list2_sorted not in visited:
                visited.add(new_list2_sorted)
                q.append(new_list2_sorted)

print("Yes" if found else "No")
0