AtCoder Grand Contest 044 A

题意: 有4种操作,你需要用最少的操作使得0变成n.
n×2
n×3
n×5
n±1.
思路,反过来,由n变为0,然后从优先级来看,肯定是除为上,懂了这个以后,就可以开始枚举遍历了。具体细节看代码吧
AC代码:

#include
#include
#include 
#include
#include
#include 
#include
#include
#include
#include
#include
#include
#include 
#define ll   long long
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define inf 0x3f3f3f3f
#define pi 3.1415926535898
using namespace std;
int t;
ll n,a,b,c,d;
map <ll,ll> mp;
ll f(ll n)
{
    if(mp.count(n))
        return mp[n];
    ll res=n<1e18/d?n*d:(ll)1e18;//确定最大值
    ll l1=(n/2)*2;//向下取整
    ll r1=((n+1)/2)*2;//向上取整
    ll l2=(n/3)*3;
    ll r2=((n+2)/3)*3;
    ll l3=(n/5)*5;
    ll r3=((n+4)/5)*5;
    res=min(res,(n-l1)*d+f(l1/2)+a);//枚举搜索
    res=min(res,(r1-n)*d+f(r1/2)+a);
    res=min(res,(n-l2)*d+f(l2/3)+b);
    res=min(res,(r2-n)*d+f(r2/3)+b);
    res=min(res,(n-l3)*d+f(l3/5)+c);
    res=min(res,(r3-n)*d+f(r3/5)+c);
    return mp[n]=res;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>a>>b>>c>>d;
        mp.clear();
        mp[0]=0;
        mp[1]=d;
        cout<<f(n)<<endl;
    }

    return 0;
}

你可能感兴趣的:(Atcoder)