結果

問題 No.1006 Share an Integer
ユーザー yaumu3yaumu3
提出日時 2021-03-06 11:20:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,371 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 359 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 95,360 KB
最終ジャッジ日時 2024-04-16 23:30:00
合計ジャッジ時間 14,869 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
67,200 KB
testcase_01 AC 73 ms
67,200 KB
testcase_02 AC 80 ms
68,480 KB
testcase_03 AC 76 ms
67,072 KB
testcase_04 AC 84 ms
70,784 KB
testcase_05 AC 109 ms
78,080 KB
testcase_06 AC 113 ms
78,720 KB
testcase_07 AC 114 ms
78,336 KB
testcase_08 AC 109 ms
78,080 KB
testcase_09 AC 111 ms
77,952 KB
testcase_10 AC 110 ms
78,336 KB
testcase_11 AC 812 ms
88,320 KB
testcase_12 AC 1,303 ms
94,464 KB
testcase_13 AC 1,371 ms
95,316 KB
testcase_14 AC 1,368 ms
95,232 KB
testcase_15 AC 1,196 ms
92,800 KB
testcase_16 AC 655 ms
86,400 KB
testcase_17 AC 837 ms
88,704 KB
testcase_18 AC 1,183 ms
92,928 KB
testcase_19 AC 1,152 ms
92,672 KB
testcase_20 AC 1,229 ms
93,824 KB
testcase_21 AC 1,367 ms
95,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import 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 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 = []
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)] if a != b else [(a, b)]
        cur_min = tmp
    elif tmp == cur_min:
        ans += [(a, b), (b, a)] if a != b else [(a, b)]
for a, b in sorted(ans):
    print(a, b)
0