USACO 2.2 Subset Sums(水DP)

纠结了几次INT64和longlong,CE了几次。。。

 1 /*

 2 ID: cuizhe

 3 LANG: C++

 4 TASK: subset

 5 */

 6 #include <cstdio>

 7 #include <cstring>

 8 #include <cmath>

 9 #include <algorithm>

10 using namespace std;

11 long long dp[50][1001];

12 int main()

13 {

14     int i,j,n,sum;

15     freopen("subset.in","r",stdin);

16     freopen("subset.out","w",stdout);

17     scanf("%d",&n);

18     for(i = 1;i <= n;i ++)

19     dp[i][0] = 1;

20     sum = (1+n)*n/2;

21     if(sum%2)

22     {

23         printf("0\n");

24         return 0;

25     }

26     sum = sum/2;

27     dp[1][1] = 1;

28     for(i = 2;i <= n;i ++)

29     {

30         for(j = 1;j <= sum;j ++)

31         dp[i][j] = dp[i-1][j];

32         for(j = i;j <= sum;j ++)

33         {

34             dp[i][j] += dp[i-1][j-i];

35         }

36     }

37     printf("%lld\n",dp[n][sum]/2);

38     return 0;

39 }

你可能感兴趣的:(USACO)