結果

問題 No.2119 一般化百五減算
ユーザー titiatitia
提出日時 2022-11-05 01:51:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,371 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 86,680 KB
実行使用メモリ 95,788 KB
最終ジャッジ日時 2023-09-26 03:49:08
合計ジャッジ時間 11,753 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,324 KB
testcase_01 AC 73 ms
70,956 KB
testcase_02 AC 72 ms
71,424 KB
testcase_03 AC 71 ms
71,408 KB
testcase_04 AC 73 ms
71,280 KB
testcase_05 AC 73 ms
71,316 KB
testcase_06 AC 73 ms
71,328 KB
testcase_07 AC 72 ms
71,400 KB
testcase_08 AC 72 ms
70,952 KB
testcase_09 AC 74 ms
71,328 KB
testcase_10 AC 71 ms
71,316 KB
testcase_11 AC 71 ms
71,104 KB
testcase_12 AC 70 ms
71,380 KB
testcase_13 AC 71 ms
71,480 KB
testcase_14 AC 70 ms
71,296 KB
testcase_15 AC 71 ms
71,108 KB
testcase_16 AC 74 ms
71,148 KB
testcase_17 AC 72 ms
71,168 KB
testcase_18 AC 70 ms
71,400 KB
testcase_19 AC 73 ms
71,328 KB
testcase_20 TLE -
testcase_21 AC 141 ms
93,944 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())
M=int(input())

S=[list(map(int,input().split())) for i in range(M)]

S2=set()

for x,y in S:
    ma=x
    a=y%ma
    S2.add((ma,a))

# 拡張ユークリッドの互除法.ax+by=gcd(a,b)となる(x,y)を一つ求め、(x,y)とgcd(x,y)を返す.
def Ext_Euc(a,b,axy=(1,0),bxy=(0,1)): # axy=a*1+b*0,bxy=a*0+b*1なので,a,bに対応する係数の初期値は(1,0),(0,1)
    q,r=divmod(a,b)

    if r==0:
        return bxy,b # a*bxy[0]+b*bxy[1]=b
   
    rxy=(axy[0]-bxy[0]*q,axy[1]-bxy[1]*q) # rに対応する係数を求める.
    return Ext_Euc(b,r,bxy,rxy)

# 中国剰余定理(拡張ユークリッドの互除法を使う)
def Chirem(a,ma,b,mb): # N=a mod ma,N=b mod mbのときN=k mod(lcm(ma,mb))なるk,lcm(ma,mb)を返す.
    (p,q),d=Ext_Euc(ma,mb)
    if (a-b)%d!=0:
        return -1 # 解がないとき-1を出力
    return (b*ma*p+a*mb*q)//d%(ma*mb//d),ma*mb//d

S=list(S2)

if len(S)==1:
    print(S[0][1]%S[0][0])
    exit()


mb=S[0][0]
b=S[0][1]%mb

for i in range(1,len(S)):
    ma=S[i][0]
    a=S[i][1]%S[i][0]

    if ma>N:
        if a==b:
            continue
        else:
            print("NaN")
            exit()

    k=Chirem(a,ma,b,mb)

    if k==-1:
        print("NaN")
        exit()

    else:
        b,mb=k

    if b>N:
        print("NaN")
        exit()

print(b)

    
0