description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) if k == 1: print((n - 1) * a) quit() ans = 0 while n > 1: if n < k: ans += (n - 1) * a n = 1 continue ans += min(n % k * a + b, (n - (n - n % k) // k) * a) n -= n % k n //= k print(int(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) x = n s = 0 while x > 1: if x % k != 0 and x > k: s = s + A * (x % k) x = x - x % k elif x >= k: if x * (k - 1) // k * A > B: x = x // k s = s + B else: s = s + A * (x - 1) x = 1 if x > 1 and x < k: s = s + (x - 1) * A x = 1 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def solve(n, k, a, b): ans = 0 if k == 1: print((n - 1) * a) return while n != 1: if n < k: ans += (n - 1) * a n = 1 elif n % k != 0: ans += n % k * a n -= n % k elif b < (n - n // k) * a: n /= k ans += b else: ans += (n - n // k) * a n /= k print(int(ans)) n = int(input()) k = int(input()) a = int(input()) b = int(input()) solve(n, k, a, b)
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) cost = 0 if k == 1: print(A * (n - 1)) else: while n != 1: if n % k != 0: shift = n % k if n > k else n - 1 tcost = shift * A n -= shift else: tcost = min(B, A * (n - n // k)) n //= k cost += tcost print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) total = 0 flag = True while n > 1: if n < k: total += (n - 1) * A break if n % k == 0: new = n / k if flag: if B <= A * (n - new): total += B n = new else: flag = False if not flag: total += (n - 1) * A n = 1 else: temp = n - n // k * k total += temp * A n -= temp print(int(total))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
import sys def read_int(): return int(input()) def read_ints(): return [int(x) for x in input().split()] n = read_int() k = read_int() a = read_int() b = read_int() cost = 0 if k == 1: cost = (n - 1) * a else: while n != 1: if n % k == 0: if b < (n - n // k) * a: cost += b else: cost += (n - n // k) * a n = n // k else: cost += n % k * a n -= n % k if n == 0: n += 1 cost -= a print(cost)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, A, B = [int(input()) for i in "iiii"] s = 0 if k != 1: while n != 1 and n > 0: if n % k == 0: n = n // k s += min(n * (k - 1) * A, B) else: s += n % k * A n = n - n % k if n == 0: s -= A n = 1 print(s) else: print((n - 1) * A)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) if k == 1: print(A * (n - 1)) else: ans = 0 while n != 1: if n >= k: ans += n % k * A n -= n % k else: ans += (n - 1) * A break while n != 1 and n % k == 0: ans += min(B, (n - n // k) * A) n = n // k print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) s = 0 while n != 1: if k == 1: s = (n - 1) * a break if n >= k: if n % k == 0: if b < a * (n - n // k): n = n // k s += b else: s += a * (n - n // k) n = n // k else: d = n % k s += d * a n -= d else: s += (n - 1) * a n = 1 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) c = 0 while n != 1: if n % k == 0 and k != 1: v = n // k d = (n - v) * a if d < b: n = v c += d else: n = n // k c += b elif k == 1: var = (n - 1) * a n = 1 c += var elif n > k: f = n % k n -= f c += a * f else: ans = (n - 1) * a n = 1 c += ans print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
import sys input = sys.stdin.readline n = int(input()) k = int(input()) a = int(input()) b = int(input()) ans = 0 if k == 1: print((n - 1) * a) exit() while n > 1: if n < k: ans += (n - 1) * a break if n % k != 0: ans += n % k * a n -= n % k else: while n % k == 0 and n > 1: temp = n // k if (n - temp) * a >= b: ans += b else: ans += (n - temp) * a n //= k print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) x = n cost = 0 if k == 1: print(int((x - 1) * a)) exit(0) while x > 1: if x < k: cost += (x - 1) * a break rem = x % k x -= rem cost += a * rem if rem == 0: temp = x // k * (k - 1) x //= k cost += min(temp * a, b) print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) if k == 1: n -= 1 print(n * A) exit(0) cnt = 0 while n > 1: if n < k: x = n - 1 cnt += x * A break r = n % k if r != 0: cnt += A * r n -= r else: x = n - n // k cnt += min(B, x * A) n //= k print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) c = 0 x = n if k == 1: c = a * (n - 1) else: while x >= k: y = x % k c += y * a x -= y c += min(b, a * (x - x // k)) x = x // k c += a * (x - 1) print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) sum = 0 sumA = 0 sumB = 0 if k == 1: sum = (n - 1) * A else: while n != 1: if n % k != 0 and n > k: sum = sum + A * (n % k) n = n - n % k elif n < k: sum = sum + A * (n - 1) n = n - (n - 1) else: sumB = sum + B count = n - n // k sumA = sum + A * count if sumA > sumB: n = n // k sum = sumB else: n = n - count sum = sumA print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) cost = 0 if k != 1: while n != 1: if n < k: cost += (n - 1) * A n = 1 elif n % k == 0: res = n // k costA = B costB = (n - res) * A n = res cost += min(costA, costB) else: res = n // k * k cost += (n - res) * A n = res else: cost += (n - 1) * A print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) s = 0 if k == 1: print((n - 1) * A) else: while n // k * (k - 1) * A > B: s += n % k * A + B n //= k print(s + (n - 1) * A)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, A, B = (int(input()) for i in range(4)) cost = 0 if k == 1: print((n - 1) * A) else: while n > 1: if n % k != 0: m = n % k n -= m cost += A * (m - 1) if n < 1 else A * m else: m = n // k cost += (n - m) * A if (n - m) * A < B else B n = m print(cost)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
import sys inf = float("inf") abc = "abcdefghijklmnopqrstuvwxyz" abd = { "a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11, "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17, "s": 18, "t": 19, "u": 20, "v": 21, "w": 22, "x": 23, "y": 24, "z": 25, } mod, MOD = 1000000007, 998244353 vow = ["a", "e", "i", "o", "u"] dx, dy = [-1, 1, 0, 0], [0, 0, 1, -1] def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() n = int(input()) k = int(input()) a = int(input()) b = int(input()) if k == 1: print((n - 1) * a) exit(0) total = 0 while n > 1: if n % k != 0 and n < k: total = total + a * (n - 1) n -= n - 1 elif n % k != 0: total += a * (n % k) n -= n % k elif n % k == 0: total += min(b, (n - n // k) * a) n = n // k print(total)
IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def main(): n, k, a, b = (int(input()) for _ in "nkab") r = 0 if k > 1: while n >= k: r += n % k * a n //= k r += min(b, n * (k - 1) * a) print(r + (n - 1) * a) main()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input().strip()) k = int(input().strip()) a = int(input().strip()) b = int(input().strip()) cost = 0 if k == 1: cost = (n - 1) * a else: while n != 1: if n % k == 0: if b <= (n - n // k) * a: cost += b else: cost += (n - n // k) * a n = n // k else: m = n % k n -= m cost += m * a if n == 0: n = 1 cost -= a print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def run(): n, k, a, b = int(input()), int(input()), int(input()), int(input()) if n == 1: print(0) return if k == 1: print((n - 1) * a) return r = 0 while True: if n < k: print(r + (n - 1) * a) return rem = n % k if rem != 0: r += a * rem n -= rem continue decr = n - n // k r += min(b, decr * a) n = n // k run()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
x = int(input()) k = int(input()) A = int(input()) B = int(input()) ans = 0 while x != 1: if x < k or k == 1: ans += (x - 1) * A x -= x - 1 elif x % k != 0: ans += x % k * A x -= x % k else: ans += min(B, (x - x // k) * A) x = x // k print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, a, b = int(input()), int(input()), int(input()), int(input()) if k == 1: print(a * (n - 1)) else: ans = 0 while n > 1: if n % k == 0: nw = n // k diff = n - nw if a * diff < b: ans += a * diff else: ans += b n = nw else: tosub = n % k ans += a * tosub n -= tosub if n == 0: ans -= a print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) res = 0 if k == 1: print((n - 1) * a) else: while n != 1: res += n % k * a if b > n // k * (k - 1) * a: res += n // k * (k - 1) * a else: res += b n //= k if n < k: res += (n - 1) * a break print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) def fun(n, k, a, b): if k == 1: return (n - 1) * a if n == 1: return 0 if n < k: return (n - 1) * a return min(n % k * a + fun(n // k, k, a, b) + b, (n - 1) * a) print(fun(n, k, a, b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
import sys n = int(input()) k = int(input()) a = int(input()) b = int(input()) coin = 0 if n == 1: print("0") sys.exit() if k == 1: print((n - 1) * a) sys.exit() sum = 0 while n != 1: if n < k: sum = sum + (n - 1) * a break if n % k != 0: sum = sum + a * (n % k) n = n - n % k else: i = n - n // k if b > i * a: n = n // k sum = sum + i * a else: n = n // k sum = sum + b print(sum)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) ans = 0 if n >= k and k != 1: n1 = n while True: if n1 == 1: break if n1 < k: ans += (n1 - 1) * a break if n1 % k != 0: ans += n1 % k * a n1 -= n1 % k ans += min(b, (n1 - n1 // k) * a) n1 = n1 // k print(ans) else: print((n - 1) * a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
x = int(input()) k = int(input()) a = int(input()) b = int(input()) cost = 0 av_a = 0 av_b = 0 if a == 0: cost = 0 elif k == 1: cost = (x - 1) * a else: while x > 1: if x % k != 0: if x > k: w = x % k x = x - w cost = cost + w * a else: cost = cost + (x - 1) * a x = 1 elif x % k == 0: av_a = a av_b = b / (x - x / k) if av_a >= av_b: x = x / k cost = cost + b elif av_a < av_b: cost = cost + a * (x - x / k) x = x / k print("%d" % cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def tanya(n, k, a, b): if k == 1: return (n - 1) * a s = 0 while n > 1: if n % k != 0 and n < k: s += a * (n - 1) n -= n - 1 elif n % k != 0: s += a * (n % k) n -= n % k elif n % k == 0: s += min(b, (n - n // k) * a) n = n // k return s N = int(input()) K = int(input()) A = int(input()) B = int(input()) print(tanya(N, K, A, B))
FUNC_DEF IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def solve(): n = int(input()) k = int(input()) a = int(input()) b = int(input()) c = n ans = 0 if k > 1: while c != 1 and c > 0: if c % k != 0: if c > k: ans += c % k * a c = c - c % k else: ans += (c % k - 1) * a c = c - c % k elif b <= (c - c // k) * a: c = c // k ans += b else: p = c - c // k ans += p * a c = c // k else: ans = a * (n - 1) print(ans) test_case = 1 while test_case: solve() test_case -= 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) if k == 1 or k > n: print((n - 1) * a) else: x = n coin = 0 while x != 1: if x < k: coin += (x - 1) * a x = 1 elif x % k != 0: r = x % k coin += r * a x -= r elif x % k == 0: p = x // k if b <= (x - p) * a: coin += b x = p else: coin += a * (x - p) x = p print(coin)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
import sys n = int(sys.stdin.readline()) k = int(sys.stdin.readline()) A = int(sys.stdin.readline()) B = int(sys.stdin.readline()) if k == 1: print((n - 1) * A) else: ans = 0 while True: x = n // k * k ans += (n - x) * A n = x if (n - n // k) * A <= B: break n //= k ans += B print(ans + (n - 1) * A)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
RealAnswer = 1e18 def KhelteThako(n, k, A, B, ans): global RealAnswer RealAnswer = min(RealAnswer, ans + (n - 1) * A) if n <= 1: if n == 1: RealAnswer = min(RealAnswer, ans) return return if n % k == 0: KhelteThako(n / k, k, A, B, ans + B) elif n > k: xx = n % k KhelteThako(n - xx, k, A, B, ans + xx * A) else: xx = n % k KhelteThako(n - xx + 1, k, A, B, ans + (xx - 1) * A) n = int(input()) k = int(input()) A = int(input()) B = int(input()) ans = (n - 1) * A if k == 1: print(ans) else: KhelteThako(n, k, A, B, 0) print(min(ans, int(RealAnswer)))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN RETURN IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) ans = 0 if k > 1: while n > 1: new_n = max(1, n // k * k) ans += (n - new_n) * a n = new_n while n % k == 0: new_n = n // k ans += min((n - new_n) * a, b) n = new_n else: ans = (n - 1) * a print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) kapa = 0 if k == 1: print((n - 1) * A) else: while n != 1: if n < k: kapa += (n - 1) * A n = 1 elif n % k == 0: kapa += min(B, (n - n // k) * A) n //= k else: kapa += A * (n - n // k * k) n = n // k * k if n == 0: kapa += 1 print(kapa)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, a, b = [int(input()) for _ in range(4)] if k == 1: print((n - 1) * a) exit(0) t, c = n, 0 while t > 1: m = t % k if m: if t < k: m -= 1 c += m * a t -= m else: c += min(a * (k - 1) * t // k, b) t //= k print(c)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
x = [] for i in range(4): x.append(int(input())) n = x[0] k = x[1] a = x[2] b = x[3] cost = 0 x = n while int(x) != 1: if k == 1: cost = cost + (x - 1) * a x = 1 elif x < k: cost = cost + (x - 1) * a x = 1 elif x == k: if b > (x - 1) * a: cost = cost + (x - 1) * a x = 1 else: cost = cost + b x = 1 elif x > k: re = x % k if re > 0: cost = cost + re * a x = x - re elif b < (x - x / k) * a: cost = cost + b x = int(x / k) else: cost = cost + (x - x / k) * a x = int(x / k) print(int(cost))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) x = n cost = 0 while x != 1: if k == 1: cost += A * (x - 1) x = 1 elif x % k == 0: x = x / k cost += min(B, A * (x * k - x)) elif x % k == 1: x = x - 1 cost += A else: cost += A * (x % k - 1) x = x - (x % k - 1) print(int(cost))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) if n == 1: print(0) exit() if k == 1: print((n - 1) * a) exit() ans = 0 while n != 1: q, r = divmod(n, k) if q == 0: ans += (r - 1) * a n = 1 elif r == 0: ans += min(b, (n - n // k) * a) n //= k else: ans += r * a ans += min(b, (n - r - n // k) * a) n //= k print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) coins = 0 while n > 1: if n < k or k == 1: coins += a * (n - 1) n = 1 elif n == k: coins += min(a * (n - 1), b) n = 1 elif n % k == 0: if a * (n - n // k) < b: coins += a * (n - n // k) n //= k else: coins += b n //= k else: coins += a * (n % k) n -= n % k print(coins)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, A, B = (int(input()) for i in range(4)) if k == 1: print((n - 1) * A) else: x = n ans = 0 while x != 1: if 1 < x < k: ans += (x - 1) * A x = 1 elif x % k != 0: ans += (x - x // k * k) * A x = x // k * k else: d = x - x // k ans += min(d * A, B) x //= k print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def solve(n, k, A, B): if k == 1: return A * (n - 1) res = 0 while n >= k: res += A * (n % k) n -= n % k diff = n - n // k if A * diff > B: res += B n //= k else: res += A * (n - 1) n = 1 res += A * (n - 1) return res n, k, A, B = [int(input()) for i in range(4)] print(solve(n, k, A, B))
FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) if k == 1: print(a * (n - 1)) exit(0) res = 0 while n > 1: if n % k == 0: res += min(a * (n - n // k), b) n = n // k else: res += a * (n % k if n > k else n - 1) n = n // k * k print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) ans = 0 while n != 1: if n % k == 0: d = n // k if b >= (n - d) * a: ans += (n - 1) * a n = 1 else: ans += b n = d else: r = n % k ans += r * a n = n - r print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
import sys n = int(sys.stdin.readline().strip()) k = int(sys.stdin.readline().strip()) A = int(sys.stdin.readline().strip()) B = int(sys.stdin.readline().strip()) optimalPrice = 0 if k == 1: optimalPrice = A * (n - 1) else: while n > 1: if n % k == 0: optimalPrice += min(B, A * (n - n // k)) n //= k elif n < k: optimalPrice += A * (n - 1) n = 1 else: optimalPrice += A * (n % k) n -= n % k print(optimalPrice)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) mi = 0 if k == 1: print((n - 1) * A) exit() while n != 1: if n % k != 0: mi += n % k * A n -= n % k if n <= 0: mi -= A * abs(n - 1) n = 1 else: p = n n /= k mi += min(B, (p - n) * A) print(int(mi))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) coins = 0 while n != 1: if n % k == 0 and (n - n / k) * A > B: coins += B n /= k elif n % k == 0: coins += A * (n - 1) n = 1 else: coins += A * (n % k) n = n - n % k print(int(coins))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) mans = 0 if k == 1: print((n - 1) * A) else: while n != 1: ans = 0 if n % k == 0: ans = n / k mans += min((n - ans) * A, B) n = int(n / k) else: x = n - n % k if x == 0: mans += (n - 1) * A n = 1 else: mans += (n - x) * A n = x print(int(mans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) ost = 0 res = 0 while a * (n // k) + b + n % k * a < n * a: ost = ost + n % k * a res = res + b n = n // k res = res + (n - 1) * a + ost print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) cost = 0 if n < k or k == 1: print((n - 1) * a) else: while n > 1: r = n % k cost += r * a n -= r if n == 0: cost -= a break cost += min(a * (n - int(n / k)), b) n = int(n / k) print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
from sys import stdin n, k, a, b = [int(stdin.readline()) for i in range(4)] ans = 0 if k == 1: ans = a * (n - 1) else: while n > 1: div, mod = divmod(n, k) if mod: ans += mod * a n -= mod if not n: ans -= a else: x1 = a * (n - div) ans += min(b, x1) n = div print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
copys = int(input()) k = int(input()) A = int(input()) B = int(input()) time = 0 while copys != 1: b = copys % k if b != 0 and copys >= k and k != 1: time += A * b copys -= b elif b == copys or k == 1: time += A * (copys - 1) copys = 1 break elif b == 0 and B >= A * (copys - copys // k) and k != 1: time += A * (copys - copys // k) copys //= k if copys != 1 and b == 0 and B < A * (copys - copys // k) and k != 1: time += B copys //= k print(time)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, a, b = (int(input()) for i in range(4)) x, s = n, 0 if k <= 1: print((x - 1) * a) quit() while x != 1: if k > x: s, x = s + (x - 1) * a, 1 elif x % k != 0: s, x = s + x % k * a, x - x % k elif x * (k - 1) // k * a > b: s, x = s + b, x // k else: s, x = s + x * (k - 1) // k * a, x // k print(s)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) count = 0 if n == 1: print("0") exit(0) while n // k * a * (k - 1) > b: count += n % k * a + b n = n // k count += (n - 1) * a print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) if k == 1: print(A * (n - 1)) else: SUM = 0 while n >= k: if n % k != 0: SUM += A * (n % k) n -= n % k else: X = A * (n - n // k) SUM += min(B, X) n //= k SUM += A * (n - 1) print(SUM)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input("")) k = int(input("")) a = int(input("")) b = int(input("")) ans = 0 while not n == 1: m = n % k n = n - m ans = ans + m * a if (n - n / k) * a > b: n = n / k ans = ans + b else: ans = ans + (n - 1) * a n = 1 print(int(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) x = n cost = 0 worth_div = True while x > 1: if x % k == 0 and worth_div: worth_div = (x - int(x / k)) * A > B if worth_div: cost += B x = int(x / k) else: cost += A * (x - 1) x = 1 elif x % k == 0 or x < k: cost += A * (x - 1) x = 1 else: cost += x % k * A x -= x % k print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def f(n, k, A, B): if k == 1: return (n - 1) * A if n < k: return A * (n - 1) elif n % k == 0: return min((n - 1) * A, B + f(n // k, k, A, B)) else: return n % k * A + f(n - n % k, k, A, B) n = int(input()) k = int(input()) A = int(input()) B = int(input()) print(f(n, k, A, B))
FUNC_DEF IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, A, B = int(input()), int(input()), int(input()), int(input()) if k == 1: print((n - 1) * A) else: ans = (n - 1) * A for i in range(1, 64): pw = k**i if pw > n: break cur_ans, x = i * B, n - pw while pw > 0: cur_ans += A * (x // pw) x %= pw pw //= k ans = min(ans, cur_ans) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) sum = 0 x = n if k == 1: print((n - 1) * a) else: while x != 1: if (x - 1) * a <= b: break if x < k: sum += (x - 1) * a x = 1 elif x % k != 0: sum += a * (x % k) x = x - x % k elif (x - x / k) * a >= b: x //= k sum += b elif (x - x / k) * a < b: x -= 1 sum += a print(sum + (x - 1) * a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
a = int(input()) b = int(input()) c = int(input()) d = int(input()) n = 0 if b == 1: print(c * (a - 1)) else: while True: if a < b: n += (a - 1) * c a = 1 elif a % b != 0: n += a % b * c a = a - a % b elif d > c * (a - int(a / b)): n += c * (a - int(a / b)) a = int(a / b) else: n += d a = int(a / b) if a <= 1: break print(n)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) tot = 0 while n > 1: if n < k or k == 1: tot = tot + a * (n - 1) n = 1 break if a * (n - int(n / k)) > b: if n % k == 0 and not k == 1: n = n / k tot = tot + b else: tot = tot + a * (n % k) n = n - n % k else: tot = tot + a * (n - n // k) n = n // k print(int(tot))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
o = lambda: int(input()) n = o() k = o() A = o() B = o() coins = 0 if k == 1: print(A * (n - 1)) exit() while n != 1: if n % k != 0: if n < k: coins += A * (n - 1) n -= n - 1 else: coins += A * (n % k) n -= int(n % k) elif (n - n / k) * A < B: coins += int(A * (n - n / k)) n //= k else: n //= k coins += B print(coins)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) sum = 0 while n != 1: if k > 1: if n % k == 0: f = n // k h = n - f n = f sum += min(h * a, b) elif n > k: sum += a * (n % k) n = n - n % k else: sum += a * (n - 1) n = 1 else: sum += (n - 1) * a n = 1 print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) cost = 0 if k != 1: while n != 1 and n > 0: if n % k == 0: n = n // k cost = cost + min(n * (k - 1) * a, b) else: cost = cost + n % k * a n = n - n % k if n == 0: cost = cost - a n = 1 print(cost) else: print((n - 1) * a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) x = n ans = 0 while x > 1: if x % k == 0 and k != 1: case1 = b case2 = (x - x // k) * a ans += min(case1, case2) x = x // k elif x >= k and k != 1: ans += x % k * a x -= x % k else: ans += (x - 1) * a x = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) cost = 0 if k == 1: print((n - 1) * a) else: while n >= k: opa = n % k cost += opa * a n = n - opa if n * (k - 1) * a > k * b: cost += b n = int(n / k) else: cost += (n - int(n / k)) * a n = int(n / k) print(cost + (n - 1) * a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) if k == 1: if n > 1: res = (n - 1) * A print(res) exit(0) else: print(0) exit(0) res = 0 while n > 1: if n < k: res += (n - 1) * A break r = n % k res += r * A n -= r p = (n - n // k) * A res += min(p, B) n = n // k print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
x = int(input()) k = int(input()) A = int(input()) B = int(input()) def test(i): if i == 1: return 0 else: res = A * (i - 1) if i >= k: res = min(res, A * (i % k) + B + test(i // k)) return res if k == 1: print((x - 1) * A) else: print(test(x))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) c = 0 if k > n or k == 1: c = (n - 1) * A n = 1 while n > 1: if n % k != 0: if n > k: c += A * (n % k) n -= n % k else: c += A n -= 1 else: if (n - n / k) * A < B: c += (n - n / k) * A else: c += B n /= k print(str(int(c)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) ans = 0 if n == 1: print(0) elif k == 1: print((n - 1) * A) else: while True: if n == 1: print(ans) break else: ost = n % k if ost == 0: s = A * (n - n // k) ans += min(s, B) n = n // k elif n - ost > 0: ans += A * ost n = n - ost else: n = 1 ans += (ost - 1) * A
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
from sys import stdin as cin def main(): n = int(cin.readline()) k = int(cin.readline()) a = int(cin.readline()) b = int(cin.readline()) o = 0 if k == 1: print((n - 1) * a) return while n > 1: if n % k: t = n - n // k * k if t == n: t -= 1 o += a * t n -= t else: t = n // k o += min(a * (n - t), b) n = t print(o) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN WHILE VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) if k == 1: print((n - 1) * a) exit(0) cost = 0 while n > k: rem = n % k cost += rem * a n -= rem cost_a = (n - n // k) * a n = n // k cost += min(cost_a, b) if n == k: cost += min((n - 1) * a, b) else: cost += (n - 1) * a print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) if k == 1: print((n - 1) * a) exit() else: c = 0 while True: if k > n: c += a * (n - 1) print(int(c)) exit() elif n % k != 0: r = n - n // k * k c += r * a n -= r else: d = n - n / k if b > d * a: c += d * a n -= d else: c += b n /= k if n == 1: print(int(c)) exit()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) coin = 0 if k == 1: print(int(A * (n - 1))) else: while n > 1: if n % k == 0: cur = (n - n / k) * A if cur > B: coin += B else: coin += cur n /= k else: cur = n % k * A n -= n % k coin += cur if n == 0: coin -= A print(int(coin))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) x = n ans = 0 if k == 1: ans = (n - k) * A print(ans) else: while x > 1: if x % k == 0: div = int(x / k) temp1 = (x - div) * A temp2 = B if temp1 > temp2: x = int(x / k) ans += B else: x = int(x / k) ans += temp1 else: val = int(x / k) if val != 0: val1 = val * k ans = ans + (x - val1) * A x = val1 else: ans = ans + (x - 1) * A x = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) cost = 0 if k == 1: cost = (n - 1) * A else: while n > 1: if n % k == 0: d = (k - 1) * n // k if d * A < B: n //= k cost += A * d else: n //= k cost += B elif n > k: cost += n % k * A n -= n % k else: cost += A * (n - 1) n = 1 print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) x = n c = 0 done = False while x > 0: if done: c += x * A x = 0 elif x % k != 0: c += x % k * A x -= x % k elif B < (x - x // k) * A: c += B x = x // k else: c += (x - x // k) * A x = x // k done = True c -= A print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) l = [] s = 0 if k == 1: print(a * (n - 1)) else: while n != 1: if n < k and n != 1: s += (n - 1) * a n = 1 elif n % k != 0: s += n % k * a n -= n % k else: s += min((n - n / k) * a, b) n = n // k print(int(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, A, B, sum = int(input()), int(input()), int(input()), int(input()), 0 while n > 1: if n < k or k == 1: sum += A * (n - 1) break next = n // k if n % k == 0: sum += min(A * (n - next), B) else: next *= k sum += A * (n - next) n = next print(sum)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, a, b, s = int(input()), int(input()), int(input()), int(input()), 0 ans = 0 if n == 1: print(0) exit(0) while n > 1: if k == 1: print((n - 1) * a) exit(0) if k > n: print(ans + (n - 1) * a) exit(0) if n % k == 0: ans = ans + min(b, (n - int(n / k)) * a) n = int(n / k) continue ans += n % k * a n = n - n % k print(int(ans))
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
a = [0] * 4 for i in range(4): a[i] = int(input()) cost = 0 while a[0] > 1: if a[0] % a[1] == 0: if a[1] == 1: cost += (a[0] - 1) * a[2] a[0] = 1 elif a[3] <= (a[0] - a[0] // a[1]) * a[2]: a[0] = a[0] // a[1] cost += a[3] else: cost += (a[0] - a[0] // a[1]) * a[2] a[0] = a[0] // a[1] elif a[0] % a[1] == a[0]: cost += (a[0] - 1) * a[2] a[0] = 1 else: cost += (a[0] - a[1] * (a[0] // a[1])) * a[2] a[0] = a[1] * (a[0] // a[1]) print(cost)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, a, b = int(input()), int(input()), int(input()), int(input()) s = 0 while n > 1: if n < k or k == 1: s += (n - 1) * a n = 1 continue if n % k == 0: if b < a * (n - n // k): s += b else: s += a * (n - n // k) n //= k else: s += n % k * a n -= n % k print(int(s))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
x = int(input()) k = int(input()) a = int(input()) b = int(input()) c = 0 while x > 1: if x % k > 0: if x > k: c += a * (x % k) x -= x % k else: c += a * (x % k - 1) x = 1 elif (x - x // k) * a >= b: c += b x = x // k else: c += (x - 1) * a x = 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) res = 0 while n > k: if n % k > 0: res += n % k * A n -= n % k dif = n - n // k if dif * A > B: res += B n = n // k else: res += (n - 1) * A n = 1 if n == k: dif = n - n // k if dif * A > B: res += B n = n // k else: res += (n - 1) * A n = 1 if n > 1: res += (n - 1) * A n = 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) B = int(input()) summ = 0 while n != 1: if k == 1 or n < k: summ += A * (n - 1) break if n % k != 0: times = n - n // k * k summ += A * times n -= times else: t2 = A * (n - n // k) if B < t2: summ += B else: summ += t2 n = n // k print(summ)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def solve(): n = int(input()) k = int(input()) A = int(input()) B = int(input()) if k == 1: print((n - 1) * A) return cost = 0 x = n while True: if x == 1: print(cost) return r = x % k if r != 0: if x > k: cost += A * r x -= r else: cost += A * (r - 1) x -= r - 1 else: tmp = x // k if A * (x - tmp) <= B: cost += A * (x - tmp) x -= x - tmp else: cost += B x //= k solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) c1 = int(input()) c2 = int(input()) if k == 1: k += n ans = 0 while n > 1: if n > k: temp = n % k ans += temp * c1 n -= temp if n % k == 0: ans += min(c2, (n - n // k) * c1) n //= k else: ans += (n - 1) * c1 n = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
I = lambda: int(input()) n = I() k = I() a = I() b = I() t = 0 if k == 1: print(a * (n - 1)) else: while n > 1: if n >= k: x = n % k else: x = n % k - 1 if x != 0: t += x * a n -= x else: y = n // k xs = n - y if xs * a < b: t += xs * a n -= xs else: t += b n = n // k print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
GI = lambda: int(input()) GIS = lambda: map(int, input().split()) LGIS = lambda: list(GIS()) def main(): n = GI() k = GI() A = GI() B = GI() x = n cost = 0 if k == 1: return A * (x - 1) while x > 1: if x < k: cost += A * (x - 1) x = 1 continue div, mod = divmod(x, k) if mod: cost += A * mod x -= mod else: cost += min((x - div) * A, B) x = div return cost print(main())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, a, b = [int(input()) for i in range(4)] r = 0 if k - 1: while n: m, t = divmod(n, k) r += t * a n -= t r += min(b, (n - m) * a) n = m r -= a else: r += (n - 1) * a print(r)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) r = 0 if k == 1: print((n - 1) * a) return while n != 1: if n % k != 0: r += n % k * a n -= n % k elif n < k: r += (n - 1) * a n = 1 else: if n // k * (k - 1) * a < b: r += n // k * (k - 1) * a else: r += b n //= k print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n, k, a, b = [int(input()) for i in range(4)] sm = 0 x = n if k == 1: print((x - 1) * a) exit() while x != 1: if x % k == 0: a1 = x // k sm += min(b, (x - a1) * a) x //= k else: if x < k: sm += (x - 1) * a break sm += a * (x % k) x = x // k * k print(sm)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
def tanya(): n = int(input()) k = int(input()) a = int(input()) b = int(input()) ans = 0 if k == 1: ans = (n - 1) * a n = 1 while n > 1: rem = n % k q = n // k if rem == 0: ans += min((n - n // k) * a, b) n = n // k else: if q == 0: rem = rem - 1 ans += int(a * rem) n = n - rem print(ans) tanya()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) if k == 1: cost = a * (n - 1) print(cost) exit() ans = 0 cost = 0 while n > 1: cost += n % k * a n -= n % k if n > 1: aa = b bb = (n - n / k) * a cost += min(aa, bb) n = n // k elif n == 0: cost -= a print(int(cost))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) count = 0 if k == 1: count += (n - 1) * a n = 1 else: while n > 1: if n % k == 0: temp = n // k if (n - temp) * a < b: count += (n - temp) * a n = temp else: count += b n = temp else: temp = n // k * k count += (n - temp) * a n = temp if n == 0: count -= a print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) A = int(input()) if k == 1: print((n - 1) * A) exit() B = int(input()) res = 0 x = n k1 = (k - 1) * A while x > 1: res += A * (x % k) x //= k if x: if B > x * k1: res += (x * k - 1) * A x = 1 else: res += B print(res - (1 - x) * A)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
n = int(input()) k = int(input()) a = int(input()) b = int(input()) ans = 0 while n != 1 and n >= k and k != 1: d = n % k if d != 0: ans = ans + d * a n = n - d ans = ans + min((n - n // k) * a, b) n = n // k else: ans = ans + min((n - n // k) * a, b) n = n // k ans = ans + (n - 1) * a print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
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: Subtract 1 from x. This operation costs you A coins. 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? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2·10^9). The second line contains a single integer k (1 ≤ k ≤ 2·10^9). The third line contains a single integer A (1 ≤ A ≤ 2·10^9). The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9). -----Output----- Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. -----Examples----- Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 -----Note----- 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.
inf = 4557430888798830399 M = mod = 1000000007 mod2inv = 500000004 pt = lambda *a, **k: print(*a, **k, flush=True) rd = lambda: map(int, input().split()) n = int(input()) k = int(input()) a = int(input()) b = int(input()) r = 0 if k == 1: print((n - 1) * a) return while n: if n % k: r += n % k * a n -= n % k r += min(b, (n - n // k) * a) n //= k pt(r - a)
ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN WHILE VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR