結果

問題 No.1544 [Cherry 2nd Tune C] Synchroscope
ユーザー hir355hir355
提出日時 2021-06-11 21:34:28
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,258 ms / 2,000 ms
コード長 1,004 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 77,256 KB
最終ジャッジ日時 2023-08-21 11:43:49
合計ジャッジ時間 9,476 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,224 KB
testcase_01 AC 68 ms
71,172 KB
testcase_02 AC 69 ms
71,272 KB
testcase_03 AC 107 ms
76,380 KB
testcase_04 AC 133 ms
76,508 KB
testcase_05 AC 114 ms
76,716 KB
testcase_06 AC 124 ms
76,444 KB
testcase_07 AC 124 ms
76,844 KB
testcase_08 AC 107 ms
76,604 KB
testcase_09 AC 80 ms
75,836 KB
testcase_10 AC 120 ms
76,344 KB
testcase_11 AC 108 ms
76,728 KB
testcase_12 AC 103 ms
76,520 KB
testcase_13 AC 78 ms
76,228 KB
testcase_14 AC 135 ms
76,596 KB
testcase_15 AC 144 ms
76,712 KB
testcase_16 AC 100 ms
76,544 KB
testcase_17 AC 95 ms
76,448 KB
testcase_18 AC 80 ms
76,436 KB
testcase_19 AC 82 ms
76,288 KB
testcase_20 AC 135 ms
76,472 KB
testcase_21 AC 91 ms
76,484 KB
testcase_22 AC 81 ms
76,252 KB
testcase_23 AC 168 ms
76,452 KB
testcase_24 AC 167 ms
76,852 KB
testcase_25 AC 169 ms
76,956 KB
testcase_26 AC 169 ms
76,976 KB
testcase_27 AC 168 ms
76,288 KB
testcase_28 AC 168 ms
76,504 KB
testcase_29 AC 167 ms
76,476 KB
testcase_30 AC 168 ms
76,308 KB
testcase_31 AC 170 ms
76,732 KB
testcase_32 AC 170 ms
76,416 KB
testcase_33 AC 1,258 ms
77,256 KB
testcase_34 AC 70 ms
71,256 KB
testcase_35 AC 69 ms
71,232 KB
testcase_36 AC 78 ms
76,268 KB
testcase_37 AC 74 ms
75,892 KB
testcase_38 AC 160 ms
76,248 KB
testcase_39 AC 161 ms
76,636 KB
testcase_40 AC 161 ms
76,380 KB
testcase_41 AC 159 ms
76,328 KB
testcase_42 AC 158 ms
76,396 KB
testcase_43 AC 157 ms
76,640 KB
testcase_44 AC 158 ms
76,416 KB
testcase_45 AC 161 ms
76,176 KB
testcase_46 AC 159 ms
76,020 KB
testcase_47 AC 159 ms
76,640 KB
権限があれば一括ダウンロードができます

ソースコード

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