結果
| 問題 | 
                            No.2119 一般化百五減算
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2022-11-04 23:00:46 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 665 bytes | 
| コンパイル時間 | 165 ms | 
| コンパイル使用メモリ | 81,792 KB | 
| 実行使用メモリ | 88,448 KB | 
| 最終ジャッジ日時 | 2024-07-18 20:55:51 | 
| 合計ジャッジ時間 | 4,734 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| 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
    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)