the partition number

有一个容量为n的背包,有1, 2, 3, ..., n这n种物品,每种物品可以无限使用,求装满的方案数。

法一:

http://mathworld.wolfram.com/PartitionFunctionP.html:

Euler invented a generating function which gives rise to a recurrence equation in P(n),

 P(n)=sum_(k=1)^n(-1)^(k+1)[P(n-1/2k(3k-1))+P(n-1/2k(3k+1))]

 

 

法二:

背包求解

大小 < sqrt(n) 的物品,只有sqrt(n)种,用普通的完全背包可以在O(n ** 1.5)的时间复杂度内解决。

大小 >= sqrt(n) 的物品,只会选sqrt(n)个,设k = sqrt(n), f[i][j]表示选了i个物品,和为j的方案

则f[i][j] = f[i-1][j-k] + f[i][j-i](第一种是加入了一个大小为k的物品,第二种是所有物品重量+1)

 这种方法常数略大(是我写得丑吗)

posted @ 2016-01-13 21:12  Showson  阅读(152)  评论(0编辑  收藏  举报