刚开始做的时候把Hi>Hi+1忽略了,导致结果错误,而且剪枝不够彻底,要注意使用剩余的部分与当前结果相结合来剪枝。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class BirthdayCake2 {
static int[][] minvs;
static int n;
static int m;
static int mins = 10000000;
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
minvs = new int[21][2];
minvs[1][0] = 1;
minvs[1][1] = 2;
for (int i = 2; i < 21; i++) {
minvs[i][0] = minvs[i - 1][0] + i * i * i;
minvs[i][1] = minvs[i - 1][1] + 2 * i * i;
}
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
sc = new Scanner(new File("files/birthdaycake"));
n = sc.nextInt();
m = sc.nextInt();
for (int i = 71; i >= m; i--) {
for (int j = 30; j >= m; j--) {
if (n - i * i * j >= 0)
dfs(i, j, n - i * i * j, m - 1, i * i + 2 * i * j);
}
}
if (mins == 10000000)
System.out.println(0);
else
System.out.println(mins);
}
private static void dfs(int prer, int preh, int nn, int mm, int cs) {
// TODO Auto-generated method stub
if (mm == 0) {
if (nn == 0 && cs < mins)
mins = cs;
return;
}
if (nn <= 0 || cs + minvs[mm][1] >= mins || 2 * nn / prer + cs >= mins
|| nn < minvs[mm][0])
return;
if (mm == 1) {
if ((prer - 1) * (prer - 1) * (preh - 1) < nn)
return;
for (int r = prer - 1; r > 0; r--) {
if (nn % (r * r) == 0 && nn / (r * r) < preh)
if (cs + 2 * nn / r < mins) {
mins = cs + 2 * nn / r;
break;
}
}
} else {
for (int i = prer - 1; i >= mm; i--) {
for (int j = preh - 1; j >= mm; j--) {
dfs(i, j, nn - i * i * j, mm - 1, cs + 2 * i * j);
}
}
}
}
}
sample input:
10000
8
sample output:
1532