結果

問題 No.1544 [Cherry 2nd Tune C] Synchroscope
ユーザー tktk_snsntktk_snsn
提出日時 2021-06-11 21:57:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 413 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 64,768 KB
最終ジャッジ日時 2024-05-08 17:40:33
合計ジャッジ時間 6,312 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,072 KB
testcase_01 AC 38 ms
51,968 KB
testcase_02 AC 36 ms
51,968 KB
testcase_03 AC 69 ms
63,232 KB
testcase_04 AC 95 ms
64,384 KB
testcase_05 AC 74 ms
63,616 KB
testcase_06 AC 84 ms
63,488 KB
testcase_07 AC 84 ms
64,128 KB
testcase_08 AC 74 ms
63,744 KB
testcase_09 AC 52 ms
61,824 KB
testcase_10 AC 85 ms
64,000 KB
testcase_11 AC 71 ms
63,868 KB
testcase_12 AC 69 ms
63,360 KB
testcase_13 AC 49 ms
61,312 KB
testcase_14 AC 97 ms
63,872 KB
testcase_15 AC 106 ms
63,360 KB
testcase_16 AC 65 ms
63,232 KB
testcase_17 AC 60 ms
62,848 KB
testcase_18 AC 50 ms
61,184 KB
testcase_19 AC 50 ms
61,184 KB
testcase_20 AC 91 ms
63,616 KB
testcase_21 AC 58 ms
62,208 KB
testcase_22 AC 51 ms
61,824 KB
testcase_23 AC 118 ms
64,128 KB
testcase_24 AC 121 ms
64,000 KB
testcase_25 AC 122 ms
63,872 KB
testcase_26 AC 122 ms
64,128 KB
testcase_27 AC 112 ms
64,768 KB
testcase_28 AC 124 ms
64,768 KB
testcase_29 AC 122 ms
63,104 KB
testcase_30 AC 123 ms
63,380 KB
testcase_31 AC 124 ms
64,128 KB
testcase_32 AC 113 ms
64,256 KB
testcase_33 AC 339 ms
63,488 KB
testcase_34 AC 37 ms
51,968 KB
testcase_35 AC 38 ms
51,712 KB
testcase_36 AC 47 ms
61,184 KB
testcase_37 AC 43 ms
59,776 KB
testcase_38 AC 119 ms
61,952 KB
testcase_39 AC 117 ms
62,208 KB
testcase_40 AC 118 ms
62,464 KB
testcase_41 AC 119 ms
62,720 KB
testcase_42 AC 119 ms
62,464 KB
testcase_43 AC 117 ms
62,720 KB
testcase_44 AC 120 ms
63,104 KB
testcase_45 AC 121 ms
63,232 KB
testcase_46 AC 119 ms
62,976 KB
testcase_47 AC 120 ms
62,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def extgcd(a, b):
    """
    拡張Euclidの互除法
    INPUT:
        a, b
    OUTPUT:
        d: gcd(a, b)
        (x, y): ax + by = d の解
    """
    if b == 0:
        return a, (1, 0)
    d, (y, x) = extgcd(b, a % b)
    y -= a // b * x
    return d, (x, y)


N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
g, (x, y) = extgcd(N, -M)

lcm = N * (M // -g)

ans = 10**18
for i, a in enumerate(A, 1):
    for j, b in enumerate(B, 1):
        if a != b:
            continue
        if (j - i) % g != 0:
            continue
        d = (j - i) // g
        tmp = d * N * x + i
        tmp %= lcm
        if tmp == 0:
            tmp += lcm
        ans = min(ans, tmp)


if ans >= 10**18:
    ans = -1
print(ans)
0