The birthday cake has m layers and the total volume is V. From bottom up, the radius r and height h of each layer are decreasing.
For m and v, obtain the smallest surface area s. (Not counted from the ground area on the bottom)
Link: poj1190
Pruning is not yet added .. The output in the sample is all incorrect... No problem found...
# Include <iostream> # include <cstring> # include <string> # include <cstdio> # include <cmath> # include <algorithm> # include <vector> # include <map> # define inf 0x3f3f3fusing namespace std; int ans, V, M, flag, s; int dfs (int v, int m, int newr, int newh) {int r, h, tmp, I; if (m = 0) {flag = 1; if (v = 0 & s <ans) ans = s; return 0 ;}for (I = 1, tmp = 0; I <= m; I ++) // obtain the minimum tmp + = (I * I) for each layer. if (tmp> v) return 0; // if the value is greater than the remaining v, it is impossible to use tmp-=. (M * m); for (r = newr; r> = m; r --) {for (h = newh; h> = m; h --) {// for (I = 0, tmp = 0; I <m; I ++) // The maximum value is obtained for each layer. // This pruning is also affected. // tmp + = (r-I) * (h-I ); // if (v> tmp) break; // It is neither possible if (m = M) s + = r * (2 * h + r) if it is still smaller than v ); else s + = 2 * r * (h + r); if (s <ans) dfs (v-(r * h), S-1, R-1, h-1); if (m = M) s-= r * (2 * h + r); else s-= 2 * r * (h + r );}} return 0;} int main () {while (~ Scanf ("% d", & V, & M) {ans = inf; s = 0; flag = 0; dfs (V, M, 1000,1000 ); printf ("% d \ n", flag? Ans: 0);} return 0 ;}