Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.  
  6. long long a,b,k,n,t=0;
  7. cin >> n;
  8. cin >> k;
  9. cin >> a;
  10. cin >> b;
  11. if(k==1){
  12. if(b > (n-1)*a)
  13. {
  14. cout << (n-1)*a;
  15. return 0;
  16. }
  17. else
  18. {
  19. cout << b;
  20. return 0;
  21. }
  22.  
  23.  
  24. }
  25. if(k==1)
  26. {
  27. cout << (n-1)*a;
  28. return 0;
  29. }
  30.  
  31. while(n > 1)
  32. {
  33. if(n > k && n % k > 0){
  34. t += (n%k)*a;
  35. n -= (n%k);}
  36.  
  37. if(n > k && n % k == 0){
  38. if( (n - n/k)*a < b){
  39. t += (n- n/k)*a;
  40. n -= n/k;}
  41. else{
  42. t += b;
  43. n /= k;}
  44. }
  45.  
  46. if(n < k){
  47.  
  48. cout << t + (n-1)*a;
  49. return 0;
  50.  
  51. if(n == k){
  52. if(b > (n-1)*a)
  53. cout << t +(n-1)*a;
  54.  
  55. else
  56. cout << t+b;
  57. return 0;
  58. }
  59.  
  60. if(n <= 1){
  61. break;}
  62.  
  63. }
  64. cout << t;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement