結果

問題 No.1809 Divide NCK
ユーザー LyricalMaestro
提出日時 2024-01-07 12:18:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 860 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 59,412 KB
最終ジャッジ日時 2024-09-27 19:21:37
合計ジャッジ時間 3,658 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1809

import math

def calc_divisor(K, M):
    m = M
    x = K // m
    ans = x
    while x > 0:
        m *= M
        x = K // m
        ans += x
    return ans


def main():
    N, K, M = map(int, input().split())

    # Mを素因数分解
    primes_map = {}
    sqrt_m = int(math.sqrt(M))
    for p in range(2, sqrt_m + 1):
        if M % p == 0:
            primes_map[p] = 0
            while M % p == 0:
                M //= p
                primes_map[p] += 1
    if M > 1:
        primes_map[M] = 1

    answer = float("inf")
    for p, v in primes_map.items():
        ans1 = calc_divisor(K, p)
        ans2 = calc_divisor(N - K, p)
        ans3 = calc_divisor(N, p)
        x = ans3 - ans2 - ans1
        x0 = x // v
        answer = min(answer, x0)
    print(answer)




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