找到没有。对获得一笔n,其中所有的正整数小于n方式有的、方式、正整数

2023-09-11 03:02:22 作者:我给你的爱已停机、

对于给定的数n,说2有多少种方法可以得到一笔2使用数字少于2。

  1 + 1 = 2
因此,对于2  - 仅1路。

N = 3
1 + 1 + 1 = 3
1 + 2 = 3
所以,对于3  - 它是2种方式
N = 4
1 + 1 + 1 + 1 = 4
1 + 1 + 2 = 4
1 + 3 = 4
2 + 2 = 4

所以,为4  - 它是4种方式
 

是否会有一个通用的模式/解决这个问题吗?

解决方案

这称为划分问题你可以看到详细的引用链接。 从维基:

  

获得分区上的功能的处理的一种方法涉及到一个   中间函数P(K,N),从而重新presents数量   仅使用自然数至少一样大k×n个的分区。对于   k的任一给定值,P(K,N)计算分区融入完全   下列类别之一:

 最小的加数为k
最小的加数是严格大于k更大。
 
一文看懂主流P2P平台资产端 这类资产占据绝对C位

     

满足第一条件分区的数目为p(K,N - k)的。   看到这一点,想象中的数n的所有分区的列表 - K   成规模至少为k的数字,那么试想追加+ K给每个   分区中的列表。现在是什么名单?作为一个侧面说明,人们   可以用这种方法来定义一种递归关系的分区   中间功能的长期功能,即

  1 +总和{k = 1至地面(1/2)N} P(K,NK)= P(N),
 

     

满足第二条件分区的数目为p第(k + 1,n)的   因为分区成至少k的部分,其中包含的任何部件   k个必须拥有所有部分至少K + 1。

     的

由于两个条件是相互排斥的,数   满足任一条件的分区为p第(k + 1中,n)+ P(K,N - k)的。该   递归定义的函数是这样的:

  P(K,N)= 0,如果K> ñ

P(K,N)= 1,如果k = N

P(K,N)= P(K + 1中,n)+ P(K,N  -  k)的其他方式。
 

事实上,你可以计算出记忆化的所有值,从额外的递归调​​用prevent。

编辑:作为unutbu在他的评论中提到,最后你应该减去结果从1到输出的话,其实所有的 P 值将被计算为维基链接,但到底什么时候你想要的输出结果,你应该把 1 减去它。

For a given number n, say 2 how many ways can we get a sum 2 using numbers less that 2.

1+1 = 2  
so, for 2 - just 1 way.

n = 3   
1+1+1=3  
1+2=3  
so,for 3 - it is 2 ways  
n = 4   
1+1+1+1=4  
1+1+2=4  
1+3=4  
2+2=4  

so, for 4 - it is 4 ways  

Can there be a generic pattern/solution to this question?

解决方案

This known as Partition Problem and you can see detail in referenced link. from wiki:

One way of getting a handle on the partition function involves an intermediate function p(k, n), which represents the number of partitions of n using only natural numbers at least as large as k. For any given value of k, partitions counted by p(k, n) fit into exactly one of the following categories:

smallest addend is k
smallest addend is strictly greater than k.

The number of partitions meeting the first condition is p(k, n − k). To see this, imagine a list of all the partitions of the number n − k into numbers of size at least k, then imagine appending "+ k" to each partition in the list. Now what is it a list of? As a side note, one can use this to define a sort of recursion relation for the partition function in term of the intermediate function, namely

1+ sum{k=1 to floor (1/2)n} p(k,n-k) = p(n),

The number of partitions meeting the second condition is p(k + 1, n) since a partition into parts of at least k that contains no parts of exactly k must have all parts at least k + 1.

Since the two conditions are mutually exclusive, the number of partitions meeting either condition is p(k + 1, n) + p(k, n − k). The recursively defined function is thus:

p(k, n) = 0 if k > n

p(k, n) = 1 if k = n

p(k, n) = p(k+1, n) + p(k, n − k) otherwise.

In fact you can calculate all values by memoization, to prevent from extra recursive calls.

Edit: As unutbu mentioned in his comment, at last you should subtract result from 1 to output it, in fact all the P values will be calculated as wiki link, but in the end when you want output result, You should subtract it by 1.