結果
問題 | No.1955 Not Prime |
ユーザー | H20 |
提出日時 | 2022-04-10 18:36:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 674 ms / 2,000 ms |
コード長 | 3,052 bytes |
コンパイル時間 | 283 ms |
コンパイル使用メモリ | 82,016 KB |
実行使用メモリ | 290,384 KB |
最終ジャッジ日時 | 2024-12-21 07:30:42 |
合計ジャッジ時間 | 8,233 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 98 ms
85,540 KB |
testcase_01 | AC | 101 ms
86,080 KB |
testcase_02 | AC | 101 ms
85,484 KB |
testcase_03 | AC | 100 ms
86,280 KB |
testcase_04 | AC | 99 ms
85,768 KB |
testcase_05 | AC | 99 ms
86,212 KB |
testcase_06 | AC | 199 ms
97,784 KB |
testcase_07 | AC | 457 ms
116,692 KB |
testcase_08 | AC | 454 ms
125,068 KB |
testcase_09 | AC | 422 ms
107,096 KB |
testcase_10 | AC | 201 ms
97,456 KB |
testcase_11 | AC | 674 ms
290,384 KB |
testcase_12 | AC | 312 ms
96,700 KB |
testcase_13 | AC | 475 ms
118,732 KB |
testcase_14 | AC | 309 ms
97,520 KB |
testcase_15 | AC | 302 ms
96,988 KB |
testcase_16 | AC | 410 ms
108,100 KB |
testcase_17 | AC | 487 ms
132,596 KB |
testcase_18 | AC | 491 ms
118,140 KB |
testcase_19 | AC | 406 ms
145,704 KB |
testcase_20 | AC | 98 ms
86,636 KB |
testcase_21 | AC | 317 ms
97,104 KB |
testcase_22 | AC | 98 ms
86,808 KB |
testcase_23 | AC | 96 ms
85,216 KB |
testcase_24 | AC | 106 ms
85,832 KB |
testcase_25 | AC | 103 ms
85,884 KB |
ソースコード
def sieve(n): is_prime = [True for _ in range(n+1)] is_prime[0] = False for i in range(2, n+1): if is_prime[i-1]: j = i * i while j <= n: is_prime[j-1] = False j += i table = [ i for i in range(1, n+1) if is_prime[i-1]] return [False]+is_prime, table def two_sat(n,clause): answer=[0]*n edges=[] N=2*n for s in clause: i,f,j,g=s edges.append((2*i+(0 if f else 1),2*j+(1 if g else 0))) edges.append((2*j+(0 if g else 1),2*i+(1 if f else 0))) M=len(edges) start=[0]*(N+1) elist=[0]*M for e in edges: start[e[0]+1]+=1 for i in range(1,N+1): start[i]+=start[i-1] counter=start[:] for e in edges: elist[counter[e[0]]]=e[1] counter[e[0]]+=1 visited=[] low=[0]*N Ord=[-1]*N ids=[0]*N NG=[0,0] def dfs(v): stack=[(v,-1,0),(v,-1,1)] while stack: v,bef,t=stack.pop() if t: if bef!=-1 and Ord[v]!=-1: low[bef]=min(low[bef],Ord[v]) stack.pop() continue low[v]=NG[0] Ord[v]=NG[0] NG[0]+=1 visited.append(v) for i in range(start[v],start[v+1]): to=elist[i] if Ord[to]==-1: stack.append((to,v,0)) stack.append((to,v,1)) else: low[v]=min(low[v],Ord[to]) else: if low[v]==Ord[v]: while(True): u=visited.pop() Ord[u]=N ids[u]=NG[1] if u==v: break NG[1]+=1 low[bef]=min(low[bef],low[v]) for i in range(N): if Ord[i]==-1: dfs(i) for i in range(N): ids[i]=NG[1]-1-ids[i] for i in range(n): if ids[2*i]==ids[2*i+1]: return None answer[i]=(ids[2*i]<ids[2*i+1]) return answer N = int(input()) T,_ = sieve(10**6) AB = [list(input().split()) for _ in range(N)] clause=[] if not 1<=N<=500: exit() for a,b in AB: if 1<=int(a)<1000 and 1<=int(b)<1000 and a==str(int(a)) and b==str(int(b)): continue exit() for i in range(N): for j in range(i,N): ia,ib,ja,jb = AB[i][0],AB[i][1],AB[j][0],AB[j][1] if T[int(ia+ib)] or T[int(ja+jb)] or T[int(ia+jb)] or T[int(ja+ib)]: clause.append((i,0,j,0)) if T[int(ia+ib)] or T[int(jb+ja)] or T[int(ia+ja)] or T[int(jb+ib)]: clause.append((i,0,j,1)) if T[int(ib+ia)] or T[int(ja+jb)] or T[int(ib+jb)] or T[int(ja+ia)]: clause.append((i,1,j,0)) if T[int(ib+ia)] or T[int(jb+ja)] or T[int(jb+ia)] or T[int(ib+ja)]: clause.append((i,1,j,1)) ans=two_sat(N,clause) if ans==None: print('No') else: print('Yes')