洛谷P1466 集合 Subset Sums

洛谷P1466 集合 Subset Sums
这题可以看成是背包问题
用空间为 1--n 的物品恰好填充总空间一半的空间 有几种方案
01 背包问题
1、注意因为两个交换一下算同一种方案,所以最终 要 f [ v ] / 2
2、要开 long long

 

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <iostream> 
 9 #define ll long long 
10 using namespace std ; 
11 
12 int n,v ; 
13 ll f[801] ; 
14 
15 int main() 
16 {
17     scanf("%d",&n) ;
18     v = n*(n+1) / 2 ; 
19     if(v&1) 
20     {
21         printf("0\n") ;
22         return 0 ; 
23     }  
24     v/=2 ; 
25     f[ 0 ] = 1 ;
26     for(int i=1;i<=n;i++) 
27         for(int j=v;j>=i;j--) 
28             f[ j ] = f[ j-i ] + f[ j ] ; 
29     printf("%lld\n",f[ v ]/2) ;
30     return 0 ; 
31 }

 

posted @ 2017-05-30 23:27  third2333  阅读(173)  评论(0编辑  收藏  举报