結果
| 問題 |
No.2119 一般化百五減算
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-11-04 23:03:05 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 712 bytes |
| コンパイル時間 | 306 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 88,064 KB |
| 最終ジャッジ日時 | 2024-07-18 20:57:36 |
| 合計ジャッジ時間 | 4,933 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 TLE * 1 -- * 4 |
ソースコード
import sys
input = sys.stdin.readline
from collections import *
def extgcd(a, b):
if b:
d, y, x = extgcd(b, a % b)
y -= (a // b) * x
return d, x, y
return a, 1, 0
def crt(rs, ms):
x = 0; d = 1
for X, Y in zip(rs, ms):
g, a, b = extgcd(d, Y)
x, d = (Y*b*x + d*a*X) // g, d*(Y // g)
x %= d
if x>N:
exit(print('NaN'))
return x, d
N = int(input())
M = int(input())
rs, ms = [], []
for _ in range(M):
B, C = map(int, input().split())
C %= B
rs.append(C)
ms.append(B)
x, d = crt(rs, ms)
for X, Y in zip(rs, ms):
if x%Y!=X:
print('NaN')
exit(0)
if x>N:
print('NaN')
else:
print(x)