luoguP1120 小木棍 [数据加强版]
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e6+5; 4 int n,ans,tot,maxx,mark; 5 int sum,num,len; 6 int a[maxn]; 7 bool v[maxn]; 8 template <class t>void red(t &x) 9 { 10 x=0; 11 int w=1; 12 char ch=getchar(); 13 while(ch<'0'||ch>'9') 14 { 15 if(ch=='-') 16 w=-1; 17 ch=getchar(); 18 } 19 while(ch>='0'&&ch<='9') 20 { 21 x=(x<<3)+(x<<1)+ch-'0'; 22 ch=getchar(); 23 } 24 x*=w; 25 } 26 void input() 27 { 28 freopen("input.txt","r",stdin); 29 //freopen("output.txt","w",stdout); 30 } 31 bool cmp(int x,int y) 32 { 33 return x>y; 34 } 35 void dfs(int cur,int k,int cnt) 36 { 37 //printf("%d %d %d\n",a[cur-1],k,cnt); 38 if(cnt==num) 39 { 40 ans=len; 41 mark=1; 42 return; 43 } 44 if(!k) 45 { 46 47 int i; 48 for(i=1;i<=tot;++i) 49 if(!v[i]) 50 { 51 v[i]=1; 52 break; 53 } 54 //printf("%d\n",cnt+1); 55 dfs(i+1,len-a[i],cnt+1); 56 if(mark) 57 return; 58 v[i]=0; 59 } 60 for(int i=cur;i<=tot;++i) 61 if(!v[i]&&k>=a[i]) 62 { 63 v[i]=1; 64 dfs(i+1,k-a[i],cnt); 65 v[i]=0; 66 if(k==a[i]) 67 return; 68 int j=i+1; 69 while(i<tot&&a[j]==a[i]) 70 i++; 71 if(i==n) 72 return; 73 if(mark) 74 return; 75 } 76 } 77 int main() 78 { 79 //input(); 80 red(n); 81 int x; 82 for(int i=1;i<=n;++i) 83 { 84 red(x); 85 if(x<=50) 86 { 87 a[++tot]=x; 88 maxx=max(maxx,x); 89 sum+=x; 90 } 91 } 92 ans=sum; 93 sort(a+1,a+tot+1,cmp); 94 for(int i=maxx;i<=sum/2;++i) 95 { 96 if(sum%i) 97 continue; 98 //printf("\n%d\n",i); 99 num=sum/i; 100 len=i; 101 mark=0; 102 memset(v,0,sizeof(v)); 103 v[1]=1; 104 dfs(1,len-a[1],1); 105 if(mark) 106 break; 107 } 108 printf("%d",ans); 109 return 0; 110 }