結果

問題 No.1955 Not Prime
ユーザー chineristACchineristAC
提出日時 2022-05-20 22:44:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,805 bytes
コンパイル時間 228 ms
コンパイル使用メモリ 81,576 KB
実行使用メモリ 262,132 KB
最終ジャッジ日時 2023-10-20 13:33:26
合計ジャッジ時間 12,462 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 178 ms
84,304 KB
testcase_01 AC 177 ms
84,300 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 174 ms
84,300 KB
testcase_05 WA -
testcase_06 AC 335 ms
92,956 KB
testcase_07 AC 722 ms
176,444 KB
testcase_08 AC 677 ms
171,604 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 839 ms
256,148 KB
testcase_14 AC 422 ms
116,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 603 ms
257,672 KB
testcase_20 WA -
testcase_21 AC 596 ms
141,536 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 176 ms
84,300 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
import heapq
from itertools import permutations
from math import gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

class scc_graph:
 
    def __init__(self, N):
        self.N = N
        self.edges = []
 
    def csr(self):
        self.start = [0]*(self.N+1)
        self.elist = [0]*len(self.edges)
        for e in self.edges:
            self.start[e[0]+1] += 1
        for i in range(1, self.N+1):
            self.start[i] += self.start[i-1]
        counter = self.start[:]
        for e in self.edges:
            self.elist[counter[e[0]]] = e[1]
            counter[e[0]] += 1
 
    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 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:
                        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 two_sat:
    
    def __init__(self,N):
        self.N = N
        self.answer = [-1]*N
        self.scc = scc_graph(2*N)
 
    def add_clause(self,i,f,j,g):
        self.scc.add_edge(2*i+(f==0),2*j+(g==1))
        self.scc.add_edge(2*j+(g==0),2*i+(f==1))
    
    def satisfiable(self):
        _,ids = self.scc.scc_ids()
        for i in range(N):
            if ids[2*i] == ids[2*i+1]:
                return False
            self.answer[i] = ids[2*i] < ids[2*i+1]
        return True

M = 10**6
prime = [True] * (M+1)
prime[1] = False
for p in range(2,M+1):
    if not prime[p]:
        continue
    for n in range(2*p,M+1,p):
        prime[p] = False

def check(x,y):
    n = int(str(x) + str(y))
    return prime[n]


N = int(input())
AB = [tuple(mi()) for i in range(N)]

G = two_sat(N)
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):
            G.add_clause(i,1,j,1)
        if check(a,c) or check(d,b):
            G.add_clause(i,1,j,0)
        if check(b,d) or check(c,a):
            G.add_clause(i,0,j,1)
        if check(b,c) or check(d,a):
            G.add_clause(i,0,j,0)

if G.satisfiable():
    print("Yes")
else:
    print("No")

0