結果

問題 No.2117 中国剰余定理入門
ユーザー SidewaysOwl
提出日時 2022-11-05 09:11:47
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 732 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,468 KB
実行使用メモリ 66,812 KB
最終ジャッジ日時 2024-07-19 04:37:53
合計ジャッジ時間 1,703 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 12 RE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
def Chinese_Surplus_Theorem(b1,b2,m1,m2):
    def Euclidean_Algorithm(a,b,d):
        a,b = max(a,b),min(a,b)
        if a == d and b == 0:
            return 1,0
        div,mod = divmod(a,b)
        nx,ny = Euclidean_Algorithm(b,mod,d)
        res = (ny,nx - div*ny)
        return res
    d = math.gcd(m1,m2)
    if m1 < m2:
        m1,m2 = m2,m1
        b1,b2 = b2,b1
    if b1 % d != b2 % d:return 0,0
    p,q = Euclidean_Algorithm(m1,m2,d)
    s = (b2-b1)//d
    return (b1 + s*m1*p) % (m1*m2//d) ,m1*m2//d

b0,c0 = map(int,input().split())
b1,c1 = map(int,input().split())
c0 %= b0
c1 %= b1
x,y = Chinese_Surplus_Theorem(b0,c0,b1,c1)
if c0 == c1 == 0:
    print(0)
elif x == 0:
    print("NaN")
else:
    print(x)
0