結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-31 12:44:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,165 bytes
コンパイル時間 833 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 109,884 KB
最終ジャッジ日時 2023-09-13 06:30:24
合計ジャッジ時間 24,990 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1,351 ms
108,148 KB
testcase_03 AC 1,351 ms
108,112 KB
testcase_04 AC 1,477 ms
109,840 KB
testcase_05 AC 1,486 ms
109,772 KB
testcase_06 AC 1,467 ms
109,792 KB
testcase_07 AC 1,475 ms
109,788 KB
testcase_08 AC 1,357 ms
109,792 KB
testcase_09 AC 1,348 ms
109,884 KB
testcase_10 AC 1,345 ms
109,844 KB
testcase_11 AC 1,482 ms
109,796 KB
testcase_12 AC 1,460 ms
109,864 KB
testcase_13 AC 972 ms
78,376 KB
testcase_14 AC 964 ms
78,492 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 20 ms
8,840 KB
testcase_18 WA -
testcase_19 AC 20 ms
8,840 KB
testcase_20 AC 1,110 ms
96,376 KB
testcase_21 AC 20 ms
8,908 KB
testcase_22 AC 1,487 ms
109,608 KB
testcase_23 AC 20 ms
8,896 KB
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
from functools import *
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(a, m, md):
    m = m[:]
    N = len(m)
    for i in range(N-1):
        for j in range(N-1):
            if i == j:
                continue
            g = gcd(m[i], m[j])
            if (a[i]-a[j])%g != 0:
                return None
            m[i] //= g
    m.append(md)
    N += 1
    M = [[1]*N for _ 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] 
    @lru_cache(maxsize=None)
    def rec(i, j):
        if i == -1:
            return 0
        return (rec(i-1, j) + M[j][i]*(R[i]*(a[i] - rec(i-1, i)) % m[i])) % m[j]
    return rec(N-2, N-1), 0

n = int(input())
a, m = zip(*(tuple(map(int, input().split())) for _ in range(n)))
a, m = list(a), list(m)
x = crt(a, m, 10**9+7)
if x is None:
    print(-1)
else:
    x, mm = x
if all(ai==0 for ai in a):
    print(mm)
else:
    print(x)

0