結果

問題 No.1955 Not Prime
ユーザー chineristACchineristAC
提出日時 2022-05-20 22:47:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 559 ms / 2,000 ms
コード長 3,804 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 82,604 KB
実行使用メモリ 258,560 KB
最終ジャッジ日時 2024-09-20 09:08:24
合計ジャッジ時間 8,782 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
76,672 KB
testcase_01 AC 93 ms
76,160 KB
testcase_02 AC 96 ms
76,288 KB
testcase_03 AC 102 ms
75,904 KB
testcase_04 AC 96 ms
76,288 KB
testcase_05 AC 98 ms
76,416 KB
testcase_06 AC 240 ms
87,456 KB
testcase_07 AC 517 ms
111,180 KB
testcase_08 AC 489 ms
105,820 KB
testcase_09 AC 444 ms
100,120 KB
testcase_10 AC 165 ms
85,504 KB
testcase_11 AC 465 ms
258,560 KB
testcase_12 AC 333 ms
91,288 KB
testcase_13 AC 508 ms
105,308 KB
testcase_14 AC 337 ms
93,852 KB
testcase_15 AC 315 ms
92,128 KB
testcase_16 AC 440 ms
102,936 KB
testcase_17 AC 559 ms
141,524 KB
testcase_18 AC 514 ms
105,140 KB
testcase_19 AC 370 ms
115,096 KB
testcase_20 AC 96 ms
77,696 KB
testcase_21 AC 316 ms
90,676 KB
testcase_22 AC 96 ms
76,544 KB
testcase_23 AC 94 ms
76,672 KB
testcase_24 AC 96 ms
76,288 KB
testcase_25 AC 95 ms
76,288 KB
権限があれば一括ダウンロードができます

ソースコード

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[n] = 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