B. Our Tanya is Crying Out Loud
简单题,但记得特判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; }