http://codeforces.com/problemset/problem/940/B
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.
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
9
2
3
1
6
5
5
2
20
8
19
3
4
2
12
In the first testcase, the optimal strategy is as follows:
- Subtract 1 from x (9 → 8) paying 3 coins.
- Divide x by 2 (8 → 4) paying 1 coin.
- Divide x by 2 (4 → 2) paying 1 coin.
- Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
简单题,但记得特判K=1
// 去吧!皮卡丘! 把AC带回来! // へ /| // /\7 ∠_/ // / │ / / // │ Z _,< / /`ヽ // │ ヽ / 〉 // Y ` / / // イ● 、 ● ⊂⊃〈 / // () へ | \〈 // >ー 、_ ィ │ // // / へ / ノ<| \\ // ヽ_ノ (_/ │// // 7 |/ // >―r ̄ ̄`ー―_ //************************************** #pragma comment(linker, "/STACK:1024000000,1024000000") #include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 2147483647 const ll INF = 0x3f3f3f3f3f3f3f3fll; #define ri register int template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } #define scanf1(x) scanf("%d", &x) #define scanf2(x, y) scanf("%d%d", &x, &y) #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X) #define pi acos(-1) #define me(x, y) memset(x, y, sizeof(x)); #define For(i, a, b) for (int i = a; i <= b; i++) #define FFor(i, a, b) for (int i = a; i >= b; i--) #define bug printf("***********\n"); #define mp make_pair #define pb push_back const int maxn = 3e5 + 10; const int maxx = 1e6 + 10; // name******************************* ll n, k, A, B; ll ans = 0; // function****************************** //*************************************** int main() { cin >> n >> k >> A >> B; if(k==1){ cout<<(n-1)*A; return 0; } while (n) { if (n < k) { ans += (n - 1) * A; break; } int t = n / k; ans += (n - k * t) * A + min((k - 1) * A * t, B); n = t; } cout << ans; return 0; }