[USACO]Subset Sums

13 篇文章 0 订阅
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
 
解题:
典型01背包问题。
首先问题可以做初步简化:
对于N%4==1或N%4==2时,总和为奇数,不存在二等分。对于N%4==0或3的N的二等分子序列对的个数等同于从1至N-1中选取子序列其和为N*(N+1)/4-N,这个很好理解。这就转换成了典型的01背包问题。
令f(n,sum)表示1-n序列中子序列和为sum的子序列数。则有:
f(n,sum)=f(n-1,sum)+f(n-1,sum-n);
上面公式只是一个大体的框子,有很多细节需要考虑。
当n==sum时,f(n,sum)=f(n-1,sum)+1;
当n>sum时,f(n,sum)=f(n-1,sum);
若不想增加这么多判断分支,则可以设定初始值f(n,0)=1,如何理解这个初始值呢?即对于任意1-n序列,必然存在一个且唯一的子序列(空集)的和为0.
用递归解必然会超时,复杂度和搜索+剪枝相同为O(2^n)。最终的解题为非递归(循环)、借助一维数组来完成。这个需要好好的考虑一下。
下列出时间复杂度空间复杂度分别为O(N^3)和O(N^2)的解法:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class subset {
    public static void main(String[] args) throws IOException,FileNotFoundException {
    BufferedReader br = new BufferedReader(new FileReader("subset.in"));
    FileWriter fout = new FileWriter("subset.out");
    int N = Integer.parseInt(br.readLine());
    int[] V = new int[N*(N+1)/4-N+1];
    V[1]=1;
    if((N&3)==1||(N&3)==2)
        fout.write("0\n");
    else {
        for(int i = 2;i<N;i++) {
            for(int v = V.length-1;v>0;v--) {
                if(v>i) 
                    V[v] = V[v]+V[v-i];
                else if(v==i)
                    V[v]++;
                else
                    break;
            }
        }
       fout.write(V[V.length-1]+"\n");
    }
    System.out.println(V[V.length-1]);
    fout.flush();
    fout.close();
    br.close();
    System.exit(0);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值