結果

問題 No.1544 [Cherry 2nd Tune C] Synchroscope
ユーザー tktk_snsntktk_snsn
提出日時 2021-06-11 21:57:35
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 394 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 76,944 KB
最終ジャッジ日時 2023-08-21 12:21:06
合計ジャッジ時間 8,000 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,388 KB
testcase_01 AC 76 ms
71,456 KB
testcase_02 AC 71 ms
71,184 KB
testcase_03 AC 101 ms
76,728 KB
testcase_04 AC 127 ms
76,788 KB
testcase_05 AC 107 ms
76,620 KB
testcase_06 AC 119 ms
76,816 KB
testcase_07 AC 119 ms
76,840 KB
testcase_08 AC 106 ms
76,696 KB
testcase_09 AC 83 ms
76,592 KB
testcase_10 AC 117 ms
76,584 KB
testcase_11 AC 103 ms
76,588 KB
testcase_12 AC 102 ms
76,732 KB
testcase_13 AC 83 ms
76,480 KB
testcase_14 AC 131 ms
76,880 KB
testcase_15 AC 137 ms
76,664 KB
testcase_16 AC 99 ms
76,644 KB
testcase_17 AC 93 ms
76,432 KB
testcase_18 AC 82 ms
76,464 KB
testcase_19 AC 84 ms
76,616 KB
testcase_20 AC 124 ms
76,624 KB
testcase_21 AC 90 ms
76,620 KB
testcase_22 AC 83 ms
76,376 KB
testcase_23 AC 158 ms
76,744 KB
testcase_24 AC 157 ms
76,944 KB
testcase_25 AC 156 ms
76,808 KB
testcase_26 AC 158 ms
76,640 KB
testcase_27 AC 150 ms
76,724 KB
testcase_28 AC 159 ms
76,836 KB
testcase_29 AC 157 ms
76,748 KB
testcase_30 AC 156 ms
76,796 KB
testcase_31 AC 159 ms
76,616 KB
testcase_32 AC 149 ms
76,712 KB
testcase_33 AC 394 ms
76,332 KB
testcase_34 AC 74 ms
71,480 KB
testcase_35 AC 74 ms
71,100 KB
testcase_36 AC 81 ms
76,528 KB
testcase_37 AC 77 ms
76,296 KB
testcase_38 AC 154 ms
76,464 KB
testcase_39 AC 155 ms
76,460 KB
testcase_40 AC 156 ms
76,396 KB
testcase_41 AC 155 ms
76,520 KB
testcase_42 AC 152 ms
76,476 KB
testcase_43 AC 152 ms
76,508 KB
testcase_44 AC 153 ms
76,448 KB
testcase_45 AC 153 ms
76,396 KB
testcase_46 AC 153 ms
76,372 KB
testcase_47 AC 154 ms
76,488 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