結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-02-07 12:31:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,498 bytes
コンパイル時間 255 ms
コンパイル使用メモリ 11,008 KB
実行使用メモリ 56,240 KB
最終ジャッジ日時 2023-09-18 20:10:56
合計ジャッジ時間 27,400 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1,220 ms
54,340 KB
testcase_03 AC 1,206 ms
54,440 KB
testcase_04 AC 1,365 ms
56,144 KB
testcase_05 AC 1,371 ms
56,076 KB
testcase_06 AC 1,398 ms
56,144 KB
testcase_07 AC 1,390 ms
56,140 KB
testcase_08 AC 1,306 ms
56,040 KB
testcase_09 AC 1,267 ms
56,240 KB
testcase_10 AC 1,280 ms
56,208 KB
testcase_11 AC 1,394 ms
56,076 KB
testcase_12 AC 1,397 ms
56,068 KB
testcase_13 AC 783 ms
24,680 KB
testcase_14 AC 782 ms
24,696 KB
testcase_15 AC 1,082 ms
39,464 KB
testcase_16 AC 1,089 ms
39,564 KB
testcase_17 AC 17 ms
8,572 KB
testcase_18 WA -
testcase_19 AC 18 ms
8,220 KB
testcase_20 AC 1,054 ms
44,784 KB
testcase_21 AC 18 ms
8,568 KB
testcase_22 AC 1,369 ms
55,952 KB
testcase_23 AC 18 ms
8,508 KB
testcase_24 AC 18 ms
8,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
import sys
sys.setrecursionlimit(10**6)

def extgcd(a, b, x1=1, y1=0, x2=0, y2=1):
    if b == 0:
        return x1, y1
    q = a//b
    return extgcd(b, a%b, x2 ,y2, x1-x2*q, y1-y2*q)

def crt(b, m, md):
    b = b[:]
    m = m + [md]
    N = len(m)
    M = [[1]*N for i in range(N)]
    R = [0]*N
    for i in range(N):
        for j in range(1, i+1):
            M[i][j] = (M[i][j-1] * m[j-1]) % m[i]
        R[i] = extgcd(M[i][i], m[i])[0] % m[i] 
    memo = [[-1]*N for i in range(N)]
    def rec(i, j):
        if i == -1:
            return 0
        if memo[i][j] >= 0:
            return memo[i][j]
        v = (rec(i-1, j) + M[j][i]*(R[i]*(b[i]-rec(i-1, i))%m[i])) % m[j]
        memo[i][j] = v
        return memo[i][j]
    rec(N-2, N-1)
    return memo[N-2][N-1], M[N-1][N-1]


def pre_garner(b, m):
    for i in range(len(m)):
        for j in range(len(m)):
            if i == j:
                continue
            d = gcd(m[i], m[j])
            m[i] //= d
            while True:
                v = gcd(m[i], d)
                if v == 1:
                    break
                m[i] *= v
                m[j] //= v
                d //= v
            b[i] %= m[i]
            b[j] %= m[j]
    return True
    
n = int(input())
a, m = zip(*(tuple(map(int, input().split())) for _ in range(n)))
a, m = list(a), list(m)
if not pre_garner(a, m):
    print(-1)
    exit()
x, mm = crt(a, m, 10**9+7)
if all(ai==0 for ai in a):
    print(mm)
else:
    print(x)

0