結果

問題 No.2117 中国剰余定理入門
ユーザー SidewaysOwlSidewaysOwl
提出日時 2022-11-05 09:11:47
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 732 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 78,924 KB
最終ジャッジ日時 2023-09-26 09:41:34
合計ジャッジ時間 2,889 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,216 KB
testcase_01 AC 72 ms
71,560 KB
testcase_02 RE -
testcase_03 AC 76 ms
71,212 KB
testcase_04 AC 77 ms
71,300 KB
testcase_05 AC 76 ms
71,296 KB
testcase_06 WA -
testcase_07 AC 74 ms
71,296 KB
testcase_08 AC 74 ms
71,032 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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