結果

問題 No.1955 Not Prime
ユーザー siganaisiganai
提出日時 2022-05-26 21:27:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 503 ms / 2,000 ms
コード長 4,647 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 263,256 KB
最終ジャッジ日時 2024-09-20 15:06:50
合計ジャッジ時間 7,541 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
93,440 KB
testcase_01 AC 103 ms
92,928 KB
testcase_02 AC 99 ms
92,672 KB
testcase_03 AC 98 ms
93,056 KB
testcase_04 AC 98 ms
92,800 KB
testcase_05 AC 99 ms
92,800 KB
testcase_06 AC 211 ms
98,816 KB
testcase_07 AC 430 ms
106,068 KB
testcase_08 AC 406 ms
101,232 KB
testcase_09 AC 351 ms
98,548 KB
testcase_10 AC 128 ms
98,688 KB
testcase_11 AC 438 ms
263,256 KB
testcase_12 AC 277 ms
98,688 KB
testcase_13 AC 417 ms
98,976 KB
testcase_14 AC 298 ms
98,688 KB
testcase_15 AC 278 ms
98,924 KB
testcase_16 AC 374 ms
98,560 KB
testcase_17 AC 503 ms
133,956 KB
testcase_18 AC 443 ms
99,744 KB
testcase_19 AC 357 ms
105,664 KB
testcase_20 AC 99 ms
94,080 KB
testcase_21 AC 280 ms
98,472 KB
testcase_22 AC 101 ms
92,928 KB
testcase_23 AC 100 ms
93,056 KB
testcase_24 AC 105 ms
93,312 KB
testcase_25 AC 102 ms
93,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env PyPy3

from collections import Counter, defaultdict, deque
import itertools
import re
import math
from functools import reduce
import operator
import bisect
from heapq import *
import functools
import typing
mod=998244353

import sys
input = sys.stdin.readline
import typing
class SCC:
 
    def __init__(self, N):
        self.N = N
        self.edges = []
 
    def csr(self):
        start = [0]*(self.N+1)
        elist = [0]*len(self.edges)
        for v, w in self.edges:
            start[v+1] += 1
        for i in range(1, self.N+1):
            start[i] += start[i-1]
        counter = start.copy()
        for v, w in self.edges:
            elist[counter[v]] = w
            counter[v] += 1
        self.start = start
        self.elist = elist
 
    def add_edge(self, v, w):
        self.edges.append((v, w))
 
    def scc_ids(self):
        self.csr()
        N = self.N
        now_ord = group_num = 0
        visited = []
        low = [0]*N
        order = [-1]*N
        ids = [0]*N
        parent = [-1]*N
        stack = []
        for i in range(N):
            if order[i] == -1:
                stack.append(~i)
                stack.append(i)
                while stack:
                    v = stack.pop()
                    if v >= 0:
                        if order[v] == -1:
                            low[v] = order[v] = now_ord
                            now_ord += 1
                            visited.append(v)
                            for i in range(self.start[v], self.start[v+1]):
                                to = self.elist[i]
                                if order[to] == -1:
                                    stack.append(~to)
                                    stack.append(to)
                                    parent[to] = v
                                else:
                                    low[v] = min(low[v], order[to])
                    else:
                        v = ~v
                        if low[v] == order[v]:
                            while True:
                                u = visited.pop()
                                order[u] = N
                                ids[u] = group_num
                                if u == v:
                                    break
                            group_num += 1
                        if parent[v] != -1:
                            low[parent[v]] = min(low[parent[v]], low[v])
        for i, x in enumerate(ids):
            ids[i] = group_num - 1 - x
 
        return group_num, ids
 
    def scc(self):
        group_num, ids = self.scc_ids()
        groups = [[] for _ in range(group_num)]
        for i, x in enumerate(ids):
            groups[x].append(i)
        return groups
class TwoSAT:
 
    def __init__(self, N):
        self.N = N
        self._answer = [0] * N
        self.scc = SCC(2 * N)
 
    def add_clause(self, i, f, j, g): #f,g = 0/1 (True/False)
        # 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):
        ids = self.scc.scc_ids()[1]
        for i in range(self.N):
            if ids[2 * i] == ids[2 * i + 1]:
                #self.answer.clear()
                return False
            self._answer[i] = ids[2 * i] < ids[2 * i + 1]
        return True

    def answer(self) -> typing.List[bool]:
        return self._answer
def sieve(limit):
        if limit < 2:
            return []
    
        # primep[n]==Trueのとき、(2 * n) + 1 が素数とする
        primep = [1] * ((limit - 1) // 2 + 1)
    
        primep[0] = 0
        for num in range(1, int((limit ** 0.5) - 1) // 2 + 1):
            if primep[num]:
                p = 2 * num + 1 
                for i in range(num + p, len(primep), p):
                    primep[i] = 0
    
        return [2] + [2 * p + 1 for p in range(len(primep)) if primep[p]]
primes = set(sieve(1000000))
n = int(input())
ab = [list(map(int,input().split())) for _ in range(n)]
twosat = TwoSAT(n)


def check(a,b):
    return a * 10 ** (len(str(b))) + b in primes

for i in range(n):
    a,b = ab[i]
    for j in range(n):
        c,d = ab[j]
        if check(a,d) or check(c,b):
            twosat.add_clause(i,1,j,1)
        if check(a,c) or check(d,b):
            twosat.add_clause(i,1,j,0)
        if check(b,d) or check(c,a):
            twosat.add_clause(i,0,j,1)
        if check(b,c) or check(d,a):
            twosat.add_clause(i,0,j,0)
if twosat.satisfiable():
    print('Yes')
else:
    print('No')
0