http://codeforces.com/contest/940/problem/B
一个数n,每次可以花费b代价把它除以k(如果k|n),或者花费a的代价把它减一。问把这个数变成1的最少代价。
这种记得暴力直接算,不要想到bfs去了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define ll long long
ll n,k,a,b;
ll DFS(ll x){
if (x<k||k==1){
return a*(x-1);
}
if (x%k!=0){
return DFS(x-x%k)+x%k*a;
}
return DFS(x/k)+min(a*(x-x/k),b);
}
int main(){
cin >> n >> k >> a >> b;
cout << DFS(n) << endl;
}