Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- long long n,k,a,b,x,c=0;
- cin >>n;
- cin >>k;
- cin >>a;
- cin >>b;
- if(b > (n-1)*a || k == 1)
- {
- cout <<((n-1)*a);
- return 0;
- }
- x = n;
- while(x != 1) {
- if(k >= x && (x-1)*a <= b) {
- c += (x-1)*a;
- break;
- }
- else if(x % k == 0 && (x-1)*a >= b) {
- c += b;
- x /= k;
- }
- else{
- c += a*(x%k);
- x -= (x%k);
- }
- }
- cout <<c;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement