結果

問題 No.1955 Not Prime
ユーザー titiatitia
提出日時 2022-05-22 18:29:18
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,341 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 81,808 KB
実行使用メモリ 202,044 KB
最終ジャッジ日時 2023-10-20 17:12:55
合計ジャッジ時間 4,742 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 76 ms
85,496 KB
testcase_02 AC 78 ms
85,496 KB
testcase_03 AC 76 ms
85,496 KB
testcase_04 AC 77 ms
85,496 KB
testcase_05 AC 81 ms
85,496 KB
testcase_06 AC 109 ms
99,356 KB
testcase_07 AC 193 ms
99,412 KB
testcase_08 AC 187 ms
99,416 KB
testcase_09 WA -
testcase_10 AC 113 ms
96,508 KB
testcase_11 AC 309 ms
202,044 KB
testcase_12 WA -
testcase_13 AC 202 ms
99,416 KB
testcase_14 AC 145 ms
99,624 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 208 ms
99,416 KB
testcase_20 AC 74 ms
87,548 KB
testcase_21 AC 147 ms
99,624 KB
testcase_22 AC 75 ms
85,500 KB
testcase_23 AC 71 ms
85,500 KB
testcase_24 AC 72 ms
85,500 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
import math 
N=int(input())

AB=[tuple(map(int,input().split())) for i in range(N)]

x=10**6+10

L=math.floor(math.sqrt(x)) # 平方根を求める

Primelist=[i for i in range(x+1)]
Primelist[1]=0 # 1は素数でないので0にする.
 
for i in Primelist:
    if i>L:
        break
    if i==0:
        continue
    for j in range(2*i,x+1,i):
        Primelist[j]=0

Primes=[Primelist[j] for j in range(x+1) if Primelist[j]!=0]
Primes=set(Primes)

EDGE=[[] for i in range(2*N)]
EDGE_INV=[[] for i in range(2*N)]

# 0...N-1: x
# N...2N-1: ¬x

for i in range(N):
    a,b=AB[i]

    for j in range(N):
        c,d=AB[j]

        if int(str(a)+str(c)) in Primes:
            # ¬(x ⋀ y)
            EDGE[i].append(j)
            EDGE[j].append(i)

            EDGE_INV[j].append(i)
            EDGE_INV[i].append(j)

        if int(str(a)+str(d)) in Primes:
            # ¬(x ⋀ ¬y)
            EDGE[i].append(j+N)
            EDGE[j+N].append(i)

            EDGE_INV[j+N].append(i)
            EDGE_INV[i].append(j+N)


        if int(str(b)+str(c)) in Primes:
            # ¬(¬x ⋀ y)
            EDGE[i+N].append(j)
            EDGE[j].append(i+N)

            EDGE_INV[j].append(i+N)
            EDGE_INV[i+N].append(j)

        if int(str(b)+str(d)) in Primes:
            # ¬(¬x ⋀ ¬y)
            EDGE[i+N].append(j+N)
            EDGE[j+N].append(i+N)

            EDGE_INV[j+N].append(i+N)
            EDGE_INV[i+N].append(j+N)
            
            
QUE = list(range(2*N))
check=[0]*(2*N)
TOP_SORT=[]
 
def dfs(x):
    if check[x]==1:
        return
    check[x]=1
    
    for to in EDGE[x]:
        if check[to]==0:
            dfs(to)
 
    TOP_SORT.append(x) # 全ての点からDFSを行い, 帰りがけに点を答えに入れる
    check[x]=1
 
while QUE:
    x=QUE.pop()
    dfs(x)
 
USE=[0]*(2*N)
SCC=[]
 
def dfs2(x):
    Q=[x]
    USE[x]=1
    ANS=[]
 
    while Q:
        x=Q.pop()
        ANS.append(x)
        for to in EDGE_INV[x]:
            if USE[to]==0:
                USE[to]=1
                Q.append(to)
    return ANS
 
for x in TOP_SORT[::-1]:
    if USE[x]==0:
        SCC.append(dfs2(x))


for scc in SCC:
    SET=set(scc)

    for x in scc:
        if x+N in SET or x-N in SET:
            print("No")
            exit()

print("Yes")
0