1. 程式人生 > >洛谷P1466 集合 Subset Sums

洛谷P1466 集合 Subset Sums

man using 註意 總空間 cstring ostream 方案 amp lld

洛谷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 }

洛谷P1466 集合 Subset Sums