結果

問題 No.2119 一般化百五減算
ユーザー roarisroaris
提出日時 2022-11-11 13:25:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 707 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 10,916 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2023-10-11 10:18:02
合計ジャッジ時間 4,620 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,672 KB
testcase_01 AC 16 ms
8,428 KB
testcase_02 AC 16 ms
8,452 KB
testcase_03 AC 16 ms
8,272 KB
testcase_04 AC 16 ms
8,456 KB
testcase_05 AC 16 ms
8,304 KB
testcase_06 AC 16 ms
8,420 KB
testcase_07 AC 16 ms
8,420 KB
testcase_08 AC 16 ms
8,296 KB
testcase_09 AC 16 ms
8,332 KB
testcase_10 AC 16 ms
8,468 KB
testcase_11 AC 17 ms
8,272 KB
testcase_12 AC 17 ms
8,332 KB
testcase_13 AC 17 ms
8,276 KB
testcase_14 AC 17 ms
8,284 KB
testcase_15 AC 16 ms
8,168 KB
testcase_16 AC 16 ms
8,456 KB
testcase_17 AC 16 ms
8,044 KB
testcase_18 AC 16 ms
8,432 KB
testcase_19 AC 17 ms
8,456 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