結果

問題 No.811 約数の個数の最大化
ユーザー tktk_snsntktk_snsn
提出日時 2021-04-28 18:45:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,011 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-07-07 17:22:36
合計ジャッジ時間 1,740 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
10,880 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 88 ms
12,032 KB
testcase_03 AC 24 ms
10,880 KB
testcase_04 AC 25 ms
10,880 KB
testcase_05 AC 27 ms
11,008 KB
testcase_06 AC 30 ms
11,008 KB
testcase_07 AC 35 ms
11,136 KB
testcase_08 AC 59 ms
11,392 KB
testcase_09 AC 78 ms
11,392 KB
testcase_10 AC 52 ms
11,264 KB
testcase_11 AC 91 ms
12,032 KB
testcase_12 AC 45 ms
11,136 KB
testcase_13 AC 182 ms
12,160 KB
testcase_14 AC 95 ms
11,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
from math import gcd
from functools import lru_cache


def prime_sieve(N):
    #sieve[i] : iの最小の素因数
    sieve = [0] * (N + 1)
    prime = []
    for i in range(2, N + 1):
        if sieve[i] == 0:
            sieve[i] = i
            prime.append(i)
        for p in prime:
            if p > sieve[i] or i * p > N:
                break
            sieve[i * p] = p
    return sieve


@lru_cache(maxsize=None)
def get_factor(N):
    cnt = 0
    while sieve[N]:
        cnt += 1
        N //= sieve[N]
    return cnt


def count_divisor(N):
    pf = []
    while sieve[N]:
        pf.append(sieve[N])
        N //= sieve[N]
    res = 1
    for v in Counter(pf).values():
        res *= v+1
    return res


N, K = map(int, input().split())
sieve = prime_sieve(N)

ans = 0
div = 0
for x in reversed(range(1, N)):
    g = gcd(x, N)
    if get_factor(g) >= K:
        cnt = count_divisor(x)
        if cnt >= div:
            div = cnt
            ans = x

print(ans)
0