結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー 👑 rin204rin204
提出日時 2022-11-03 16:57:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 3,961 bytes
コンパイル時間 248 ms
コンパイル使用メモリ 81,776 KB
実行使用メモリ 63,488 KB
最終ジャッジ日時 2024-07-18 01:37:36
合計ジャッジ時間 3,468 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,736 KB
testcase_01 AC 43 ms
52,992 KB
testcase_02 AC 39 ms
53,120 KB
testcase_03 AC 43 ms
55,168 KB
testcase_04 AC 55 ms
62,848 KB
testcase_05 AC 56 ms
62,336 KB
testcase_06 AC 41 ms
54,492 KB
testcase_07 AC 43 ms
55,040 KB
testcase_08 AC 54 ms
62,720 KB
testcase_09 AC 42 ms
54,016 KB
testcase_10 AC 49 ms
56,576 KB
testcase_11 AC 49 ms
56,192 KB
testcase_12 AC 50 ms
55,424 KB
testcase_13 AC 49 ms
56,192 KB
testcase_14 AC 49 ms
56,832 KB
testcase_15 AC 51 ms
57,600 KB
testcase_16 AC 56 ms
63,104 KB
testcase_17 AC 49 ms
56,756 KB
testcase_18 AC 51 ms
56,960 KB
testcase_19 AC 56 ms
62,720 KB
testcase_20 AC 53 ms
57,856 KB
testcase_21 AC 50 ms
56,704 KB
testcase_22 AC 50 ms
56,704 KB
testcase_23 AC 49 ms
56,320 KB
testcase_24 AC 51 ms
57,728 KB
testcase_25 AC 50 ms
56,320 KB
testcase_26 AC 57 ms
63,488 KB
testcase_27 AC 48 ms
56,064 KB
testcase_28 AC 56 ms
63,360 KB
testcase_29 AC 56 ms
63,432 KB
testcase_30 AC 57 ms
63,488 KB
testcase_31 AC 52 ms
56,320 KB
testcase_32 AC 58 ms
62,976 KB
testcase_33 AC 55 ms
62,592 KB
testcase_34 AC 57 ms
63,232 KB
testcase_35 AC 50 ms
57,216 KB
testcase_36 AC 56 ms
63,488 KB
testcase_37 AC 43 ms
54,528 KB
testcase_38 AC 43 ms
54,400 KB
testcase_39 AC 44 ms
54,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def modinv(a, MOD):
    b = MOD
    u = 1
    v = 0
    while b:
        t = a // b
        a -= t * b
        u -= t * v
        a, b = b, a
        u, v = v, u
    u %= MOD
    return u

def Garner(M, R):
    m_prod = M[0]
    C = R[0]
    for m, r in zip(M[1:], R[1:]):
        t = (r - C) * modinv(m_prod, m) % m
        C += t * m_prod
        m_prod *= m

    if C == 0:
        C = m_prod
    return C

from math import gcd

def isprime(n):
    if n <= 1:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False
    
    A = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
    s = 0
    d = n - 1
    while d % 2 == 0:
        s += 1
        d >>= 1
    
    for a in A:
        if a % n == 0:
            return True
        x = pow(a, d, n)
        if x != 1:
            for t in range(s):
                if x == n - 1:
                    break
                x = x * x % n
            else:
                return False
    return True
        
def pollard(n):
    if n % 2 == 0:
        return 2
    if isprime(n):
        return n
    
    f = lambda x:(x * x + 1) % n
    
    step = 0
    while 1:
        step += 1
        x = step
        y = f(x)
        while 1:
            p = gcd(y - x + n, n)
            if p == 0 or p == n:
                break
            if p != 1:
                return p
            x = f(x)
            y = f(f(y))

def primefact(n):
    if n == 1:
        return []
    p = pollard(n)
    if p == n:
        return [p]
    left = primefact(p)
    right = primefact(n // p)
    left += right
    return sorted(left)

n = int(input())
k = int(input())
P = [i for i in range(n)]
for _ in range(k):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    P[x], P[y] = P[y], P[x]

loop = []
used = [False] * n
for i in range(n):
    if used[i]:
        continue
    x = i
    loop.append([])
    while not used[x]:
        used[x] = True
        loop[-1].append(x)
        x = P[x]

Q = int(input())
for _ in range(Q):
    A = list(map(int, input().split()))
    A = [a - 1 for a in A]

    ng = False
    mr = []
    for lp in loop:
        B = [A[i] for i in lp]
        if lp[0] not in B:
            ng = True
            break
        idx = B.index(lp[0])
        B = B[idx:] + B[:idx]
        if lp != B:
            ng = True
            break
        else:
            mr.append((len(lp), len(lp) - idx))
    if ng:
        print(-1)
        continue


    mm = {}
    rr = {}
    for m, r in mr:
        if m == 1:
            continue
        
        primes = primefact(m)
        bef = -1
        x = 1
        for p in primes:
            if p != bef:
                if bef != -1:
                    if bef in mm:
                        r_ = r % x
                        if mm[bef] >= x:
                            if rr[bef] % x != r_:
                                ng = True
                                break
                        else:
                            if r_ % mm[bef] != rr[bef]:
                                ng = True
                                break
                            mm[bef] = x
                            rr[bef] = r_

                    else:
                        mm[bef] = x
                        rr[bef] = r % x
                x = p
                bef = p
            else:
                x *= p

        if bef in mm:
            r_ = r % x
            if mm[bef] >= x:
                if rr[bef] % x != r_:
                    ng = True
            else:
                if r_ % mm[bef] != rr[bef]:
                    ng = True                    
                mm[bef] = x
                rr[bef] = r_
        else:
            mm[bef] = x
            rr[bef] = r % x

        if ng:
            break
    if ng:
        print(-1)
    else:
        M = [1]
        R = [0]
        for k in mm:
            M.append(mm[k])
            R.append(rr[k])
        print(Garner(M, R))
0