結果

問題 No.1006 Share an Integer
ユーザー yaumu3yaumu3
提出日時 2021-03-06 10:47:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,371 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 60,928 KB
最終ジャッジ日時 2024-04-16 23:02:03
合計ジャッジ時間 4,438 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
16,896 KB
testcase_01 AC 40 ms
11,264 KB
testcase_02 AC 43 ms
11,520 KB
testcase_03 WA -
testcase_04 AC 43 ms
11,264 KB
testcase_05 AC 48 ms
11,136 KB
testcase_06 AC 50 ms
11,520 KB
testcase_07 AC 51 ms
11,392 KB
testcase_08 AC 48 ms
11,520 KB
testcase_09 AC 52 ms
11,648 KB
testcase_10 AC 50 ms
11,520 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List
from collections import Counter
from functools import reduce


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) -> List[int]:
        assert x < len(self.spf)
        if x < 2:
            return []
        return self.__traverse(x)


def calc_score(x):
    cntr = Counter(spf.factor(x))
    if len(cntr) == 0:
        return x - 1
    return x - reduce(lambda x, y: x * y, [v + 1 for v in cntr.values()])


x = int(input())
spf = SmallestPrimeFactors(x)
ans = []
cur_min = 1 << 62
for i in range(2, x):
    tmp = abs(calc_score(i) - calc_score(x - i))
    if tmp < cur_min:
        ans = [(i, x - i)]
        cur_min = tmp
    elif tmp == cur_min:
        ans.append((i, x - i))
for a, b in ans:
    print(a, b)
0