結果

問題 No.2119 一般化百五減算
ユーザー H3PO4H3PO4
提出日時 2022-11-05 08:28:54
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 929 bytes
コンパイル時間 1,177 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 89,132 KB
最終ジャッジ日時 2023-09-26 09:11:12
合計ジャッジ時間 6,826 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
77,052 KB
testcase_01 AC 103 ms
72,420 KB
testcase_02 AC 102 ms
72,180 KB
testcase_03 AC 102 ms
72,176 KB
testcase_04 AC 102 ms
72,304 KB
testcase_05 AC 104 ms
72,652 KB
testcase_06 AC 103 ms
72,540 KB
testcase_07 AC 102 ms
72,328 KB
testcase_08 AC 103 ms
72,180 KB
testcase_09 AC 103 ms
72,256 KB
testcase_10 AC 102 ms
72,524 KB
testcase_11 AC 103 ms
72,440 KB
testcase_12 AC 104 ms
72,204 KB
testcase_13 AC 102 ms
72,264 KB
testcase_14 AC 103 ms
72,300 KB
testcase_15 AC 106 ms
72,300 KB
testcase_16 AC 105 ms
72,316 KB
testcase_17 AC 103 ms
72,468 KB
testcase_18 AC 106 ms
72,536 KB
testcase_19 AC 103 ms
72,428 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from functools import lru_cache

input = sys.stdin.buffer.readline

"""中国剰余定理 https://tjkendev.github.io/procon-library/python/math/chinese-remainder.html から拝借し、一部改変しています"""


@lru_cache
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(N, 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
        if x > N:
            return None
        if x % Y != X:
            return None
    return x


def solve(N, V):
    res = remainder(N, V)
    if res is None:
        return "NaN"
    return res


N = int(input())
M = int(input())
V = []
for _ in range(M):
    b, c = map(int, input().split())
    c %= b
    V.append((c, b))
print(solve(N, V))
0