HIT暑期集训Day4 搜索
A POJ 1190
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cmath> #define inf 0x3f3f3f3f using namespace std; int n,m,ans,sumv[25],sums[25]; void dfs(int lr,int lh,int res,int num,int s) { if (num==0) { if (res==0 && s<ans) ans=s; return; } if (sumv[num]>res || sums[num]+s>ans) return; if (res<0 || 2*res/lr+s>ans) return; int i,j,tmp; for (i=lr;i>=num;i--) { tmp=(res-sumv[num-1])/(i*i); if (lh<tmp) tmp=lh; for (j=tmp;j>=num;j--) { if (i*i*j>res) break; if (num==m) dfs(i-1,j-1,res-i*i*j,num-1,s+2*i*j+i*i); else dfs(i-1,j-1,res-i*i*j,num-1,s+2*i*j); } } } int main() { int i; sumv[0]=0;sums[0]=0; for (i=1;i<=20;i++) { sumv[i]=sumv[i-1]+i*i*i; sums[i]=sums[i-1]+2*i*i; } while (scanf("%d%d",&n,&m)!=EOF) { ans=inf; dfs(n,n,n,m,0); if (ans==inf) printf("0\n"); else printf("%d\n",ans); } return 0; }
C HDU 1043
D POJ 1011
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<algorithm> using namespace std; int a[70],vis[70],n,flag; void dfs(int num,int res,int x,int last) { if (num==0 && res==0) { flag=1;return; } if (num==0 || flag) return; if (res==0) res=x,last=n; for (int i=last;i>=1;i--) { if (i!=n && a[i]==a[i+1] && !vis[i+1]) continue; if (a[i]>res || vis[i]) continue; vis[i]=1; dfs(num-1,res-a[i],x,i-1); vis[i]=0; if (res==a[i] || res==x) return; } } int main() { int i,sum; while (scanf("%d",&n)!=EOF && n!=0) { sum=0; for (i=1;i<=n;i++) { scanf("%d",&a[i]); sum+=a[i]; } sort(a+1,a+n+1); for (i=a[n];i<=sum;i++) { if (sum%i!=0) continue; flag=0; dfs(n,i,i,n); if (flag) break; } printf("%d\n",i); } return 0; }
H POJ 1167