結果

問題 No.1955 Not Prime
ユーザー titiatitia
提出日時 2022-05-22 18:10:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,341 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 12,248 KB
実行使用メモリ 123,864 KB
最終ジャッジ日時 2023-10-20 17:11:44
合計ジャッジ時間 22,944 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 498 ms
52,648 KB
testcase_01 AC 482 ms
52,648 KB
testcase_02 AC 487 ms
52,648 KB
testcase_03 WA -
testcase_04 AC 477 ms
52,648 KB
testcase_05 WA -
testcase_06 AC 544 ms
52,660 KB
testcase_07 AC 990 ms
53,916 KB
testcase_08 AC 905 ms
53,532 KB
testcase_09 AC 1,242 ms
53,388 KB
testcase_10 AC 1,200 ms
52,712 KB
testcase_11 TLE -
testcase_12 WA -
testcase_13 AC 1,296 ms
54,012 KB
testcase_14 AC 645 ms
52,672 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 1,318 ms
55,864 KB
testcase_20 AC 492 ms
52,652 KB
testcase_21 AC 746 ms
52,680 KB
testcase_22 WA -
testcase_23 AC 484 ms
52,652 KB
testcase_24 AC 497 ms
52,652 KB
testcase_25 AC 490 ms
52,652 KB
権限があれば一括ダウンロードができます

ソースコード

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

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

        if 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 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 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