結果

問題 No.2119 一般化百五減算
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-11-04 22:29:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 844 bytes
コンパイル時間 500 ms
コンパイル使用メモリ 86,696 KB
実行使用メモリ 71,472 KB
最終ジャッジ日時 2023-09-26 00:59:21
合計ジャッジ時間 3,394 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,376 KB
testcase_01 AC 76 ms
71,136 KB
testcase_02 AC 75 ms
71,044 KB
testcase_03 AC 74 ms
71,128 KB
testcase_04 AC 75 ms
71,304 KB
testcase_05 AC 73 ms
71,368 KB
testcase_06 AC 74 ms
71,364 KB
testcase_07 AC 75 ms
71,432 KB
testcase_08 AC 75 ms
71,212 KB
testcase_09 AC 75 ms
71,392 KB
testcase_10 AC 73 ms
71,420 KB
testcase_11 AC 74 ms
71,256 KB
testcase_12 AC 74 ms
71,252 KB
testcase_13 AC 73 ms
71,312 KB
testcase_14 AC 72 ms
71,036 KB
testcase_15 AC 74 ms
71,192 KB
testcase_16 AC 72 ms
71,120 KB
testcase_17 AC 72 ms
71,348 KB
testcase_18 WA -
testcase_19 AC 74 ms
71,160 KB
testcase_20 WA -
testcase_21 AC 74 ms
71,472 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

def extgcd(a, b):
    # ax + by = gcd(a,b)
    # return gcd(a,b), x, y
    if a == 0:
        return b, 0, 1
    else:
        g, x, y = extgcd(b % a, a)
        return g, y - (b // a) * x, x

def chineseRem(b1, m1, b2, m2):
    # x ≡ b1 (mod m1) ∧ x ≡ b2 (mod m2) <=> x ≡ r (mod m)
    # となる(r. m)を返す
    # 解無しのとき(0, -1)

    d, p, q = extgcd(m1, m2)
    if (b2 - b1) % d != 0:
        return 0, -1
    m = m1 * (m2 // d)  # m = lcm(m1, m2)
    tmp = (b2-b1) // d * p % (m2 // d)
    r = (b1 + m1 * tmp) % m
    return r, m



n = int(input())
m = int(input())

x,y = 0,1

for _ in range(m):
    b,c = map(int,input().split())
    c %= b
    x,y = chineseRem(x,y,c,b)
    # print(x,y,b,c)
    if y == -1 or x > n or y >= 10**30:
        print("NaN")
        exit()
if x <= n:
    print(x)
else:
    print("NaN")
0