結果

問題 No.2119 一般化百五減算
ユーザー roarisroaris
提出日時 2022-11-04 21:32:42
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 829 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 86,700 KB
実行使用メモリ 71,912 KB
最終ジャッジ日時 2023-09-25 23:48:11
合計ジャッジ時間 6,529 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
71,912 KB
testcase_01 AC 87 ms
71,308 KB
testcase_02 AC 88 ms
71,648 KB
testcase_03 AC 88 ms
71,244 KB
testcase_04 AC 88 ms
71,484 KB
testcase_05 AC 88 ms
71,528 KB
testcase_06 AC 87 ms
71,528 KB
testcase_07 AC 86 ms
71,252 KB
testcase_08 AC 88 ms
71,244 KB
testcase_09 AC 89 ms
71,300 KB
testcase_10 AC 87 ms
71,248 KB
testcase_11 AC 88 ms
71,428 KB
testcase_12 AC 86 ms
71,552 KB
testcase_13 AC 91 ms
71,524 KB
testcase_14 AC 85 ms
71,608 KB
testcase_15 AC 85 ms
71,328 KB
testcase_16 AC 85 ms
71,528 KB
testcase_17 AC 87 ms
71,488 KB
testcase_18 AC 87 ms
71,496 KB
testcase_19 AC 87 ms
71,324 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *
from math import gcd
 
def extGCD(a, b): #ax+by=gcd(a,b)となる(x,y)を1つ求める
    if b==0:
        return 1, 0
    
    s, t = extGCD(b, a%b)
    return t, s-a//b*t
 
def crt(rs, ms): #x≡rs[0](mod ms[0]),...となるxについてx≡r(mod lcm(ms))となるrを求める
    r, m = 0, 1
    
    for i in range(len(rs)):
        p, q = extGCD(m, ms[i])
        g = gcd(m, ms[i])
        
        if (rs[i]-r)%g!=0:
            return -1
        
        t = (rs[i]-r)//g*p%(ms[i]//g)
        r += m*t
        m *= ms[i]//g
    
    return r%m

N = int(input())
M = int(input())
rs, ms = [], []

for _ in range(M):
    B, C = map(int, input().split())
    rs.append(C)
    ms.append(B)

x = crt(rs, ms)

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