Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define pf printf
- #define sf scanf
- long long n,k,a,b,x,ans;
- int main() {
- sf("%lld%lld%lld%lld",&n,&k,&a,&b);
- if(b > (n-1)*a || k == 1) {
- pf("%lld",(n-1)*a);
- return 0;
- }
- x = n;
- while(x != 1) {
- if(k >= x && (x-1)*a <= b) {
- ans += (x-1)*a;
- break;
- }
- else if(x % k == 0 && (x-1)*a >= b) {
- ans += b;
- x /= k;
- }
- else{
- ans += a*(x%k);
- x -= (x%k);
- }
- }
- pf("%lld",ans);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement