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 }

 

转载于:https://www.cnblogs.com/Stomach-ache/p/3703226.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值