B. Our Tanya is Crying Out Loud;题目
1特判,因为除1还是本身,这点就很淦
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> a((int)6e5);
vector<int> b((int)6e5), c[(int)6e5];
const int mod = 1e9 + 7;
signed main()
{
int n, k, A, B;
cin >> n >> k >> A >> B;
if (k==1)
{
cout<<(n-1)*A;
return 0;
}
int res = 0;
while (n > 1)
{
if (n % k == 0)
res += min((n - n / k) * A, B), n /= k;
else
res += min((n % k), n - 1) * A, n -= min((n % k), n - 1);
}
cout << res << endl;
}