题意:
维数为m,维度需枚举的深搜,剪枝很重要,手痒了随便找题写写没想到被卡了2个小时。。
代码:
//poj 1190
//sep9
#include <iostream>
#include <cmath>
using namespace std;
int N,M,ans,rm,min_s[20],min_v[20];void dfs(int cur,int res,int sum,int pre_r,int pre_h)
{if(cur==0){if(res==0)ans=min(ans,sum);return ;}if(res<=0) return ;if(sum+min_s[cur]>=ans) return ;if(res<min_v[cur]) return ;if(pre_r>1&&2*res/(pre_r-1)+sum>=ans) return ; for(int r=pre_r-1;r>=cur;--r){if(cur==M) sum=r*r;for(int h=pre_h-1;h>=cur;--h){if(cur==M)rm=r;dfs(cur-1,res-r*r*h,sum+2*r*h,r,h); } }
}int main()
{min_s[0]=0;min_v[0]=0;for(int i=1;i<=20;++i){min_s[i]=min_s[i-1]+2*i*i;min_v[i]=min_v[i-1]+i*i*i; }scanf("%d%d",&N,&M);ans=INT_MAX;dfs(M,N,0,(int)sqrt(N*1.0)+1,N+1);printf("%d",ans==INT_MAX?0:ans); return 0;
}