简体   繁体   English

如何找到选择3种类型的k个对象的方式数量

[英]how to find the number of ways of choosing k objects of 3 types

You are given an integer k and 3 types of objects - A, B and C each type has a large supply of objects. 给您一个整数k和3种类型的对象-A,B和C,每种类型都有大量的对象。 In how many ways can u arrange k objects so that you can choose any type of object any number of times with one restriction that B always comes between A and C or C and A. 您可以用多少种方式排列k对象,以便您可以选择任意次数的任何类型的对象,但要限制B始终位于A和C之间,或者C和A之间。

for example if, k is 3, then the answer is : 10 explanation : {AAA, AAC, ABC, ACA, ACC, CAA, CAC, CBA, CCA, CCC} 例如,如果k为3,则答案为:10解释:{AAA,AAC,ABC,ACA,ACC,CAA,CAC,CBA,CCA,CCC}

I tried to solve it using dynamic programming, I don't know if that's the correct approach, though. 我试图使用动态编程来解决它,但是我不知道这是否是正确的方法。

I tried something like this : f(n) = 2*f(n-1) + f(n-2) , where you take an array of size k and f(1) = 2 {A, C} and f(2) = 4 {AA, AC, CA, CC} , 我尝试过这样的事情: f(n) = 2*f(n-1) + f(n-2) ,其中您采用了大小为k的数组,而f(1) = 2 {A, C}f(2) = 4 {AA, AC, CA, CC}

so if(n-1)th alphabet is A or C, then nth alphabet can A or C, but if it's B, then nth alphabet can only be the complementary aplphabet ie, if (n-2)th letter is A, then nth letter is C or vice versa. 因此,如果第(n-1)个字母是A或C,那么第n个字母可以是A或C,但是如果它是B,则第n个字母只能是互补的全名,即,如果第(n-2)个字母是A,则第n个字母为C,反之亦然。 But I feel like I'm missing some cases. 但是我觉得我想念一些案件。

Is there a better way, can someone help me with this? 有没有更好的方法,有人可以帮助我吗?

[Edited] 将帖子

Recursion: 递归:

F(1) = 2
F(2) = 4
F(N) = 2 * F(N - 1) + F(N-2)

explanation: any valid combination ends with A or C. 说明:任何有效的组合都以A或C结尾。
To make n-combination, we can add both A and C to any (n-1)-combination - two variants. 要进行n组合,我们可以将A和C都添加到任何(n-1)组合中-两个变体。
And we have one more variant, adding to (n-2) combinations: BA for xxxxC and BC for xxxxA 我们还有一个变体,增加了(n-2)种组合:xxxxC的BA和xxxxA的BC

Let L be the language of such strings, and La and Lc be the subsets of the language where the strings end with A or C respectively. 令L为此类字符串的语言,而La和Lc为字符串分别以A或C结尾的语言的子集。

Then we can write this unambiguous grammar: 然后,我们可以编写明确的语法:

L = La | Lc
La = "A" | L + "A" | Lc + "BA"
Lc = "C" | L + "C" | La + "BC"

Let L(n), La(n), Lc(n) be the number of strings of length n in each of the three languages. 令L(n),La(n),Lc(n)为三种语言中每种语言的长度为n的字符串数。 By symmetry, La(n) = Lc(n) = L(n)/2. 通过对称,La(n)= Lc(n)= L(n)/ 2。

Then, from the second equation and using the fact that the grammar is unambiguous, for n > 1, La(n) = L(n-1) + Lc(n-2), and substituting we get: L(n) = 2L(n-1) + L(n-2). 然后,从第二个方程式中,使用语法无歧义的事实,对于n> 1,La(n)= L(n-1)+ Lc(n-2),然后代入:L(n)= 2L(n-1)+ L(n-2)。 We have L(0)=0 and L(1)=2. 我们有L(0)= 0和L(1)= 2。

To compute L, we can write iterative code (which you might call dynamic programming): 为了计算L,我们可以编写迭代代码(您可以将其称为动态编程):

def L(n):
    a, b = 0, 2
    for _ in xrange(n):
        a, b = b, a + 2 * b
    return a

Or, like we do for the Fibonacci series, we can solve it using matrix exponentiation, which can be done in O(log n) arithmetic operations. 或者,就像我们对斐波那契数列所做的那样,我们可以使用矩阵求幂来解决它,这可以在O(log n)算术运算中完成。

L(n) = the first component of [0 1]^n (0)
                              [1 2]   (2)

This is a generalised solution. 这是一个广义的解决方案。 Say you have n number of objects and you have to choose k objects out of them. 假设您有n个对象,并且必须从其中选择k对象。 You can choose x1,x2,x3.. objects from each type. 您可以从每种类型中选择x1,x2,x3..对象。 And their sum 和他们的总和

x1+x2+x3...+xn=k 0<=xi

Hence, provided you can select each object or not ( 0<=xi ), the answer will be (k+n-1)C(n-1) 因此,假设您可以选择是否选择每个对象( 0<=xi ),答案将是(k+n-1)C(n-1)

This is a simple solution of an integral equation 这是积分方程的简单解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到从 k 个子集中选择一个的组合数 - How to find number of combinations of choosing one from k subsets 如何计算从列表L(所有子串的列表)中选择k个相等子串的方式的数量 - How to count the number of ways of choosing of k equal substrings from a List L(the list of All Substrings) 如何找到给定大小n的聚类方式的数目和k的聚类数目的递归公式? - How to find the recurrence formula for the number of ways of clustering given size n and number of clusters to be k? 使用范围 1 到 k 查找总和值的方法数 - Find the number of ways to find the total sum value using the range 1 to k 从 n 个元素的数组中找出形成元素 K 的方法数 - Find the number of ways to form an element K from an array of n elements 以k表示选择簇数 - Choosing number of clusters in k means 将 n 个对象分成 k 个组的方法有多少种,使得没有一个组的对象比以前形成的组少? - Number of ways to divide n objects in k groups, such that no group will have fewer objects than previously formed groups? C程序,用于计算从n个不同对象中选择k个对象的方法数。 &#39;k&#39;和&#39;n&#39;都是整数 - C program to calculates the number of ways to choose k objects from n distinct objects. 'k' and 'n' both are integers 选择比较算法以找到k个最大值 - Choosing comparing algorithms to find k max values 找到多种生成数字的方法 - Find a Number of ways to Generate a Number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM