結果

問題 No.2119 一般化百五減算
ユーザー mikammikam
提出日時 2022-11-04 22:01:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 601 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 10,944 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-26 00:26:46
合計ジャッジ時間 4,559 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,860 KB
testcase_01 AC 16 ms
8,156 KB
testcase_02 AC 16 ms
8,092 KB
testcase_03 AC 14 ms
8,288 KB
testcase_04 AC 15 ms
7,748 KB
testcase_05 AC 16 ms
8,296 KB
testcase_06 AC 15 ms
8,228 KB
testcase_07 AC 15 ms
7,920 KB
testcase_08 AC 16 ms
8,320 KB
testcase_09 AC 16 ms
7,820 KB
testcase_10 AC 15 ms
8,296 KB
testcase_11 AC 15 ms
7,848 KB
testcase_12 AC 15 ms
8,232 KB
testcase_13 AC 15 ms
7,812 KB
testcase_14 AC 14 ms
7,804 KB
testcase_15 AC 15 ms
7,976 KB
testcase_16 AC 16 ms
7,860 KB
testcase_17 AC 16 ms
7,812 KB
testcase_18 AC 16 ms
7,812 KB
testcase_19 AC 16 ms
8,176 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def extgcd(a, b):
    if b:
        d, y, x = extgcd(b, a % b)
        y -= (a // b) * x
        return d, x, y
    return a, 1, 0

# V = [(X_i, Y_i), ...]: X_i (mod Y_i)
def remainder(V):
    x = 0; d = 1
    for X, Y in V:
        g, a, b = extgcd(d, Y)
        x, d = (Y*b*x + d*a*X) // g, d*(Y // g)
        x %= d
    return x, d

N = int(input())
M = int(input())
V = []
for i in range(M):
    B,C = map(int,input().split())
    C %= B
    V.append([C,B])

x,d = remainder(V)
if x > N:
    print("NaN")
    exit(0)

for C,B in V:
    if x % B != C:
        print("NaN")
        exit(0)

print(x)
0