AtCoder Grand Contest 044 A - Pay to Win(记忆化搜索DFS)

AtCoder Grand Contest 044 A - Pay to Win(记忆化搜索DFS)

https://atcoder.jp/contests/agc044/tasks/agc044_a

 

题目讲解:https://www.bilibili.com/video/BV1354y1Q7yw?p=4

 

代码:

#include <bits/stdc++.h>
typedef long long LL;
#define pb push_back
#define mst(a) memset(a,0,sizeof(a))
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int mod = 1e9+7;
const int maxn = 1e5+10;
using namespace std;

unordered_map<LL, LL> ump;
LL a, b, c, d, n;

LL DFS(LL n)
{
    if(!n) return 0;
    if(n==1) return d;
    if(ump[n]) return ump[n];
    LL res = 1e18;
    if(n < res/d) res = n*d;
    res = min(res, n%2*d+DFS(n/2)+a); res = min(res,(2-n%2)*d+DFS((n+1)/2)+a);
    res = min(res, n%3*d+DFS(n/3)+b); res = min(res,(3-n%3)*d+DFS((n+2)/3)+b);
    res = min(res, n%5*d+DFS(n/5)+c); res = min(res,(5-n%5)*d+DFS((n+4)/5)+c);
    ump[n]=res;
    return res;
}

int main()
{
    #ifdef DEBUG
    freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout);
    #endif
    
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ump.clear();
        scanf("%lld %lld %lld %lld %lld",&n, &a, &b, &c, &d);
        printf("%lld\n",DFS(n));
    }
    
    return 0;
}

 

-

posted @ 2020-05-29 02:53  jiamian22  阅读(338)  评论(0)    收藏  举报