結果

問題 No.2119 一般化百五減算
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-11-04 22:17:55
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 920 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 84,252 KB
最終ジャッジ日時 2023-09-26 00:44:35
合計ジャッジ時間 5,726 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
75,676 KB
testcase_01 AC 67 ms
71,508 KB
testcase_02 AC 68 ms
71,440 KB
testcase_03 AC 66 ms
71,280 KB
testcase_04 AC 69 ms
71,576 KB
testcase_05 AC 68 ms
71,140 KB
testcase_06 AC 69 ms
71,392 KB
testcase_07 AC 69 ms
71,472 KB
testcase_08 AC 69 ms
71,140 KB
testcase_09 AC 68 ms
71,476 KB
testcase_10 AC 68 ms
71,372 KB
testcase_11 AC 68 ms
71,368 KB
testcase_12 AC 68 ms
71,456 KB
testcase_13 AC 67 ms
71,392 KB
testcase_14 AC 67 ms
71,396 KB
testcase_15 AC 68 ms
71,312 KB
testcase_16 AC 69 ms
71,452 KB
testcase_17 AC 69 ms
71,132 KB
testcase_18 AC 68 ms
71,428 KB
testcase_19 AC 68 ms
71,276 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

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)
    if (b1,m1) == (0,1):
        return b2,m2
    if (b2,m2) == (0,1):
        return b1,m1
    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:
        print("NaN")
        exit()
if x <= n:
    print(x)
else:
    print("NaN")
0