結果

問題 No.2119 一般化百五減算
ユーザー mkawa2mkawa2
提出日時 2022-11-04 21:47:37
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,935 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 93,620 KB
最終ジャッジ日時 2023-09-26 00:08:55
合計ジャッジ時間 10,403 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 151 ms
84,404 KB
testcase_01 AC 153 ms
79,992 KB
testcase_02 AC 154 ms
80,068 KB
testcase_03 AC 157 ms
79,980 KB
testcase_04 AC 156 ms
79,948 KB
testcase_05 AC 156 ms
79,752 KB
testcase_06 AC 157 ms
79,920 KB
testcase_07 AC 157 ms
80,076 KB
testcase_08 AC 158 ms
79,932 KB
testcase_09 AC 156 ms
79,920 KB
testcase_10 AC 157 ms
79,792 KB
testcase_11 AC 150 ms
80,024 KB
testcase_12 AC 153 ms
80,008 KB
testcase_13 AC 153 ms
79,964 KB
testcase_14 AC 155 ms
79,860 KB
testcase_15 AC 165 ms
80,000 KB
testcase_16 AC 161 ms
80,032 KB
testcase_17 AC 153 ms
80,032 KB
testcase_18 AC 154 ms
80,032 KB
testcase_19 AC 157 ms
79,752 KB
testcase_20 TLE -
testcase_21 AC 201 ms
86,668 KB
testcase_22 TLE -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

import typing

def inv_gcd(a, b):
    a %= b
    if a == 0: return b, 0
    s, t = b, a
    m0, m1 = 0, 1
    while t:
        u = s//t
        s -= t*u
        m0 -= m1*u
        s, t = t, s
        m0, m1 = m1, m0
    if m0 < 0: m0 += b//s
    return s, m0

# 複数の「mで割ったらr余る」という条件を満たすxをmod zで返す
# 返り値 x,z(解なしの場合は0,0)
def crt(r: typing.List[int], m: typing.List[int]) -> typing.Tuple[int, int]:
    assert len(r) == len(m)

    n = len(r)
    r0, m0 = 0, 1
    for i in range(n):
        assert 1 <= m[i]
        r1 = r[i]%m[i]
        m1 = m[i]
        if m0 < m1:
            r0, r1 = r1, r0
            m0, m1 = m1, m0
        if m0%m1 == 0:
            if r0%m1 != r1: return 0, 0
            continue

        g, im = inv_gcd(m0, m1)

        u1 = m1//g
        if (r1-r0)%g: return 0, 0

        x = (r1-r0)//g%u1*im%u1

        r0 += x*m0
        m0 *= u1
        if r0 < 0: r0 += m0

    return r0, m0

n = II()
m = II()
bb, cc = [], []
for _ in range(m):
    b, c = LI()
    if b == 1: continue
    c %= b
    bb.append(b)
    cc.append(c)

x, z = crt(cc,bb)
# print(x,z)

if z == 0 or x > n:
    print("NaN")
else:
    print(x)
0