結果

問題 No.187 中華風 (Hard)
ユーザー ctyl_0ctyl_0
提出日時 2015-12-04 14:10:54
言語 PyPy2
(7.3.15)
結果
WA  
実行時間 -
コード長 2,843 bytes
コンパイル時間 1,181 ms
コンパイル使用メモリ 77,648 KB
実行使用メモリ 79,752 KB
最終ジャッジ日時 2023-10-12 13:59:45
合計ジャッジ時間 4,901 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
78,512 KB
testcase_01 AC 89 ms
78,512 KB
testcase_02 AC 92 ms
78,532 KB
testcase_03 AC 91 ms
78,564 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 88 ms
78,836 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 RE -
testcase_18 AC 88 ms
78,532 KB
testcase_19 RE -
testcase_20 WA -
testcase_21 RE -
testcase_22 WA -
testcase_23 AC 89 ms
78,556 KB
testcase_24 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def pow_mod(a, b, m):
    ret = 1
    while b > 0:
        if b & 1:
            ret = ret * a % m
        a = a * a % m
        b >>= 1
    return ret

def gcd(p, q):
    if q == 0:
        return p
    return gcd(q, p % q)

def ext_gcd(p, q): # return gcd, (x, y) which satisfies px + qy = 1
    if q == 0:
        return (p, 1, 0)
    g, y, x = ext_gcd(q, p % q)
    return (g, x, y - (p // q) * x)
    
def inv(p, q): # return p^(-1) mod q when (p, q) is co-prime
    g, x, y = ext_gcd(p, q)
    x %= q
    if x < 0:
        x += q
    return x
    
def garner(X, M, op = 1): # C: (X, Y) M: modulo
    # list up primes lower than 40000
    prime_sup = 40000
    primes = []
    is_prime = [0] * prime_sup
    
    for i in xrange(2, prime_sup):
        if is_prime[i] == 0:
            primes.append([i, 0])
            j = i ** 2
            while j < prime_sup:
                is_prime[j] = i
                j += i
    
    for i in xrange(len(X)):
        y = X[i][1]
        for j in xrange(len(primes)):
            cnt = 0
            while (y % primes[j][0] == 0):
                y /= primes[j][0]
                cnt += 1
            primes[j][1] = max(primes[j][1], cnt)
        if y > 1:
            primes.append([y, 1])
    
    for prime in primes[:][:]:
        if prime[1] == 0:
            primes.remove(prime)
    
    # op = 0
    if op == 0:
        ret = int(1)
        for i in xrange(len(primes)):
            ret *= pow_mod(primes[i][0], primes[i][1], M)
            ret %= M
        return ret
        
    # Does the equation have solutoion?    
    
    for i in range(len(X)):
        for j in range(i + 1, len(X)):
            g = gcd(X[i][1], X[j][1])
            if X[i][0] % g != X[j][0] % g:
                return -1
        
    # reguralization
    
    prime_check = [0] * len(primes)
    
    for i in xrange(len(X)):
        for j in xrange(len(primes)):
            if X[i][1] % pow_mod(primes[j][0], primes[j][1], M) == 0 and prime_check[j] == 0:
                prime_check[j] = 1
            else:
                while X[i][1] % primes[j][0] == 0:
                    X[i][1] /= primes[j][0]
                    X[i][0] %= X[i][1]
    
    X.sort(key = lambda x: (x[1]))
    
    # garner's algorithm
    
    for i in xrange(len(X)):
        for j in xrange(i):
            k = X[i][0] - X[j][0]
            if k < 0:
                k += X[i][1]
            X[i][0] = k * inv(X[j][1], X[i][1]) % X[i][1]
    
    ret = int(0)
    
    for i in reversed(xrange(len(X))):
        ret = (ret * X[i][1] + X[i][0]) % M
        
        
    return ret
    

N = int(input())
Z = []
zero = True
mod = int(1e9 + 7)

for i in xrange(3):
    Z.append(map(int, raw_input().strip().split(" ")))
    zero &= (Z[i][0] == 0)

if zero == True:
    print garner(Z, mod, 0)
else:
    print garner(Z, mod, 1)
0