結果
問題 | No.2119 一般化百五減算 |
ユーザー |
![]() |
提出日時 | 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 |
ソースコード
import sysfrom math import gcddef crt(a1, m1, a2, m2):g = gcd(m1, m2)if (a2 - a1) % g != 0:return Nonem_div = m1 // gn_div = m2 // ginv = pow(m_div, -1, n_div)k = (( (a2 - a1) // g ) * inv) % n_divlcm = m_div * m2 # lcm(m1, m2)x0 = (a1 + k * m1) % lcmreturn (x0, lcm)def main():input = sys.stdin.read().split()ptr = 0N = int(input[ptr])ptr += 1M = int(input[ptr])ptr += 1B = []C = []for _ in range(M):b = int(input[ptr])c = int(input[ptr + 1])ptr += 2B.append(b)# Normalize Cc_mod = c % bC.append(c_mod)# Initial conditions: x ≡ 0 mod 1current_a = 0current_mod = 1for i in range(M):b = B[i]c = C[i]res = crt(current_a, current_mod, c, b)if res is None:print("NaN")returncurrent_a, current_mod = res# Check if current_a is within Nif current_a <= N:print(current_a)else:print("NaN")if __name__ == "__main__":main()