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

 

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

AtCoder Grand Contest 044 A - Pay to Win(记忆化搜索DFS)_第1张图片

 

 

 

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

 

 

 

代码:

 1 #include 
 2 typedef long long LL;
 3 #define pb push_back
 4 #define mst(a) memset(a,0,sizeof(a))
 5 const int INF = 0x3f3f3f3f;
 6 const double eps = 1e-8;
 7 const int mod = 1e9+7;
 8 const int maxn = 1e5+10;
 9 using namespace std;
10 
11 unordered_map ump;
12 LL a, b, c, d, n;
13 
14 LL DFS(LL n)
15 {
16     if(!n) return 0;
17     if(n==1) return d;
18     if(ump[n]) return ump[n];
19     LL res = 1e18;
20     if(n < res/d) res = n*d;
21     res = min(res, n%2*d+DFS(n/2)+a); res = min(res,(2-n%2)*d+DFS((n+1)/2)+a);
22     res = min(res, n%3*d+DFS(n/3)+b); res = min(res,(3-n%3)*d+DFS((n+2)/3)+b);
23     res = min(res, n%5*d+DFS(n/5)+c); res = min(res,(5-n%5)*d+DFS((n+4)/5)+c);
24     ump[n]=res;
25     return res;
26 }
27 
28 int main()
29 {
30     #ifdef DEBUG
31     freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout);
32     #endif
33     
34     int T;
35     scanf("%d",&T);
36     while(T--)
37     {
38         ump.clear();
39         scanf("%lld %lld %lld %lld %lld",&n, &a, &b, &c, &d);
40         printf("%lld\n",DFS(n));
41     }
42     
43     return 0;
44 }

 

 

 

 

 

 

 

-

 

你可能感兴趣的:(AtCoder Grand Contest 044 A - Pay to Win(记忆化搜索DFS))