結果

問題 No.2119 一般化百五減算
ユーザー roarisroaris
提出日時 2022-11-04 23:04:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 717 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 84,352 KB
最終ジャッジ日時 2024-07-18 20:58:22
合計ジャッジ時間 2,480 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

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 d>10**18:
            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)
0