結果

問題 No.187 中華風 (Hard)
ユーザー convexineqconvexineq
提出日時 2020-12-29 11:16:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 267 ms / 3,000 ms
コード長 1,966 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 74,324 KB
最終ジャッジ日時 2024-04-15 11:35:47
合計ジャッジ時間 5,942 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
63,264 KB
testcase_01 AC 55 ms
62,684 KB
testcase_02 AC 242 ms
73,516 KB
testcase_03 AC 238 ms
74,312 KB
testcase_04 AC 265 ms
73,340 KB
testcase_05 AC 264 ms
73,828 KB
testcase_06 AC 267 ms
73,288 KB
testcase_07 AC 266 ms
74,324 KB
testcase_08 AC 256 ms
72,788 KB
testcase_09 AC 256 ms
72,932 KB
testcase_10 AC 254 ms
72,864 KB
testcase_11 AC 265 ms
73,512 KB
testcase_12 AC 264 ms
73,360 KB
testcase_13 AC 143 ms
72,032 KB
testcase_14 AC 143 ms
72,532 KB
testcase_15 AC 238 ms
72,728 KB
testcase_16 AC 240 ms
73,284 KB
testcase_17 AC 40 ms
53,064 KB
testcase_18 AC 52 ms
61,716 KB
testcase_19 AC 40 ms
53,664 KB
testcase_20 AC 218 ms
72,544 KB
testcase_21 AC 40 ms
53,148 KB
testcase_22 AC 264 ms
73,348 KB
testcase_23 AC 41 ms
53,756 KB
testcase_24 AC 41 ms
54,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def extgcd(x,y):
    if y==0: return 1,0 #g=x
    r0,r1,s0,s1 = x,y,1,0
    while r1 != 0:
        r0,r1, s0,s1 = r1,r0%r1, s1,s0-r0//r1*s1
    #g = r0
    return s0,(r0-s0*x)//y
    
def modinv(a,MOD):
    x,y = extgcd(a,MOD)
    return x%MOD

from math import gcd
def Garner(a,m,already_coprime=True,permit0=True):
    def compute(i,M): # c[0] + c[1]m[0] + c[2]m[0]m[1] + ... c[i-1]m[0]...m[i-2] mod M を返す
        v = c[i-1]
        for j in range(i-2,-1,-1):
            v = (v*m[j] + c[j])%M
        return v
    # m を互いに素にする前計算。矛盾なら -1 を返す
    if already_coprime == 0 == Garner_coprimize(a,m):
        return -1
    # 以下、m は互いに素    
    # ans = c[0] + c[1]m[0] + c[2]m[0]m[1] + ... なる c を求める
    n = len(a)
    c = [0]*n
    c[0] = a[0]
    for i in range(1,n):
        ms = 1
        for j in range(i): ms = ms*m[j]%m[i]
        c[i] = (a[i] - compute(i,m[i]))*modinv(ms,m[i])%m[i]
    if permit0 or any(ci for ci in c):
        v = c[n-1]
        for i in range(n-2,-1,-1):
            v = (v*m[i] + c[i])%MOD
        return v
    else:
        v = 1
        for mi in m: v = v*mi%MOD
        return v

def Garner_coprimize(a,m):
    n = len(a)
    for i in range(1,n):
        for j in range(i):
            g = gcd(m[j],m[i])
            if (a[i]-a[j])%g: return 0
            m[i] //= g
            m[j] //= g
            gi = gcd(g,m[i])
            gj = g//gi
            # 不変量 gi*gj のもとで、gj の素因数を gi にうつす
            g = gcd(gi,gj)
            while g > 1:
                gi *= g
                gj //= g
                g = gcd(gi,gj)
            m[i] *= gi
            m[j] *= gj
            a[i] %= m[i]
            a[j] %= m[j]
    return 1

MOD = 10**9+7
n = int(input())
x = [0]*n
y = [0]*n
for i in range(n):
    xi,yi = map(int,input().split())
    x[i] = xi
    y[i] = yi

ans = Garner(x,y,already_coprime=False,permit0=False)
print(ans)
0