結果

問題 No.2207 pCr検査
ユーザー lam6er
提出日時 2025-04-15 22:51:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,567 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 81,868 KB
実行使用メモリ 482,888 KB
最終ジャッジ日時 2025-04-15 22:53:21
合計ジャッジ時間 7,098 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other TLE * 1 -- * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

def factorize(n):
    factors = []
    i = 2
    while i * i <= n:
        if n % i == 0:
            cnt = 0
            while n % i == 0:
                cnt += 1
                n = n // i
            factors.append((i, cnt))
        i += 1
    if n > 1:
        factors.append((n, 1))
    return factors

def compare_factors(input_factors, target_factors):
    input_dict = {}
    for p, e in input_factors:
        input_dict[p] = e
    target_dict = {}
    for p, e in target_factors:
        if p in target_dict:
            target_dict[p] += e
        else:
            target_dict[p] = e
    for p in input_dict:
        if target_dict.get(p, 0) != input_dict[p]:
            return False
    for p in target_dict:
        if input_dict.get(p, 0) != target_dict[p]:
            return False
    return True

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    k = int(input[idx])
    idx += 1
    factors = []
    for _ in range(k):
        p = int(input[idx])
        e = int(input[idx+1])
        factors.append((p, e))
        idx += 2

    p_candidates = [p for p, e in factors if e == 1]

    for p0, _ in factors:
        if p0 not in p_candidates:
            continue

        m_factors = []
        for p, e in factors:
            if p == p0:
                new_e = e - 1
                if new_e > 0:
                    m_factors.append((p, new_e))
            else:
                m_factors.append((p, e))

        if not m_factors:
            print(p0, 1)
            return

        target = (p0 - 1) // 2
        factors_target = factorize(target)
        if compare_factors(m_factors, factors_target):
            print(p0, 2)
            return

        product = (p0 - 1) * (p0 - 2)
        if product % 6 != 0:
            continue
        target3 = product // 6
        factors_target3 = factorize(target3)

        m_plus = {}
        for p, e in m_factors:
            m_plus[p] = e
        m_plus[2] = m_plus.get(2, 0) + 1
        m_plus[3] = m_plus.get(3, 0) + 1

        target_dict = {}
        for p, e in factors_target3:
            target_dict[p] = e

        valid = True
        for p in m_plus:
            if target_dict.get(p, 0) != m_plus[p]:
                valid = False
                break
        if not valid:
            continue
        for p in target_dict:
            if m_plus.get(p, 0) != target_dict[p]:
                valid = False
                break
        if valid:
            print(p0, 3)
            return

    print(-1, -1)

if __name__ == "__main__":
    main()
0