combinations

Calculate value of n choose k [closed]

寵の児 提交于 2019-12-17 08:21:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . What is the most efficient method to evaluate the value of n choose k ? The brute force way I think would be to find n factorial / k factorial / (n-k) factorial . A better strategy may be to use dp according to this recursive formula. Is there any other better method to evaluate n choose k ? 回答1: You could use

Unordered combinations in R

人走茶凉 提交于 2019-12-17 07:40:08
问题 I am looking a function that return me all the unordered combination of a vector. eg x<-c('red','blue','black') uncomb(x) [1]'red' [2]'blue' [3]'black' [4]'red','blue' [5]'blue','black' [6]'red','black' [7]'red','blue','black' I guess that there is a function in some library that do this, but in can't find it. I am trying with permutations of gtool but it is not the function i am looking for. 回答1: You could apply a sequence the length of x over the m argument of the combn() function. x <- c(

How can a KeyListener detect key combinations (e.g., ALT + 1 + 1)

二次信任 提交于 2019-12-17 03:41:42
问题 How can I let my custom KeyListener listen for combinations of ALT (or CTRL for that matter) + more than one other key? Assume I have 11 different actions I want the application to do, depending on a combination of keys pressed. ALT + 0 - ALT + 9 obviously don't pose any problems, whereas for ALT + 1 + 0 (or "ALT+10" as it could be described in a Help file or similar) I cannot find a good solution anywhere on the web (or in my head). I'm not convinced that this solution with a timer is the

Combination Generator in Linq

这一生的挚爱 提交于 2019-12-17 03:37:50
问题 Is it possible to create some Linq that generates a List containing all possible combinations of a series of numbers?? If you enter "21" it would generate a list with the elements: list[0] = "21" list[1] = "22" list[2] = "11" list[3] = "12" (Not nessesarily in that order) I understand you can use range to do things like: List<char> letterRange = Enumerable.Range('a', 'z' - 'a' + 1).Select(i => (Char)i).ToList(); //97 - 122 + 1 = 26 letters/iterations Which generates the alphabet from a-z. But

Generate list of all possible combinations of elements of vector

故事扮演 提交于 2019-12-17 01:57:46
问题 I am trying to generate all possible combinations of 0 and 1's in a vector of length 14. Is there an easy way of getting that output as a list of vectors, or even better, a dataframe? To demonstrate better what I am looking for, let's suppose that I only want a vector of length 3. I would like to be able to generate the following: (1,1,1), (0,0,0), (1,1,0), (1,0,0), (1,0,1), (0,1,0), (0,1,1), (0,0,0) Any help would be appreciated! Thanks, 回答1: You're looking for expand.grid . expand.grid(0:1,

R: Permutations and combinations with/without replacement and for distinct/non-distinct items/multiset

百般思念 提交于 2019-12-17 01:46:50
问题 In this thread, I am trying to include all the commonly asked questions and their answers here. I hope this will be useful for someone. General question : How to generate sequences of r objects from n objects? combination vs permutation. with replacement vs without replacement. distinct items vs non-distinct items (multisets). There are in total 2^3=8 questions of this type. [Update] Josh O'Brien suggests that the 8 questions are related to twelvefold way. Indeed, the "distinct" questions are

R: Permutations and combinations with/without replacement and for distinct/non-distinct items/multiset

浪子不回头ぞ 提交于 2019-12-17 01:45:12
问题 In this thread, I am trying to include all the commonly asked questions and their answers here. I hope this will be useful for someone. General question : How to generate sequences of r objects from n objects? combination vs permutation. with replacement vs without replacement. distinct items vs non-distinct items (multisets). There are in total 2^3=8 questions of this type. [Update] Josh O'Brien suggests that the 8 questions are related to twelvefold way. Indeed, the "distinct" questions are

Java: Simple Combination of a set of element of k order

荒凉一梦 提交于 2019-12-14 04:26:48
问题 I have a set of numbers {'1','13','25','32','49',...} , i want to calculate all possible combinations of this numbers of order k. Esample1: set = {'1','5','23','41,'54','63'}; k = 4; Output1: 1 5 23 41 1 5 23 54 1 5 23 63 1 5 41 54 1 5 41 63 1 5 54 63 1 23 41 54 1 23 41 63 1 23 54 63 1 41 54 63 5 23 41 54 5 23 41 63 5 23 54 63 5 41 54 63 23 41 54 63 Example2: set = {'a','v','f','z'}; k=3; Output2: a v f a v z a f z v f z in Java plaese. Thank you! 回答1: You should be able to find an

Algorithm for Combinations of given numbers with repetition? C++

痴心易碎 提交于 2019-12-14 03:54:20
问题 So I N - numbers I have to input, and I got M - numbers of places for those numbers and I need to find all combinations with repetition of given numbers. Here is example: Let's say that N is 3(I Have to input 3 numbers), and M is 4. For example let's input numbers: 6 11 and 533. This should be result 6,6,6,6 6,6,6,11 6,6,6,533 6,6,11,6 ... 533,533,533,533 I know how to do that manualy when I know how much is N and M: In example where N is 3 and M is 4: int main() { int N = 3; int M = 4; int

Given an array with integer 0 to N, how many ways to arrange it such that array[i] cannot be i

非 Y 不嫁゛ 提交于 2019-12-14 03:53:10
问题 Given an array with integer 0 to N, how many ways to arrange it such that at position i of the array, we cannot have i inserted in it? For example, N = 2 The following arrangements is valid: 1,2,0 2,0,1 Thus, the answer is 2 arrangements I can't think of a non-brute force method to do this in O(1) time, can anyone help me out? 回答1: Such kind of permutations is called derangement . Wiki page contains a lot of formulas to count them. For example, recurrence: !n=(n-1)(!(n-1)+!(n-2)) where !n ,