結果

問題 No.1955 Not Prime
ユーザー terasaterasa
提出日時 2022-09-29 00:11:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,065 bytes
コンパイル時間 516 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 319,564 KB
最終ジャッジ日時 2024-06-02 01:44:32
合計ジャッジ時間 6,411 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
82,944 KB
testcase_01 AC 90 ms
82,944 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 91 ms
82,560 KB
testcase_05 AC 93 ms
83,200 KB
testcase_06 AC 149 ms
90,892 KB
testcase_07 AC 213 ms
96,128 KB
testcase_08 AC 203 ms
92,520 KB
testcase_09 AC 233 ms
91,392 KB
testcase_10 AC 196 ms
91,264 KB
testcase_11 AC 855 ms
319,564 KB
testcase_12 AC 173 ms
91,296 KB
testcase_13 AC 252 ms
94,592 KB
testcase_14 AC 168 ms
91,364 KB
testcase_15 AC 165 ms
91,264 KB
testcase_16 AC 202 ms
91,348 KB
testcase_17 AC 276 ms
111,872 KB
testcase_18 AC 245 ms
91,648 KB
testcase_19 AC 258 ms
96,640 KB
testcase_20 AC 91 ms
83,968 KB
testcase_21 AC 170 ms
91,008 KB
testcase_22 WA -
testcase_23 AC 91 ms
83,328 KB
testcase_24 AC 91 ms
82,816 KB
testcase_25 AC 90 ms
83,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
from functools import lru_cache
from typing import List


# for AtCoder Easy test
if __file__ == 'prog.py':
    pass
else:
    sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')

input = sys.stdin.readline


def readints(): return map(int, input().split())
def readlist(): return list(readints())
def readstr(): return input()[:-1]


class SCC:
    def __init__(self, N, E):
        self.N = N
        self.E = E
        self.I = [[] for _ in range(N)]
        for s in range(N):
            for t in E[s]:
                self.I[t].append(s)

        self.V = []
        self.cid = [None] * N
        self.c_num = 0
        self.traverse()
        self.traverse2()

        self.C = [[] for _ in range(self.c_num)]
        for i in range(self.N):
            c = self.cid[i]
            self.C[c].append(i)

    def traverse(self):
        flag = [False] * self.N
        for i in range(self.N):
            if flag[i] is False:
                self.dfs(i, flag)
        self.V.reverse()

    def traverse2(self):
        flag = [False] * self.N
        cid = 0
        for v in self.V:
            if flag[v] is False:
                self.dfs2(v, flag)
                self.c_num += 1

    def dfs(self, v, flag):
        stack = [~v, v]
        while stack:
            v = stack.pop()
            if v < 0:
                self.V.append(~v)
                continue

            if flag[v] is True:
                stack.pop()
                continue
            flag[v] = True
            for dest in self.E[v]:
                if flag[dest] is False:
                    stack.append(~dest)
                    stack.append(dest)

    def dfs2(self, v, flag):
        stack = [v]
        while stack:
            v = stack.pop()
            if flag[v] is True:
                continue
            flag[v] = True
            self.cid[v] = self.c_num
            for dest in self.I[v]:
                if flag[dest] is False:
                    stack.append(dest)


class TwoSat:
    def __init__(self, N: int):
        self.N = N
        self.E = [[] for _ in range(2 * N)]

    def vid(self, n):
        return 2 * (~n) + 1 if n < 0 else 2 * n

    def add_clause(self, s: int, t: int) -> None:
        def oppose(id):
            return id ^ 1

        sid, tid = self.vid(s), self.vid(t)
        # ~s -> t
        self.E[oppose(sid)].append(tid)
        # ~t -> s
        self.E[oppose(tid)].append(sid)

    def if_then(self, s: int, t: int) -> None:
        sid, tid = self.vid(s), self.vid(t)
        self.E[sid].append(tid)

    def solve(self) -> List[bool]:
        cid = SCC(2 * self.N, self.E).cid
        for i in range(self.N):
            if cid[2 * i] == cid[2 * i + 1]:
                return None
        # if cid[2 * a] > cid[2 * a + 1], possibly ~a -> a
        ans = [cid[2 * i] > cid[2 * i + 1] for i in range(self.N)]
        return ans


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]]


N = int(input())
is_prime = PrimeTable(10 ** 6).is_prime
A = []
for _ in range(N):
    a, b = readints()
    A.append(a)
    A.append(b)

solver = TwoSat(2 * N)
for i in range(N):
    solver.add_clause(2 * i, 2 * i + 1)
    solver.add_clause(~(2 * i), ~(2 * i + 1))
for i in range(2 * N):
    for j in range(2 * N):
        if i ^ j <= 1:
            continue
        n = int(str(A[i]) + str(A[j]))
        if is_prime[n]:
            solver.if_then(i, j)
            solver.if_then(~j, ~i)
print('Yes' if solver.solve() is not None else 'No')
0