結果

問題 No.1955 Not Prime
ユーザー 👑 H20H20
提出日時 2022-03-23 00:33:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 629 ms / 2,000 ms
コード長 5,668 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 121,972 KB
最終ジャッジ日時 2023-08-23 07:00:36
合計ジャッジ時間 10,883 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 229 ms
105,876 KB
testcase_01 AC 228 ms
105,792 KB
testcase_02 AC 228 ms
105,804 KB
testcase_03 AC 224 ms
106,020 KB
testcase_04 AC 229 ms
105,736 KB
testcase_05 AC 234 ms
105,852 KB
testcase_06 AC 300 ms
107,560 KB
testcase_07 AC 232 ms
105,764 KB
testcase_08 AC 231 ms
105,888 KB
testcase_09 AC 563 ms
108,524 KB
testcase_10 AC 302 ms
107,220 KB
testcase_11 AC 236 ms
105,820 KB
testcase_12 AC 464 ms
107,660 KB
testcase_13 AC 622 ms
113,644 KB
testcase_14 AC 480 ms
107,852 KB
testcase_15 AC 479 ms
108,228 KB
testcase_16 AC 567 ms
110,268 KB
testcase_17 AC 629 ms
116,988 KB
testcase_18 AC 614 ms
116,100 KB
testcase_19 AC 484 ms
121,972 KB
testcase_20 AC 227 ms
105,920 KB
testcase_21 AC 452 ms
107,112 KB
testcase_22 AC 227 ms
105,744 KB
testcase_23 AC 231 ms
105,852 KB
testcase_24 AC 235 ms
106,052 KB
testcase_25 AC 232 ms
105,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import types

_atcoder_code = """
# Python port of AtCoder Library.

__version__ = '0.0.1'
"""

atcoder = types.ModuleType('atcoder')
exec(_atcoder_code, atcoder.__dict__)

_atcoder__scc_code = """
import copy
import sys
import typing


class CSR:
    def __init__(
            self, n: int, edges: typing.List[typing.Tuple[int, int]]) -> None:
        self.start = [0] * (n + 1)
        self.elist = [0] * len(edges)

        for e in edges:
            self.start[e[0] + 1] += 1

        for i in range(1, n + 1):
            self.start[i] += self.start[i - 1]

        counter = copy.deepcopy(self.start)
        for e in edges:
            self.elist[counter[e[0]]] = e[1]
            counter[e[0]] += 1


class SCCGraph:
    '''
    Reference:
    R. Tarjan,
    Depth-First Search and Linear Graph Algorithms
    '''

    def __init__(self, n: int) -> None:
        self._n = n
        self._edges = []

    def num_vertices(self) -> int:
        return self._n

    def add_edge(self, from_vertex: int, to_vertex: int) -> None:
        self._edges.append((from_vertex, to_vertex))

    def scc_ids(self) -> typing.Tuple[int, typing.List[int]]:
        g = CSR(self._n, self._edges)
        now_ord = 0
        group_num = 0
        visited = []
        low = [0] * self._n
        order = [-1] * self._n
        ids = [0] * self._n

        sys.setrecursionlimit(max(self._n + 1000, sys.getrecursionlimit()))

        def dfs(v: int) -> None:
            nonlocal now_ord
            nonlocal group_num
            nonlocal visited
            nonlocal low
            nonlocal order
            nonlocal ids

            low[v] = now_ord
            order[v] = now_ord
            now_ord += 1
            visited.append(v)
            for i in range(g.start[v], g.start[v + 1]):
                to = g.elist[i]
                if order[to] == -1:
                    dfs(to)
                    low[v] = min(low[v], low[to])
                else:
                    low[v] = min(low[v], order[to])

            if low[v] == order[v]:
                while True:
                    u = visited[-1]
                    visited.pop()
                    order[u] = self._n
                    ids[u] = group_num
                    if u == v:
                        break
                group_num += 1

        for i in range(self._n):
            if order[i] == -1:
                dfs(i)

        for i in range(self._n):
            ids[i] = group_num - 1 - ids[i]

        return group_num, ids

    def scc(self) -> typing.List[typing.List[int]]:
        ids = self.scc_ids()
        group_num = ids[0]
        counts = [0] * group_num
        for x in ids[1]:
            counts[x] += 1
        groups = [[] for _ in range(group_num)]
        for i in range(self._n):
            groups[ids[1][i]].append(i)

        return groups
"""

atcoder._scc = types.ModuleType('atcoder._scc')
exec(_atcoder__scc_code, atcoder._scc.__dict__)


_atcoder_twosat_code = """
import typing

# import atcoder._scc


class TwoSAT:
    '''
    Reference:
    B. Aspvall, M. Plass, and R. Tarjan,
    A Linear-Time Algorithm for Testing the Truth of Certain Quantified Boolean
    Formulas
    '''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self._answer = [False] * n
        self._scc = atcoder._scc.SCCGraph(2 * n)

    def add_clause(self, i: int, f: bool, j: int, g: bool) -> None:
        assert 0 <= i < self._n
        assert 0 <= j < self._n

        self._scc.add_edge(2 * i + (0 if f else 1), 2 * j + (1 if g else 0))
        self._scc.add_edge(2 * j + (0 if g else 1), 2 * i + (1 if f else 0))

    def satisfiable(self) -> bool:
        scc_id = self._scc.scc_ids()[1]
        for i in range(self._n):
            if scc_id[2 * i] == scc_id[2 * i + 1]:
                return False
            self._answer[i] = scc_id[2 * i] < scc_id[2 * i + 1]
        return True

    def answer(self) -> typing.List[bool]:
        return self._answer
"""

atcoder.twosat = types.ModuleType('atcoder.twosat')
atcoder.twosat.__dict__['atcoder'] = atcoder
atcoder.twosat.__dict__['atcoder._scc'] = atcoder._scc
exec(_atcoder_twosat_code, atcoder.twosat.__dict__)
TwoSAT = atcoder.twosat.TwoSAT

# https://atcoder.jp/contests/practice2/tasks/practice2_h

import sys

# from atcoder.twosat import TwoSAT


def sieve(n):
    is_prime = [True for _ in range(n+1)]
    is_prime[0] = False

    for i in range(2, n+1):
        if is_prime[i-1]:
            j = i * i
            while j <= n:
                is_prime[j-1] = False
                j += i
    table = [ i for i in range(1, n+1) if is_prime[i-1]]
    return [False]+is_prime, table


def main() -> None:
    N = int(input())
    T,_ =  sieve(10**6)
    AB = [list(input().split()) for _ in range(N)]
    two_sat = TwoSAT(N)
    for a,b in AB:
        if T[int(a+b)] and T[int(b+a)]:
            print('No')
            return

    for i in range(N):
        for j in range(i + 1, N):
            ia,ib,ja,jb = AB[i][0],AB[i][1],AB[j][0],AB[j][1]
            if T[int(ia+ib)] or T[int(ja+jb)] or T[int(ia+jb)] or T[int(ja+ib)]:
                two_sat.add_clause(i, 0, j, 0)
            if T[int(ia+ib)] or T[int(jb+ja)] or T[int(ia+ja)] or T[int(jb+ib)]:
                two_sat.add_clause(i, 0, j, 1)
            if T[int(ib+ia)] or T[int(ja+jb)] or T[int(ib+jb)] or T[int(ja+ia)]:
                two_sat.add_clause(i, 1, j, 0)
            if T[int(ib+ia)] or T[int(jb+ja)] or T[int(jb+ia)] or T[int(ib+ja)]:
                two_sat.add_clause(i, 1, j, 1)

    if not two_sat.satisfiable():
        print("No")
    else:
        print("Yes")






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