結果

問題 No.186 中華風 (Easy)
ユーザー Chihaya_chanChihaya_chan
提出日時 2020-08-10 21:46:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,242 bytes
コンパイル時間 81 ms
コンパイル使用メモリ 10,864 KB
実行使用メモリ 8,500 KB
最終ジャッジ日時 2023-09-27 01:00:06
合計ジャッジ時間 1,468 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,272 KB
testcase_01 AC 17 ms
8,492 KB
testcase_02 AC 18 ms
8,324 KB
testcase_03 AC 16 ms
8,432 KB
testcase_04 AC 16 ms
8,280 KB
testcase_05 AC 17 ms
8,320 KB
testcase_06 AC 16 ms
8,296 KB
testcase_07 AC 16 ms
8,316 KB
testcase_08 AC 16 ms
8,308 KB
testcase_09 AC 16 ms
8,444 KB
testcase_10 AC 16 ms
8,288 KB
testcase_11 AC 16 ms
8,452 KB
testcase_12 AC 17 ms
8,436 KB
testcase_13 AC 17 ms
8,284 KB
testcase_14 AC 16 ms
8,312 KB
testcase_15 AC 18 ms
8,492 KB
testcase_16 AC 16 ms
8,496 KB
testcase_17 AC 16 ms
8,280 KB
testcase_18 AC 16 ms
8,268 KB
testcase_19 AC 16 ms
8,412 KB
testcase_20 AC 16 ms
8,284 KB
testcase_21 AC 16 ms
8,284 KB
testcase_22 AC 17 ms
8,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#  中華風(easy)
import math


def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

# すなわちax==1(mod m)なる自然数xを返してくれる関数 動作時間:logM


def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        return False
    else:
        return x % m


def modular_numbers(pair1, pair2):
    x1, y1 = pair1
    x2, y2 = pair2
    g = math.gcd(y1, y2)
    if (x2-x1) % g != 0:
        return (float("inf"), float("inf"))
    else:
        K = (x2-x1)//g
        y1, y2 = y1//g, y2//g
        t = -K*modinv(y2, y1)
        m = x2+t*g*y2
        return (m % (g*y1*y2), g*y1*y2)


def main():
    que = [tuple(map(int, input().split())) for i in range(3)]
    base = modular_numbers(que[0], que[1])
    if base[0] == float("inf"):
        print(-1)
        return
    else:
        ans = modular_numbers(base, que[2])
        if ans[0] == float("inf"):
            print(-1)
            return
        else:
            if ans[0] > 0:
                print(ans[0])
            else:
                print(ans[1])
            return


if __name__ == "__main__":
    main()
0