結果

問題 No.2117 中国剰余定理入門
ユーザー SidewaysOwlSidewaysOwl
提出日時 2022-11-13 20:32:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 736 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 71,664 KB
最終ジャッジ日時 2023-10-13 04:30:57
合計ジャッジ時間 2,744 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 75 ms
71,584 KB
testcase_02 WA -
testcase_03 AC 75 ms
71,436 KB
testcase_04 AC 75 ms
71,168 KB
testcase_05 AC 74 ms
71,240 KB
testcase_06 AC 74 ms
71,564 KB
testcase_07 AC 75 ms
71,408 KB
testcase_08 AC 74 ms
71,112 KB
testcase_09 AC 74 ms
71,240 KB
testcase_10 AC 74 ms
71,416 KB
testcase_11 AC 74 ms
71,100 KB
testcase_12 AC 75 ms
71,460 KB
testcase_13 AC 74 ms
71,296 KB
testcase_14 AC 75 ms
71,404 KB
testcase_15 AC 76 ms
71,308 KB
testcase_16 AC 74 ms
71,060 KB
testcase_17 AC 75 ms
71,484 KB
testcase_18 AC 76 ms
71,664 KB
testcase_19 AC 77 ms
71,108 KB
権限があれば一括ダウンロードができます

ソースコード

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(c0,c1,b0,b1)
if c0 == c1 == 0:
    print(0)
elif x == 0:
    print("NaN")
else:
    print(x)
0