combinatorics

Finding the index of a given permutation

此生再无相见时 提交于 2019-11-27 06:10:59
问题 I'm reading the numbers 0, 1, ..., (N - 1) one by one in some order. My goal is to find the lexicography index of this given permutation, using only O(1) space. This question was asked before, but all the algorithms I could find used O(N) space. I'm starting to think that it's not possible. But it would really help me a lot with reducing the number of allocations. 回答1: Considering the following data: chars = [a, b, c, d] perm = [c, d, a, b] ids = get_indexes(perm, chars) = [2, 3, 0, 1] A

Code-golf: generate pascal's triangle

你说的曾经没有我的故事 提交于 2019-11-27 05:21:41
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Generate a list of lists (or print, I don't mind) a Pascal's Triangle of size N with the least lines of code possible! Here goes my attempt (118 characters in python 2.6 using a trick): c,z,k=locals,[0],'_[1]' p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)] Explanation: the

Concatenate values of n arrays in php

萝らか妹 提交于 2019-11-27 05:20:30
I have an unknown number of arrays, each containing an unknown number of words. I want to concatenate the values from each list so that all possible variations of the words are stored to a final array. For example, if array 1 contains: dog cat and array 2 contains: food tooth and array 3 contains: car bike I'd like the output to be: dog food car dog food bike dog tooth car dog tooth bike cat food car cat food bike cat tooth car cat tooth bike There could be more than 3 lists, and each list will most likely have more than 2 words. I'd like to do this in PHP. I know how to do it if I know the

How to generate all the permutations of a multiset?

给你一囗甜甜゛ 提交于 2019-11-27 04:53:17
A multi-set is a set in which all the elements may not be unique.How to enumerate all the possible permutations among the set elements? ruggero Generating all the possible permutations and then discarding the repeated ones is highly inefficient. Various algorithms exist to directly generate the permutations of a multiset in lexicographical order or other kind of ordering. Takaoka's algorithm is a good example, but probably that of Aaron Williams is better http://webhome.csc.uvic.ca/~haron/CoolMulti.pdf moreover, it has been implemented in the R package ''multicool''. Btw, if you just want the

How to generate permutations of a list without “reverse duplicates” in Python using generators

≯℡__Kan透↙ 提交于 2019-11-27 04:35:08
问题 This is related to question How to generate all permutations of a list in Python How to generate all permutations that match following criteria : if two permutations are reverse of each other (i.e. [1,2,3,4] and [4,3,2,1]), they are considered equal and only one of them should be in final result . Example: permutations_without_duplicates ([1,2,3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] I am permuting lists that contain unique integers. The number of resulting permutations will be high so I'd like to

Bit hack to generate all integers with a given number of 1s

假装没事ソ 提交于 2019-11-27 04:17:22
I forgot a bit hack to generate all integers with a given number of 1s. Does anybody remember it (and probably can explain it also)? sehe From Bit Twiddling Hacks Update Test program Live On Coliru #include <utility> #include <iostream> #include <bitset> using I = uint8_t; auto dump(I v) { return std::bitset<sizeof(I) * __CHAR_BIT__>(v); } I bit_twiddle_permute(I v) { I t = v | (v - 1); // t gets v's least significant 0 bits set to 1 // Next set to 1 the most significant bit to change, // set to 0 the least significant ones, and add the necessary 1 bits. I w = (t + 1) | (((~t & -~t) - 1) >> (_

all permutations of a binary sequence x bits long

久未见 提交于 2019-11-27 04:04:32
I would like to find a clean and clever way (in python) to find all permutations of strings of 1s and 0s x chars long. Ideally this would be fast and not require doing too many iterations... So, for x = 1 I want: ['0','1'] x =2 ['00','01','10','11'] etc.. Right now I have this, which is slow and seems inelegant: self.nbits = n items = [] for x in xrange(n+1): ones = x zeros = n-x item = [] for i in xrange(ones): item.append(1) for i in xrange(zeros): item.append(0) items.append(item) perms = set() for item in items: for perm in itertools.permutations(item): perms.add(perm) perms = list(perms)

How to find pair with kth largest sum?

北城以北 提交于 2019-11-27 04:02:58
问题 Given two sorted arrays of numbers, we want to find the pair with the kth largest possible sum. (A pair is one element from the first array and one element from the second array). For example, with arrays [2, 3, 5, 8, 13] [4, 8, 12, 16] The pairs with largest sums are 13 + 16 = 29 13 + 12 = 25 8 + 16 = 24 13 + 8 = 21 8 + 12 = 20 So the pair with the 4th largest sum is (13, 8). How to find the pair with the kth largest possible sum? Also, what is the fastest algorithm? The arrays are already

How to calculate the index (lexicographical order) when the combination is given

萝らか妹 提交于 2019-11-27 03:41:05
I know that there is an algorithm that permits, given a combination of number (no repetitions, no order), calculates the index of the lexicographic order. It would be very useful for my application to speedup things... For example: combination(10, 5) 1 - 1 2 3 4 5 2 - 1 2 3 4 6 3 - 1 2 3 4 7 .... 251 - 5 7 8 9 10 252 - 6 7 8 9 10 I need that the algorithm returns the index of the given combination. es: index( 2, 5, 7, 8, 10 ) --> index EDIT: actually I'm using a java application that generates all combinations C(53, 5) and inserts them into a TreeMap. My idea is to create an array that

Calculating Binomial Coefficient (nCk) for large n & k

旧时模样 提交于 2019-11-27 00:19:40
问题 I just saw this question and have no idea how to solve it. can you please provide me with algorithms , C++ codes or ideas? This is a very simple problem. Given the value of N and K, you need to tell us the value of the binomial coefficient C(N,K). You may rest assured that K <= N and the maximum value of N is 1,000,000,000,000,000. Since the value may be very large, you need to compute the result modulo 1009. Input The first line of the input contains the number of test cases T, at most 1000.