UVa 562

 1 /*
 2 
 3 * 泥马,WA了一天,这题应该是很简单的DP,不过就是做不出来,只好改用母函数。。。。。
 4 */
 5 #include<stdio.h>
 6 
 7 #include<math.h>
 8 #include<string.h>
 9 #include<algorithm>
10 using namespace std;
11 #define max 100+5
12 int a[max];
13 int c1[max*50000],c2[max*50000];
14 int m,s;
15 
16 void solve(){
17     memset(c1,0,sizeof(c1));
18     memset(c2,0,sizeof(c2));
19     for(int i=0;i<=a[0];i+=a[0]){
20         c1[i]=1;
21     }
22     for(int i=1;i<m;i++){
23         for(int j=0;j<=s/2;j++){
24             for(int k=0;k+j<=s/2&&k<=a[i];k+=a[i]){
25                 c2[j+k]+=c1[j];
26             }
27         }
28         for(int j=0;j<=s/2;j++){
29             c1[j]=c2[j];
30             c2[j]=0;
31         }
32     }
33 }
34 
35 int main(){
36     int n;
37     scanf("%d",&n);
38     while(n--){
39         s=0;
40         scanf("%d",&m);
41         for(int i=0;i<m;i++){
42             scanf("%d",&a[i]);
43             s+=a[i];
44         }
45         solve();
46         int ans;
47         for(int i=s/2;i>=0;i--){
48             if(c1[i]){
49                 ans=i;
50                 break;
51             }
52         }
53         printf("%d\n",abs(s-ans-ans));
54     }
55 }

 

posted on 2013-07-18 19:42  Stomach_ache  阅读(146)  评论(0)    收藏  举报

导航