combinatorics

Calculate multinomial coefficient

烂漫一生 提交于 2019-12-04 15:43:49
I want to calculate multinomial coefficient mod 1e9 + 7. It equals: n! / (k0! * k1! * k2 * ... * km!) In my case m = 3, k0 + k1 + k2 = n, so it would be: n! / (k0! * k1! * k2!) My code for this: .... long long k2 = n - k1 - k0; long long dans = fact[n] % MOD; long long tmp = fact[i] % MOD; tmp = (tmp * fact[j]) % MOD; tmp = (tpm * fact[k]) % MOD; res = (fact[n] / tmp) % MOD; // mb mistake is here... cout << res; fact[i] - factorial of i mod 1e9+7 It does not work on big tests I hope I'm not linkfarming here, but here is a process of work, to solve your problem : Naive implementations will

An efficient method to generate all possible ways to pair up items in a data set

让人想犯罪 __ 提交于 2019-12-04 15:41:32
This is somewhat of a combinatorial problem; I'm trying to figure out an efficient way to pair up all items in a data set. For example, I have an array of length 6: [1,2,3,4,5,6], and I want to make all possible pairings of the contents in the array as so: [1,2],[3,4],[5,6] [1,2],[3,5],[4,6] [1,2],[3,6],[4,5] [1,3],[2,4],[5,6] [1,3],[2,5],[4,6] ... and so on. In this example, there would be 15 combinations total. In general, the number of possibilities in this solution should be (N-1)!! where N is the length of the array or the number of items to be paired up. N will always be even in this

Finding Strings Neighbors By Up To 2 Differing Positions

北城以北 提交于 2019-12-04 13:59:33
Given a seed string, I want to find its neighbors with at most differ in 2 positions. All the digits involve in generating string are only four (i.e. 0,1,2,3). This is the example for what I mean: # In this example, 'first' column # are neighbors with only 1 position differ. # The rest of the columns are 2 positions differ Seed = 000 100 110 120 130 101 102 103 200 210 220 230 201 202 203 300 310 320 330 301 302 303 010 011 012 013 020 021 022 023 030 031 032 033 001 002 003 Seed = 001 101 111 121 131 100 102 103 201 211 221 231 200 202 203 301 311 321 331 300 302 303 011 010 012 013 021 020

Counting combinations of pairs of items from multiple lists without repetition

…衆ロ難τιáo~ 提交于 2019-12-04 13:54:36
Given a scenario where we have multiple lists of pairs of items, for example: {12,13,14,23,24} {14,15,25} {16,17,25,26,36} where 12 is a pair of items '1' and '2' (and thus 21 is equivalent to 12), we want to count the number of ways that we can choose pairs of items from each of the lists such that no single item is repeated. You must select one, and only one pair, from each list. The number of items in each list and the number of lists is arbitrary, but you can assume there are at least two lists with at least one pair of items per list. And the pairs are made from symbols from a finite

Given an array, how to generate all combinations of subset size k?

Deadly 提交于 2019-12-04 13:27:45
So given input = [1, 2, 3] and k=2 this would return: 1 2 1 3 2 1 2 3 3 1 3 2 This is the closest to what I am looking for, but not quite: http://algorithms.tutorialhorizon.com/print-all-combinations-of-subset-of-size-k-from-given-array/ function subsetsOfSize(a, used, startIndex, currentSize, k) { if (currentSize === k) { for (var i = 0; i < a.length; i++) { if (used[i]) console.log(a[i]); } console.log('-'); return; } if (startIndex === a.length) return; used[startIndex] = true; subsetsOfSize(a, used, startIndex+1, currentSize+1, k); used[startIndex] = false; subsetsOfSize(a, used,

Algorithm for Grouping

a 夏天 提交于 2019-12-04 10:58:59
I am trying to help someone write a program that I thought would be easy, but of course it never is :) I am trying to take a class roster (usually between 10-20 students) and effectivly uniquely pair off each classmate to another to make unique groups. Therefore in a class of 10 people, you can have 9 groups. It needs to be able to handle odd number of students too, adding to my confusion. I was looking at doing this in Java, but that is flexible. Any ideas on an algorithmic way to guarantee a)not infinite looping (ending with 2 people who have already been partners) and b) I am aiming for

Custom permutation, Equal distribution of pairs

柔情痞子 提交于 2019-12-04 10:29:04
I've been playing with a strange problem for a few weeks and can't seem to get the results I want. I'd like to take a permutation of a list of objects to get unique pairs. Then order them in a particular way to maximize equal distribution of the objects at any point in the list. This also means that if an object is at the beginning of a pair if should also be at the end of a pair soon after. No pairs can repeat. To clarify, here is an example. list (A,B,C,D) might result in the following: (A,B) (C,D) (B,A) (D,C) (A,C) (B,D) (C,A) (D,B) (A,D) (B,C) (D,A) (C,B) Notice, every letter is used every

Algorithm for generating “anti-Gray” on-demand combinations of k elements from n

六眼飞鱼酱① 提交于 2019-12-04 09:57:19
I'm trying to implement an algorithm to get all combinations of k elements out of a set of n elements where the difference between two consecutive combinations are maximized (so kind of reverse Gray codes). In other words, the combinations should be ordered to avoid elements from appearing twice in a row, and so that no element is unnecessarily discriminated. Ideally, the algorithm would also NOT pre-calculate all combinations and store them into memory, but rather deliver combinations on demand. I have searched extensively for this and found a few detailed answers such as https:/

Write a faster combinatorics algorithm

假如想象 提交于 2019-12-04 09:50:28
I'm trying to write a combinatorics algorithm to get all the possible combinations of k out of n without repetitions. The formula is: n!/(k!(n-k)!)); The results end up in an array. What I've actually written is this: function Factorial($x) { if ($x < 1) { echo "Factorial() Error: Number too small!"; ) $ans = 1; for ($xx = 2; $xx >= $x; $xx++) { $ans = $ans * $xx; } return($ans); } function Combination($selectcount,$availablecount) { $ans = Factorial($availablecount) / ( Factorial($availablecount - $selectcount) * Factorial($selectcount) ); return ($ans); } Is this the fastest way to

Minimum Tile Ordering

女生的网名这么多〃 提交于 2019-12-04 09:43:27
问题 Minimizing Tile Re-ordering Problem: Suppose I had the following symmetric 9x9 matrix, N^2 interactions between N particles: (1,2) (2,9) (4,5) (4,6) (5,8) (7,8), These are symmetric interactions, so it implicitly implies that there exists: (2,1) (9,2) (5,4) (6,4) (8,5) (8,7), In my problem, suppose they are arranged in matrix form, where only the upper triangle is shown: t 0 1 2 (tiles) # 1 2 3 4 5 6 7 8 9 1 [ 0 1 0 0 0 0 0 0 0 ] 0 2 [ x 0 0 0 0 0 0 0 1 ] 3 [ x x 0 0 0 0 0 0 0 ] 4 [ x x x 0 1