Advertisement
amine99

Untitled

Feb 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {ios_base::sync_with_stdio(false);
  5. cin.tie(0);
  6. cout.tie(0);
  7. long long n,k,a,b,x,c=0;
  8. cin >>n;
  9. cin >>k;
  10. cin >>a;
  11. cin >>b;
  12.    if(b > (n-1)*a || k == 1)
  13.     {
  14.     cout <<((n-1)*a);
  15.       return 0;
  16.    }
  17.    x = n;
  18.    while(x != 1) {
  19.       if(k >= x && (x-1)*a <= b) {
  20.          c += (x-1)*a;
  21.          break;
  22.       }
  23.       else if(x % k == 0 && (x-1)*a >= b) {
  24.          c += b;
  25.          x /= k;
  26.       }
  27.       else{
  28.          c += a*(x%k);
  29.          x -= (x%k);
  30.       }
  31.    }
  32.   cout <<c;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement