結果
問題 | No.187 中華風 (Hard) |
ユーザー | ctyl_0 |
提出日時 | 2015-12-04 05:38:50 |
言語 | PyPy2 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,701 bytes |
コンパイル時間 | 529 ms |
コンパイル使用メモリ | 76,928 KB |
実行使用メモリ | 80,976 KB |
最終ジャッジ日時 | 2024-09-14 12:28:15 |
合計ジャッジ時間 | 15,103 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 125 ms
78,208 KB |
testcase_01 | AC | 125 ms
78,464 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 957 ms
79,744 KB |
testcase_05 | AC | 956 ms
78,976 KB |
testcase_06 | AC | 997 ms
80,740 KB |
testcase_07 | AC | 965 ms
79,616 KB |
testcase_08 | AC | 743 ms
80,076 KB |
testcase_09 | AC | 858 ms
80,976 KB |
testcase_10 | AC | 831 ms
80,960 KB |
testcase_11 | AC | 954 ms
79,744 KB |
testcase_12 | AC | 955 ms
79,616 KB |
testcase_13 | AC | 238 ms
78,848 KB |
testcase_14 | AC | 253 ms
79,408 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | AC | 88 ms
75,648 KB |
testcase_18 | AC | 118 ms
78,464 KB |
testcase_19 | AC | 88 ms
75,392 KB |
testcase_20 | AC | 788 ms
78,976 KB |
testcase_21 | AC | 87 ms
75,904 KB |
testcase_22 | AC | 954 ms
79,616 KB |
testcase_23 | AC | 89 ms
75,648 KB |
testcase_24 | AC | 87 ms
75,264 KB |
ソースコード
# -*- coding: utf-8 -*- 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 = 30 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 = 1 for i in xrange(len(primes)): ret *= pow(primes[i][0], primes[i][1]) ret %= M return int(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(primes[j][0], primes[j][1]) == 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 = 0 for i in reversed(xrange(len(X))): ret = (ret * X[i][1] + X[i][0]) % M return int(ret) N = int(input()) Z = [] zero = True mod = int(1e9 + 7) for i in xrange(N): 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)