結果
| 問題 |
No.1955 Not Prime
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2022-05-23 02:00:41 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 507 ms / 2,000 ms |
| コード長 | 2,341 bytes |
| コンパイル時間 | 196 ms |
| コンパイル使用メモリ | 82,708 KB |
| 実行使用メモリ | 204,912 KB |
| 最終ジャッジ日時 | 2024-09-20 13:00:33 |
| 合計ジャッジ時間 | 4,958 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
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+N].append(i+N)
EDGE_INV[j].append(i)
EDGE_INV[i+N].append(j+N)
if int(str(a)+str(d)) 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(b)+str(c)) 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)
if int(str(b)+str(d)) 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)
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")
titia