結果

問題 No.2119 一般化百五減算
ユーザー roarisroaris
提出日時 2022-11-11 12:36:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 707 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 87,224 KB
実行使用メモリ 75,720 KB
最終ジャッジ日時 2023-10-11 09:31:29
合計ジャッジ時間 6,805 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
75,720 KB
testcase_01 AC 64 ms
71,544 KB
testcase_02 AC 64 ms
71,672 KB
testcase_03 AC 65 ms
71,456 KB
testcase_04 AC 66 ms
71,368 KB
testcase_05 AC 68 ms
71,632 KB
testcase_06 AC 65 ms
71,720 KB
testcase_07 AC 65 ms
71,720 KB
testcase_08 AC 65 ms
71,740 KB
testcase_09 AC 64 ms
71,360 KB
testcase_10 AC 67 ms
71,160 KB
testcase_11 AC 68 ms
71,740 KB
testcase_12 AC 66 ms
71,292 KB
testcase_13 AC 67 ms
71,716 KB
testcase_14 AC 66 ms
71,160 KB
testcase_15 AC 65 ms
71,428 KB
testcase_16 AC 65 ms
71,164 KB
testcase_17 AC 66 ms
71,424 KB
testcase_18 AC 65 ms
71,168 KB
testcase_19 AC 65 ms
71,712 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from math import gcd

def ext_gcd(a, b):
    if b==0:
        return 1, 0
    
    s, t = ext_gcd(b, a%b)
    return t, s-a//b*t

def crt(rs, ms):
    r, m = 0, 1
    
    for ri, mi in zip(rs, ms):
        p, q = ext_gcd(m, mi)
        g = gcd(m, mi)
        
        if (ri-r)%g!=0:
            return -1
        
        r += m*(ri-r)//g*p
        m *= mi//g
        r %= m
        
        if r>N:
            exit(print('NaN'))
    
    return r

N = int(input())
M = int(input())
BC = [tuple(map(int, input().split())) for _ in range(M)]
rs = [C for _, C in BC]
ms = [B for B, _ in BC]
ans = crt(rs, ms)

if ans>N or ans==-1:
    print('NaN')
else:
    print(ans)
0