結果
| 問題 |
No.261 ぐるぐるぐるぐる!あみだくじ!
|
| コンテスト | |
| ユーザー |
vwxyz
|
| 提出日時 | 2021-07-31 19:01:17 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 157 ms / 5,000 ms |
| コード長 | 2,114 bytes |
| コンパイル時間 | 156 ms |
| コンパイル使用メモリ | 81,920 KB |
| 実行使用メモリ | 90,624 KB |
| 最終ジャッジ日時 | 2024-09-16 09:40:27 |
| 合計ジャッジ時間 | 7,632 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
heap.append(item)
heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
if heap and item < heap[0]:
item, heap[0] = heap[0], item
heapq._siftup_max(heap, 0)
return item
from math import gcd as GCD, modf
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
def LCM(n,m):
if n or m:
return abs(n)*abs(m)//math.gcd(n,m)
return 0
def CRT(lst_r,lst_m):
r,m=lst_r[0],lst_m[0]
for r0,m0 in zip(lst_r[1:],lst_m[1:]):
if (r0,m0)==(-1,0):
r,m=-1,0
break
r0%=m0
g=math.gcd(m,m0)
l=LCM(m,m0)
if r%g!=r0%g:
r,m=-1,0
break
r,m=(r0+m0*(((r-r0)//g)*Extended_Euclid(m0//g,m//g)[0]%(m//g)))%l,l
return r,m
def Extended_Euclid(n,m):
stack=[]
while m:
stack.append((n,m))
n,m=m,n%m
if n>=0:
x,y=1,0
else:
x,y=-1,0
for i in range(len(stack)-1,-1,-1):
n,m=stack[i]
x,y=y,x-(n//m)*y
return x,y
N=int(readline())
K=int(readline())
lst=[i for i in range(N)]
for _ in range(K):
X,Y=map(int,readline().split())
X-=1;Y-=1
lst[X],lst[Y]=lst[Y],lst[X]
dct=[{} for i in range(N)]
cycle=[None]*N
for i in range(N):
ii=i
cnt=0
while not ii in dct[i]:
dct[i][ii]=cnt
ii=lst[ii]
cnt+=1
cycle[i]=cnt
Q=int(readline())
for _ in range(Q):
A=list(map(int,readline().split()))
A=[a-1 for a in A]
lst_r=[]
lst_m=[]
for i in range(N):
if A[i] in dct[i]:
lst_r.append(dct[i][A[i]])
lst_m.append(cycle[i])
else:
ans=-1
break
else:
ans,m=CRT(lst_r,lst_m)
if ans==0:
ans=m
print(ans)
vwxyz