結果

問題 No.2117 中国剰余定理入門
ユーザー SidewaysOwlSidewaysOwl
提出日時 2022-11-13 20:27:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 732 bytes
コンパイル時間 584 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 71,600 KB
最終ジャッジ日時 2023-10-13 04:26:28
合計ジャッジ時間 3,307 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,104 KB
testcase_01 AC 73 ms
71,340 KB
testcase_02 AC 74 ms
71,476 KB
testcase_03 AC 74 ms
71,348 KB
testcase_04 AC 73 ms
71,584 KB
testcase_05 AC 74 ms
71,196 KB
testcase_06 AC 75 ms
71,600 KB
testcase_07 AC 74 ms
71,444 KB
testcase_08 AC 74 ms
71,584 KB
testcase_09 AC 74 ms
71,364 KB
testcase_10 AC 74 ms
71,400 KB
testcase_11 AC 72 ms
71,344 KB
testcase_12 AC 74 ms
71,336 KB
testcase_13 AC 75 ms
71,184 KB
testcase_14 AC 76 ms
71,100 KB
testcase_15 AC 75 ms
71,472 KB
testcase_16 AC 74 ms
71,512 KB
testcase_17 AC 74 ms
71,356 KB
testcase_18 AC 74 ms
71,108 KB
testcase_19 AC 75 ms
71,508 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