题解:

$f(x)= \begin{cases} min\left(\,f\left(\frac{x}{2}\right)+b,x*a\right) &\left(\,x为偶数\,\right)\ min\left(\,f\left(\,x+1\right)+c,f\left(x-1\right)+a\right) &\left(\,x为奇数\,\right) \end{cases}$

$f \left(x \right)=min \left(f \left( \frac{x}{2}\right) + b,f(x-1) +a \right)$

$f \left(x-1\right)=min \left(f \left(x\right) + c ,f \left(x-2\right)+a \right)\$

$f \left(x \right)=min \left(f \left( \frac{x}{2}\right) + b,f(x-2) +2*a \right)$

$f \left(x-2\right) = min \left( f \left( \frac{x-2}{2} \right) +b , f \left(x-4\right) + 2*a \right)$

$f \left(x\right)= min \left( f \left( \frac{x}{2} \right) +b \, , f \left( \frac{x}{2}-1 \right) +b+2*a\,, f \left(x-4\right)+4*a \right)$

$$f \left( \frac{x}{2} \right) +b \,$$ 一定比 $$f \left( \frac{x}{2}-1 \right) +b+2*a$$ 更优

$f \left(x\right)=min \left( f \left( \frac{x}{2} \right) +b \, , f \left(x-4\right)+4*a \right)$

$f \left(x\right)=min \left( f \left( \frac{x}{2} \right) +b \, , x*a \right)$

代码：

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll> m;
ll x,a,b,c,cnt=0;
ll dfs(ll x)
{
cnt++;
if(m[x])return m[x]-1;
if(x==0)return 0;
if(x==1)return a;
if(x%2==0)m[x]=min(dfs(x/2)+b,x*a)+1;
else m[x]=min(dfs(x-1)+a,dfs(x+1)+c)+1;
return m[x]-1;
}
int main()
{
while(~scanf("%lld %lld %lld %lld",&x,&a,&b,&c))
{
m.clear();
printf("%lld\n",dfs(x));
}
return 0;
}


I.A Small Game

(0)
(0)