結果

問題 No.2119 一般化百五減算
ユーザー lam6er
提出日時 2025-03-31 17:34:42
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,114 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 82,068 KB
実行使用メモリ 54,004 KB
最終ジャッジ日時 2025-03-31 17:35:19
合計ジャッジ時間 5,659 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import sys
from math import gcd
def crt(a1, m1, a2, m2):
g = gcd(m1, m2)
if (a2 - a1) % g != 0:
return None
m_div = m1 // g
n_div = m2 // g
inv = pow(m_div, -1, n_div)
k = (( (a2 - a1) // g ) * inv) % n_div
lcm = m_div * m2 # lcm(m1, m2)
x0 = (a1 + k * m1) % lcm
return (x0, lcm)
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
M = int(input[ptr])
ptr += 1
B = []
C = []
for _ in range(M):
b = int(input[ptr])
c = int(input[ptr + 1])
ptr += 2
B.append(b)
# Normalize C
c_mod = c % b
C.append(c_mod)
# Initial conditions: x ≡ 0 mod 1
current_a = 0
current_mod = 1
for i in range(M):
b = B[i]
c = C[i]
res = crt(current_a, current_mod, c, b)
if res is None:
print("NaN")
return
current_a, current_mod = res
# Check if current_a is within N
if current_a <= N:
print(current_a)
else:
print("NaN")
if __name__ == "__main__":
main()
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0