結果

問題 No.187 中華風 (Hard)
ユーザー proriboneproribone
提出日時 2024-02-17 01:28:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 765 ms / 3,000 ms
コード長 1,412 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 77,120 KB
最終ジャッジ日時 2024-02-17 01:28:19
合計ジャッジ時間 12,350 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
63,196 KB
testcase_01 AC 52 ms
63,196 KB
testcase_02 AC 564 ms
76,364 KB
testcase_03 AC 544 ms
76,356 KB
testcase_04 AC 711 ms
76,336 KB
testcase_05 AC 743 ms
76,208 KB
testcase_06 AC 700 ms
76,208 KB
testcase_07 AC 765 ms
76,336 KB
testcase_08 AC 525 ms
76,604 KB
testcase_09 AC 529 ms
76,604 KB
testcase_10 AC 506 ms
76,604 KB
testcase_11 AC 666 ms
76,208 KB
testcase_12 AC 597 ms
76,588 KB
testcase_13 AC 153 ms
75,752 KB
testcase_14 AC 164 ms
76,132 KB
testcase_15 AC 629 ms
76,724 KB
testcase_16 AC 663 ms
77,120 KB
testcase_17 AC 39 ms
53,588 KB
testcase_18 AC 50 ms
61,100 KB
testcase_19 AC 37 ms
53,588 KB
testcase_20 AC 474 ms
76,464 KB
testcase_21 AC 39 ms
53,588 KB
testcase_22 AC 730 ms
76,464 KB
testcase_23 AC 38 ms
53,588 KB
testcase_24 AC 38 ms
53,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def pregarner(r,m):
    def gcd(a,b):
        if b==0:
            return a
        return gcd(b,a%b)
    n=len(m)

    for i in range(n):
        for j in range(i):
            g=gcd(m[i],m[j])
            if (r[i]-r[j])%g!=0:
                return False
            
            m[i]//=g;m[j]//=g
            gi=gcd(m[i],g)
            gj=g//gi

            while gcd(gi,gj)!=1:
                h=gcd(gi,gj)
                gi*=h
                gj//=h
            
            m[i]*=gi
            m[j]*=gj
    
    return True

def garner(r,m,MOD):
    def extgcd(a,b):
        if b==0:
            return 1,0,a
        else:
            c,x,g=extgcd(b,a%b)
            return x,c-a//b*x,g
    
    def modinv(a,m):
        x,_,_=extgcd(a,m)
        return x%m
    
    n=len(r)
    m.append(MOD)

    constants=[0]*(n+1)
    coeffs=[1]*(n+1)
    
    for k in range(len(r)):
        t=(r[k]-constants[k])*modinv(coeffs[k],m[k])%m[k]
        for i in range(k+1,len(m)):
            constants[i]=(constants[i]+t*coeffs[i])%m[i]
            coeffs[i]=coeffs[i]*m[k]%m[i]

    return constants[-1], coeffs[-1]

N=int(input())
X,Y=[],[]

for _ in range(N):
    x,y=map(int,input().split())
    X.append(x)
    Y.append(y)

exist_non_zero=any([x!=0 for x in X])

if not pregarner(X,Y):
    print(-1)
else:
    res,lcm=garner(X,Y,10**9+7)
    if not exist_non_zero:
        print(lcm)
    else:
        print(res)
0