結果

問題 No.2119 一般化百五減算
ユーザー lam6er
提出日時 2025-04-16 00:06:15
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 861 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 81,520 KB
実行使用メモリ 53,472 KB
最終ジャッジ日時 2025-04-16 00:07:43
合計ジャッジ時間 4,845 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

N = int(input())
M = int(input())

congruences = []
for _ in range(M):
    B, C = map(int, input().split())
    c = C % B
    congruences.append((B, c))

if M == 0:
    print("NaN")
else:
    current_a, current_mod = congruences[0][1], congruences[0][0]
    has_solution = True

    for i in range(1, M):
        B_i, c_i = congruences[i]
        d = math.gcd(current_mod, B_i)
        if (c_i - current_a) % d != 0:
            has_solution = False
            break

        new_mod = (current_mod // d) * B_i
        a = current_mod // d
        b_mod = B_i // d
        rhs = (c_i - current_a) // d

        inv = pow(a, -1, b_mod)
        t0 = (rhs * inv) % b_mod

        current_a += current_mod * t0
        current_mod = new_mod

    if not has_solution:
        print("NaN")
    else:
        print(current_a if current_a <= N else "NaN")
0