結果

問題 No.1544 [Cherry 2nd Tune C] Synchroscope
ユーザー hir355
提出日時 2021-06-11 21:34:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,367 ms / 2,000 ms
コード長 1,004 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 82,588 KB
実行使用メモリ 76,144 KB
最終ジャッジ日時 2024-12-14 22:36:30
合計ジャッジ時間 7,916 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

def ext_gcd(a :int, b :int):
    # Extended Euclidean algorithm
    x0, y0 = 1, 0
    x1, y1 = 0, 1
 
    while b != 0:
        q,r = divmod(a, b)
        x2 = x0 - q * x1
        y2 = y0 - q * y1
 
        a = b
        b = r
        x0, y0 = x1, y1
        x1, y1 = x2, y2
 
    return (a, x0, y0)
 
 
def crt(r1 :int, m1 :int, r2 :int, m2 :int):
    # Chinese Remainder Theorem
    # x = r1 (mod m1),  x = r2 (mod m2)
    # <-> x = r3 (mod m3)
 
    g,p,q = ext_gcd(m1, m2)
 
    if (r2 - r1) % g != 0:
        return 0, -1
    
    m3 = m1 * m2 // g  # lcm of m1 and m2
    r3 = r1 + m1 * ((r2 - r1) // g * p)
    r3 %= m3
 
    return (r3, m3)

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 100000000000
for i in range(n):
    for j in range(m):
        if a[i] == b[j]:
            t = crt(i, n, j, m)
            if t[1] != -1 and ans > t[0]:
                ans = t[0]
if ans == 100000000000:
    print(-1)
else:
    print(ans + 1)
0