Deep Search + several pruning.
Looks like the search order is also very important ... I don't know if it's because of this and the tle has been a long time ...
#include <cstdio>#include<iostream>#include<fstream>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#defineRep (I, L, R) for (int i=l; i<=r; i++)#defineDown (i, L, R) for (int i=l; i>=r; i--)#defineMAXN 23#defineMAX 1<<30using namespacestd;intN, M, ans, MINV[MAXN], MINS[MAXN];voidSearch (intVintSintLintRintH) { if(!l) {if(v = = N && s <ans) ans=s; return; } if(s+mins[l-1] >= ans | | v+minv[l-1] > N | | (N-V) *2>= r* (ans-s))return; Down (R, R-1, L) { if(L = = m) s = r*R; inttmp = MIN ((n-v-minv[l-1])/(R*R), H1); Down (H, TMP, L) Search (v+r*r*h, S+2*r*h, L1, R, h); }}intMain () {scanf ("%d%d", &n, &m); Ans = n+1; Rep (I,1, m) minv[i] = minv[i-1]+i*i*i; Rep (I,1, m) mins[i] = mins[i-1]+2*i*i; Search (0,0, M, sqrt ((Double) (n-minv[m-1])/m) +1, (n-minv[m-1])/(M*M) +1); if(ans = = n+1) ans =0; printf"%d\n", ans); return 0;}
View Code
POJ-1190 Birthday Cake NOI99