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