結果

問題 No.1006 Share an Integer
ユーザー yaumu3
提出日時 2021-03-06 11:10:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,313 ms / 2,000 ms
コード長 1,577 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 95,308 KB
最終ジャッジ日時 2024-10-08 06:00:36
合計ジャッジ時間 14,266 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Dict
from collections import defaultdict


class SmallestPrimeFactors:
    def __init__(self, n: int) -> None:
        self.spf = list(range(n + 1))
        self.spf[0] = -1
        self.spf[1] = -1
        for i in range(2, int(n ** 0.5) + 1):
            if self.spf[i] == i:
                for j in range(i * i, n + 1, i):
                    if self.spf[j] == j:
                        self.spf[j] = i

    def is_prime(self, x: int) -> bool:
        assert x < len(self.spf)
        return self.spf[x] == x

    def __traverse(self, x: int) -> List[int]:
        if self.spf[x] == x:
            return [x]
        nxt = x // self.spf[x]
        return self.__traverse(nxt) + [self.spf[x]]

    def factor(self, x: int) -> Dict[int, int]:
        assert x < len(self.spf)
        if x < 2:
            return {}
        res = defaultdict(int)
        while True:
            if x < 2 or self.is_prime(x):
                if x != 1:
                    res[x] += 1
                break
            s = self.spf[x]
            while x % s == 0:
                x //= s
                res[s] += 1
        return res


def f(x):
    res = 1
    for v in spf.factor(x).values():
        res *= v + 1
    return x - res


x = int(input())
spf = SmallestPrimeFactors(x)
ans = set()
cur_min = 1 << 62
for i in range(1, x // 2 + 1):
    a, b = i, x - i
    tmp = abs(f(a) - f(b))
    if tmp < cur_min:
        ans = {(a, b), (b, a)}
        cur_min = tmp
    elif tmp == cur_min:
        ans |= {(a, b), (b, a)}
for a, b in sorted(ans):
    print(a, b)
0