Smile

Smile

常用链接

统计

最新评论

usaco subset sums

Subset Sums
JRM

For many sets of consecutive integers from 1 through N (1 <= N <= 39), one can partition the set into two sets whose sums are identical.

For example, if N=3, one can partition the set {1, 2, 3} in one way so that the sums of both subsets are identical:

  • {3} and {1,2}

This counts as a single partitioning (i.e., reversing the order counts as the same partitioning and thus does not increase the count of partitions).

If N=7, there are four ways to partition the set {1, 2, 3, ... 7} so that each partition has the same sum:

  • {1,6,7} and {2,3,4,5}
  • {2,5,7} and {1,3,4,6}
  • {3,4,7} and {1,2,5,6}
  • {1,2,4,7} and {3,5,6}

Given N, your program should print the number of ways a set containing the integers from 1 through N can be partitioned into two sets whose sums are identical. Print 0 if there are no such ways.

Your program must calculate the answer, not look it up from a table.

PROGRAM NAME: subset

INPUT FORMAT

The input file contains a single line with a single integer representing N, as above.

SAMPLE INPUT (file subset.in)

7

OUTPUT FORMAT

The output file contains a single line with a single integer that tells how many same-sum partitions can be made from the set {1, 2, ..., N}. The output file should contain 0 if there are no ways to make a same-sum partition.

SAMPLE OUTPUT (file subset.out)

4

不是正解的方法

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <ctime>
 4 #include <cstdlib>
 5 
 6 typedef long long LLong;
 7 int const max_length = 40;
 8 int arr[max_length];
 9 int used[max_length * max_length];
10 
11 void init(void);
12 int  no_ans(int n);
13 LLong  dfs(LLong *ans, int cur, int n, int sum, int tag, int flg);
14 
15 void init(void){
16     int i;
17 
18     for(i = 0; i < max_length; ++i)
19         arr[i] = i + 1;
20 }
21 
22 int no_ans(int n){
23     return (1 + n) * n  % 4;
24 }
25 
26 LLong dfs(LLong *ans, int cur, int n, int sum, int tag, int flg){
27     if(cur >= n){
28         if(flg && sum <= tag)
29             *ans += used[tag - sum];
30         if(!flg)
31             ++used[sum];
32         return *ans;
33     }
34     dfs(ans, cur + 1, n, sum + arr[cur], tag, flg);
35     dfs(ans, cur + 1, n, sum, tag, flg);
36     return *ans;
37 }
38 
39 int main(void){
40     FILE *fin, *fout;
41     int n;
42     LLong ans = 0LL;
43 
44     fin = fopen("subset.in""r");
45     fout = fopen("subset.out""w");
46 
47     fscanf(fin, "%d"&n);
48     
49     init();
50     fprintf(fout, "%lld\n", no_ans(n) ? 0 : (dfs(&ans, 0, n / 20, (1 + n) * n / 40/ 2, dfs(&ans, n / 2, n, 0, (1 + n) * n / 41/ 2));
51 
52     fclose(fin);
53     fclose(fout);
54 
55     return 0;
56 }

posted on 2011-07-15 21:42 Smile3 阅读(106) 评论(0)  编辑 收藏 引用 所属分类: 未解决题目


只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理