結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-31 13:00:40
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,206 bytes
コンパイル時間 551 ms
コンパイル使用メモリ 87,236 KB
実行使用メモリ 293,148 KB
最終ジャッジ日時 2023-09-13 06:41:50
合計ジャッジ時間 23,006 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
77,036 KB
testcase_01 AC 113 ms
77,264 KB
testcase_02 MLE -
testcase_03 MLE -
testcase_04 MLE -
testcase_05 MLE -
testcase_06 MLE -
testcase_07 MLE -
testcase_08 MLE -
testcase_09 MLE -
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 MLE -
testcase_17 AC 101 ms
72,376 KB
testcase_18 AC 114 ms
77,268 KB
testcase_19 AC 99 ms
72,668 KB
testcase_20 MLE -
testcase_21 AC 102 ms
72,456 KB
testcase_22 MLE -
testcase_23 AC 102 ms
72,480 KB
testcase_24 AC 100 ms
72,484 KB
権限があれば一括ダウンロードができます

ソースコード

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):
    a = a[:]
    m = m[:]
    N = len(m)
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            g = gcd(m[i], m[j])
            if (a[i]-a[j])%g != 0:
                return None
            m[i] //= g
        a[i] %= m[i]
    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), M[N-1][N-1]

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)
    exit()
x, mm = x
if all(ai==0 for ai in a):
    print(mm)
else:
    print(x)

0