結果

問題 No.1955 Not Prime
ユーザー terasaterasa
提出日時 2022-05-22 21:50:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,407 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 81,820 KB
実行使用メモリ 101,476 KB
最終ジャッジ日時 2023-10-20 17:20:32
合計ジャッジ時間 5,990 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
94,768 KB
testcase_01 AC 118 ms
94,768 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 115 ms
94,768 KB
testcase_05 AC 113 ms
94,768 KB
testcase_06 AC 142 ms
101,404 KB
testcase_07 AC 199 ms
101,140 KB
testcase_08 AC 195 ms
101,140 KB
testcase_09 WA -
testcase_10 AC 145 ms
101,476 KB
testcase_11 AC 615 ms
100,932 KB
testcase_12 WA -
testcase_13 AC 216 ms
101,140 KB
testcase_14 AC 152 ms
101,136 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 233 ms
101,404 KB
testcase_20 AC 120 ms
94,768 KB
testcase_21 AC 165 ms
101,140 KB
testcase_22 WA -
testcase_23 AC 119 ms
94,768 KB
testcase_24 AC 117 ms
94,768 KB
testcase_25 AC 120 ms
94,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict, Counter
import string
import random

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


class PrimeTable:
    def __init__(self, N):
        self.is_prime = [True] * (N + 1)

        self.is_prime[0] = False
        self.is_prime[1] = False
        for i in range(2, N + 1):
            if i * i > N:
                break
            if self.is_prime[i] is False:
                continue
            for j in range(2, N + 1):
                if i * j > N:
                    break
                self.is_prime[i * j] = False

        self.primes = [n for n in range(2, N + 1) if self.is_prime[n]]

    def is_prime(self, n):
        return self.is_prime[n]


class UnionFind:
    def __init__(self, N):
        self.N = N
        self.par = [-1] * N

    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return False
        if x > y:
            x, y = y, x

        self.par[x] += self.par[y]
        self.par[y] = x
        return True

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.par[self.find(x)]

    def roots(self):
        return [i for i in range(self.N) if self.par[i] < 0]

    def members(self, x):
        p = self.find(x)
        return [i for i in range(self.N) if self.find(i) == p]


N = int(input())
nums = []
for i in range(N):
    a, b = map(int, input().split())
    nums.append(a)
    nums.append(b)

primes = set(PrimeTable(10 ** 6).primes)
uf = UnionFind(4 * N)
for i in range(N):
    u, v = 2 * i, 2 * i + 1
    ua, ub = 2 * u, 2 * u + 1
    va, vb = 2 * v, 2 * v + 1
    uf.unite(ua, vb)
    uf.unite(ub, va)

for u in range(2 * N):
    for v in range(u + 1, 2 * N):
        s, t = nums[u], nums[v]
        n = int(str(s) + str(t))
        if n in primes:
            ua, ub = 2 * u, 2 * u + 1
            va, vb = 2 * v, 2 * v + 1
            uf.unite(ua, vb)
            uf.unite(ub, va)

for i in range(2 * N):
    va, vb = 2 * i, 2 * i + 1
    if uf.same(va, vb):
        print('No')
        exit()
print('Yes')
0