USACO 2.2.2 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

题意:输入数字N,把1~N分成两个集合,使每个集合的和相等。

这题可以用DP解,也可以用母函数来做。初次接触母函数,就用这题练一下手了。

先来介绍一下母函数:

首先来看下这个多项式乘法:

(1+a1x)(1+a2x)…(1+anx)=1+(a1 +a2+an)x+(a1a2+a1a3+…+an-1an)x2+…+a1a1…anxn

由此可以看出:

    1.x的系数是a1,a2,…an 的单个组合的全体。

    2. x^2的系数是a1,a2,…a2的两个组合的全体。
    ………
    n. x^n的系数是a1,a2,….an的n个组合的全体(只有1个)。
进一步得到:

(1+x)n=1+C(n,1)x+C(n,2)x2+…+C(n,n)xn

母函数的定义
对于序列a0,a1,a2,…构造一函数:

G(x)=a0+a1x+a2x2+…称函数G(x)是序列a0,a1,a2,…的母函数。

这里先给出2个例子,等会再结合题目分析:

第一种:

有1克、2克、3克、4克的砝码各一枚,能称出哪几种重量?每种重量各有几种可能方案?

考虑用母函数来解决这个问题:

我们假设x表示砝码,x的指数表示砝码的重量,这样:

    1个1克的砝码可以用函数1+1*x^1表示,

    1个2克的砝码可以用函数1+1*x^2表示,

    1个3克的砝码可以用函数1+1*x^3表示,

    1个4克的砝码可以用函数1+1*x^4表示,

我们拿1+x^2来说,前面已经说过,x表示砝码,x的指数表示砝码的重量!初始状态时,这里就是一个质量为2的砝码。

那么前面的1表示什么?按照上面的理解,1其实应该写为:1*x^0,即1代表重量为2的砝码数量为0个。

所以这里1+1*x^2 = 1*x^0 + 1*x^2,即表示2克的砝码有两种状态,不取或取,不取则为1*x^0,取则为1*x^2

接着讨论上面的1+x^2,这里x前面的系数有什么意义?

这里的系数表示状态数(方案数)

1+x^2,也就是1*x^0 + 1*x^2,也就是上面说的不取2克砝码,此时有1种状态;或者取2克砝码,此时也有1种状态。

几种砝码的组合可以称重的情况,可以用以上几个函数的乘积表示:

(1+x)(1+x^2)(1+x^3)(1+x^4)=(1+x+x^2+x^4)(1+x^3+^4+x^7)

=1 + x + x^2 + 2*x^3 + 2*x^4 + 2*x^5 + 2*x^6 + 2*x^7 + x^8 + x^9 + x^10

从上面的函数知道:可称出从1克到10克,系数便是方案数。

例如右端有2^x^5 项,即称出5克的方案有2种:5=3+2=4+1;同样,6=1+2+3=4+2;10=1+2+3+4。

故称出6克的方案数有2种,称出10克的方案数有1种 。


第二种:

求用1分、2分、3分的邮票贴出不同数值的方案数:

大家把这种情况和第一种比较有何区别?第一种每种是一个,而这里每种是无限的。

G(x)=(1+x+x2+…)(1+x2+x4+…)(1+x3+x6+…)

以展开后的x^4为例,其系数为4,即4拆分成1、2、3之和的拆分方案数为4;

即 :4=1+1+1+1=1+1+2=1+3=2+2

这个题目其实是第一种的一个模型,模拟(1+x)(1+x^2)(1+x^3)(1+x^4)的计算过程就可以了。

源代码:

/*
ID: supersnow0622
PROG: subset
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include<memory.h>
using namespace std;
int half;
long long c1[500],c2[500],c[500];
int main() {
    ofstream fout ("subset.out");
    ifstream fin ("subset.in");
    int N,sum;
    fin>>N;
    sum=(1+N)*N/2;
    if(sum%2!=0)
     fout<<0;
    else
    {
      half=sum/2;
      c1[0]=c1[1]=1;

      for(int i=2;i<=N;i++)
        {
         memset(c2,0,sizeof(c2));
         memset(c,0,sizeof(c));
         c2[0]=c2[i]=1;
         for(int j=0;j<=half;j++)
          {
            c[j+0]+=c1[j]*c2[0];
            c[j+i]+=c1[j]*c2[i];
          }
         for(int k=0;k<=half;k++)
          c1[k]=c[k];
        }
      fout<<c1[half]/2;
    }
    fout<<endl;
    return 0;
}






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值