标签:
思路还是很简单的,不过关键在于剪枝,用了几个不强力的剪枝,飘过~~~998ms
#include<iostream> #include<algorithm> #include<cstring> #define maxn 65+5 using namespace std; int maxx,n,m,flag,l; int mapp[maxn]; int visit[maxn]; bool cmp(int x,int y) { return x>y; } void dfs(int x,int sum,int d) { if(sum==m/l||l==m)//不强力 { flag=1;return; } for(int i=x;i<n;i++) { if(!visit[i]&&d+mapp[i]<=l) { visit[i]=1; if(d+mapp[i]==l) dfs(x+1,sum+1,0); else dfs(x,sum,d+mapp[i]); visit[i]=0; if(flag||!d) return; } } } int main() { cin.sync_with_stdio(false); while(cin>>n&&n) { maxx=-1;m=0;flag=0; for(int i=0;i<n;i++) { cin>>mapp[i]; maxx=max(maxx,mapp[i]); m+=mapp[i]; } sort(mapp,mapp+n,cmp);//不强力 for(int i=maxx;i<=m;i++) { fill(visit,visit+n,0); if(m%i==0) l=i,dfs(0,0,0);//不强力 if(flag) { cout<<i<<endl;break; } } } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/45935737