結果
問題 |
No.2119 一般化百五減算
|
ユーザー |
![]() |
提出日時 | 2025-06-12 14:49:00 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,473 bytes |
コンパイル時間 | 370 ms |
コンパイル使用メモリ | 82,728 KB |
実行使用メモリ | 106,628 KB |
最終ジャッジ日時 | 2025-06-12 14:52:34 |
合計ジャッジ時間 | 5,065 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 TLE * 1 -- * 4 |
ソースコード
import sys def extended_gcd(a, b): if b == 0: return (a, 1, 0) else: g, x, y = extended_gcd(b, a % b) return (g, y, x - (a // b) * y) def mod_inverse(a, m): g, x, y = extended_gcd(a, m) if g != 1: return None else: return x % m def merge(a1, m1, a2, m2): g, x, y = extended_gcd(m1, m2) d = g c = a2 - a1 if c % d != 0: return None new_mod = m1 // d * m2 m1_div = m1 // d m2_div = m2 // d c_div = c // d inv = mod_inverse(m1_div, m2_div) if inv is None: return None k0 = (c_div * inv) % m2_div new_a = a1 + m1 * k0 new_a = new_a % new_mod return (new_a, new_mod) def main(): input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr += 1 M = int(input[ptr]) ptr += 1 congruences = [] for _ in range(M): B = int(input[ptr]) ptr += 1 C = int(input[ptr]) ptr += 1 c = C % B congruences.append((B, c)) if M == 0: print(0) return current_a, current_m = congruences[0][1], congruences[0][0] for i in range(1, M): b, c = congruences[i] res = merge(current_a, current_m, c, b) if res is None: print("NaN") return else: current_a, current_m = res if current_a <= N: print(current_a) else: print("NaN") if __name__ == '__main__': main()