結果

問題 No.2744 Power! or +1
ユーザー 👑 rin204rin204
提出日時 2024-04-20 14:10:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 414 ms / 3,000 ms
コード長 3,651 bytes
コンパイル時間 980 ms
コンパイル使用メモリ 82,456 KB
実行使用メモリ 91,696 KB
最終ジャッジ日時 2024-04-20 14:10:28
合計ジャッジ時間 2,723 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 414 ms
91,696 KB
testcase_01 AC 59 ms
77,660 KB
testcase_02 AC 58 ms
77,504 KB
testcase_03 AC 59 ms
78,148 KB
testcase_04 AC 155 ms
82,988 KB
testcase_05 AC 336 ms
88,124 KB
testcase_06 AC 192 ms
83,328 KB
testcase_07 AC 55 ms
75,380 KB
testcase_08 AC 230 ms
85,348 KB
testcase_09 AC 57 ms
75,940 KB
testcase_10 AC 57 ms
76,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
from heapq import *


def MillerRabin(n):
    if n <= 1:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False

    if n < 4759123141:
        A = [2, 7, 61]
    else:
        A = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
    s = 0
    d = n - 1
    while d % 2 == 0:
        s += 1
        d >>= 1

    for a in A:
        if a % n == 0:
            return True
        x = pow(a, d, n)
        if x != 1:
            for t in range(s):
                if x == n - 1:
                    break
                x = x * x % n
            else:
                return False
    return True


def pollard(n):
    # https://qiita.com/t_fuki/items/7cd50de54d3c5d063b4a

    if n % 2 == 0:
        return 2

    m = int(n**0.125) + 1
    step = 0

    while 1:
        step += 1

        def f(x):
            return (x * x + step) % n

        y = k = 0
        g = q = r = 1

        while g == 1:
            x = y
            while k < 3 * r // 4:
                y = f(y)
                k += 1
            while k < r and g == 1:
                ys = y
                for _ in range(min(m, r - k)):
                    y = f(y)
                    q = q * abs(x - y) % n
                g = gcd(q, n)
                k += m
            k = r
            r <<= 1

        if g == n:
            g = 1
            y = ys
            while g == 1:
                y = f(y)
                g = gcd(abs(x - y), n)

        if g == n:
            continue
        if MillerRabin(g):
            return g
        elif MillerRabin(n // g):
            return n // g
        else:
            return pollard(g)


def primefact(n):
    res = []
    while n > 1 and not MillerRabin(n):
        p = pollard(n)
        while n % p == 0:
            res.append(p)
            n //= p
    if n != 1:
        res.append(n)
    return sorted(res)


def primedict(n):
    P = primefact(n)
    ret = {}
    for p in P:
        ret[p] = ret.get(p, 0) + 1
    return ret


n, a, b, c = map(int, input().split())
x = 1
fact = [1, 1]
m = 400200
while fact[-1] < m:
    x += 1
    fact.append(fact[-1] * x)

if m % n == 0:
    m += 1
inf = 1 << 60
dist = [inf] * (m + 2)
dist[2] = a
for i in range(2, m + 1):
    dist[i + 1] = min(dist[i + 1], dist[i] + a)
    if i < x:
        dist[fact[i]] = min(dist[fact[i]], dist[i] + c)
    else:
        dist[m + 1] = min(dist[m + 1], dist[i] + c)

    cost = b
    nex = i
    while nex * i <= m:
        nex *= i
        cost *= b
        dist[nex] = min(dist[nex], dist[i] + cost)
    cost *= b
    dist[m + 1] = min(dist[m + 1], dist[i] + cost)

ans = 1 << 60

for i in range(n, m + 2, n):
    ans = min(ans, dist[i])

ps = primedict(n)


def ok(x):
    for k, v in ps.items():
        tot = 0
        y = x
        while y > 0:
            y //= k
            tot += y
            if tot >= v:
                break
        if tot < v:
            return False
    return True


l = 1
r = n
while r - l > 1:
    mid = (l + r) // 2
    if ok(mid):
        r = mid
    else:
        l = mid

ans = min(ans, min(dist[r:]) + c)


dist = [inf] * n
dist[1] = 0
hq = [1]
while hq:
    tmp = heappop(hq)
    d = tmp // n
    pos = tmp - d * n
    if dist[pos] < d:
        continue
    npos = (pos + 1) % n
    nd = d + a
    if dist[npos] > nd:
        dist[npos] = nd
        heappush(hq, nd * n + npos)

    npos = pos
    cc = b
    while cc * b < 400200:
        cc *= b
        npos = npos * pos % n
        nd = d + cc
        if dist[npos] > nd:
            dist[npos] = nd
            heappush(hq, nd * n + npos)

ans = min(ans, dist[0])


print(ans)
0