CF940B.Our Tanya is Crying Out Loud

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Right now she actually isn't. But she will be, if you don't solve this problem.

You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:

  1. Subtract 1 from x. This operation costs you A coins.
  2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.

What is the minimum amount of coins you have to pay to make x equal to 1?

目前她实际上还不是。但如果你不解决这个问题,她就会是。

给你整数 nn、kk、AA 和 BB。有一个数 xx,初始值为 nn。你被允许执行以下两种操作:

  1. 将 xx 减去 11。该操作花费你 AA 枚硬币。
  2. 将 xx 除以 kk。仅当 xx 能被 kk 整除时才可执行该操作。该操作花费你 BB 枚硬币。

要使 xx 变为 11,你最少需要支付多少枚硬币?

输入格式

The first line contains a single integer n (1 ≤ n ≤ 2·109).

The second line contains a single integer k (1 ≤ k ≤ 2·109).

The third line contains a single integer A (1 ≤ A ≤ 2·109).

The fourth line contains a single integer B (1 ≤ B ≤ 2·109).

第一行包含一个整数 nn(1 ≤ n ≤ 2⋅1091 \leq n \leq 2\cdot10^9)。

第二行包含一个整数 kk(1 ≤ k ≤ 2⋅1091 \leq k \leq 2\cdot10^9)。

第三行包含一个整数 AA(1 ≤ A ≤ 2⋅1091 \leq A \leq 2\cdot10^9)。

第四行包含一个整数 BB(1 ≤ B ≤ 2⋅1091 \leq B \leq 2\cdot10^9)。

输出格式

Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.

输出一个整数——使 xx 等于 11 所需支付的最少硬币数量。

输入输出样例

  • 输入#1

    9
    2
    3
    1

    输出#1

    6
  • 输入#2

    5
    5
    2
    20

    输出#2

    8
  • 输入#3

    19
    3
    4
    2

    输出#3

    12

说明/提示

In the first testcase, the optimal strategy is as follows:

  • Subtract 1 from x (9 → 8) paying 3 coins.
  • Divide x by 2 (8 → 4) paying 1 coin.
  • Divide x by 2 (4 → 2) paying 1 coin.
  • Divide x by 2 (2 → 1) paying 1 coin.

The total cost is 6 coins.

In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.

在第一个测试用例中,最优策略如下:

  • 将 xx 减去 1(9 → 89 → 8),花费 3 枚硬币。
  • 将 xx 除以 2(8 → 48 → 4),花费 1 枚硬币。
  • 将 xx 除以 2(4 → 24 → 2),花费 1 枚硬币。
  • 将 xx 除以 2(2 → 12 → 1),花费 1 枚硬币。

总花费为 6 枚硬币。

在第二个测试用例中,最优策略是将 xx 减去 1 共 4 次,总花费为 8 枚硬币。

输入解题思路,AI测评打分。不知道怎么写?

首页