結果

問題 No.3024 全単射的
ユーザー gew1fw
提出日時 2025-06-12 20:51:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,044 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 57,632 KB
最終ジャッジ日時 2025-06-12 20:56:09
合計ジャッジ時間 8,839 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 10 TLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import lru_cache

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    A = list(map(int, input[1:N+1]))
    
    def compute_possible_results(numbers):
        @lru_cache(maxsize=None)
        def helper(nums_tuple):
            if len(nums_tuple) == 1:
                return {nums_tuple[0]}
            results = set()
            nums = list(nums_tuple)
            n = len(nums)
            for i in range(n):
                for j in range(n):
                    if i == j:
                        continue
                    a = nums[i]
                    b = nums[j]
                    remaining = tuple(sorted([nums[k] for k in range(n) if k != i and k != j] + [a + b]))
                    results.update(helper(remaining))
                    remaining = tuple(sorted([nums[k] for k in range(n) if k != i and k != j] + [a - b]))
                    results.update(helper(remaining))
                    remaining = tuple(sorted([nums[k] for k in range(n) if k != i and k != j] + [a * b]))
                    results.update(helper(remaining))
                    if b != 0 and a % b == 0:
                        div = a // b
                        remaining = tuple(sorted([nums[k] for k in range(n) if k != i and k != j] + [div]))
                        results.update(helper(remaining))
            return results
        
        sorted_numbers = tuple(sorted(numbers))
        return helper(sorted_numbers)
    
    found = False
    for mask in range(1, (1 << N) - 1):
        group_a = []
        group_b = []
        for i in range(N):
            if (mask >> i) & 1:
                group_a.append(A[i])
            else:
                group_b.append(A[i])
        if not group_a or not group_b:
            continue
        res_a = compute_possible_results(group_a)
        res_b = compute_possible_results(group_b)
        if res_a & res_b:
            found = True
            break
    
    print("YES" if found else "NO")

if __name__ == "__main__":
    main()
0