結果

問題 No.2119 一般化百五減算
ユーザー roarisroaris
提出日時 2022-11-04 23:04:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 717 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 86,936 KB
実行使用メモリ 86,188 KB
最終ジャッジ日時 2023-09-26 01:34:00
合計ジャッジ時間 3,950 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,420 KB
testcase_01 AC 90 ms
71,504 KB
testcase_02 AC 91 ms
71,464 KB
testcase_03 AC 93 ms
71,404 KB
testcase_04 AC 91 ms
71,404 KB
testcase_05 AC 91 ms
71,336 KB
testcase_06 AC 91 ms
71,504 KB
testcase_07 AC 92 ms
71,408 KB
testcase_08 AC 92 ms
71,552 KB
testcase_09 AC 91 ms
71,656 KB
testcase_10 AC 92 ms
71,428 KB
testcase_11 AC 90 ms
71,700 KB
testcase_12 AC 91 ms
71,580 KB
testcase_13 AC 90 ms
71,316 KB
testcase_14 AC 93 ms
71,752 KB
testcase_15 AC 91 ms
71,428 KB
testcase_16 AC 92 ms
71,712 KB
testcase_17 AC 92 ms
71,860 KB
testcase_18 WA -
testcase_19 AC 89 ms
71,580 KB
testcase_20 WA -
testcase_21 AC 122 ms
82,068 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

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