有一个容量为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 the partition number,

the partition number

 

 

法二:

背包求解

大小 < 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)

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

相关文章:

  • 2022-01-18
  • 2021-06-10
  • 2022-01-11
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-20
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
相关资源
相似解决方案