結果

問題 No.1955 Not Prime
ユーザー titiatitia
提出日時 2022-05-22 18:13:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,361 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 81,756 KB
実行使用メモリ 209,708 KB
最終ジャッジ日時 2023-10-20 17:11:56
合計ジャッジ時間 6,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 80 ms
85,472 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 80 ms
85,472 KB
testcase_06 AC 135 ms
99,596 KB
testcase_07 AC 345 ms
153,424 KB
testcase_08 AC 302 ms
135,088 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 432 ms
209,476 KB
testcase_14 AC 186 ms
100,124 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 463 ms
207,696 KB
testcase_20 AC 81 ms
87,792 KB
testcase_21 AC 260 ms
110,464 KB
testcase_22 AC 78 ms
85,472 KB
testcase_23 WA -
testcase_24 AC 79 ms
85,472 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 not(int(str(a)+str(c)) in Primes):
            # ¬(x ⋀ y)
            EDGE[i].append(j+N)
            EDGE[j].append(i+N)

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

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

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


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

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

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

            EDGE_INV[j].append(i+N)
            EDGE_INV[i].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