#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int N=20;
int n,m,ans,r[N],h[N];
inline void dfs(int x,int y,int vnow,int snow){
if(snow+r[1]*r[1]>=ans)return;
if(vnow==0&&x==m+1){
snow+=r[1]*r[1];
if(snow<ans)ans=snow;
return;
}
if(x==m+1)return;
if(vnow-r[x-1]*r[x-1]*h[x-1]*y>0)return;//无法凑出体积n的蛋糕了
for(int i=r[x-1]-1;i>=y;--i)
for(int j=h[x-1]-1;j>=y;--j){
if(vnow-i*i*j>=0){
r[x]=i;h[x]=j;
dfs(x+1,y-1,vnow-i*i*j,snow+2*i*j);
h[x]=0;r[x]=0;
}
}
}
int main(){
n=read();m=read();
r[0]=(int)sqrt(n);h[0]=(int)sqrt(n);
ans=1e9;dfs(1,m,n,0);
if(ans==1e9)puts("0");
else printf("%d\n",ans);
return 0;
}
原文:https://www.cnblogs.com/PPXppx/p/11386073.html