|
-
February 29th, 2016, 02:34 AM
#7
Re: How to distribute a value into vector
 Originally Posted by Ramees219
Because if we input bigger value instead of 3 and 5 like 8 , 15 , its going to take lots of time .
and if i input v = 15 , D= 85 , number of sets = 0
The use of bitsets puts a limitation on my solution indeed. It limits V+D to be no bigger than 62 because larger numbers will overflow the long long int used to represent the bitsets. To overcome this the bitset way of generating subsets will have to be replaced by some other subset generating algorithm.
Yes, this algorithm will generate all subsets and thus produce some with zeroes. My suggestion is to accept that and to simply just skip such subsets. Gosper's hack is a very fast algorithm and so skipping unwanted subsets may not even be slower than other approaches that may avoid such subsets altogether.
But okay, for some selections of V and D there will be lots of combinations with zeroes and if you want to keep searching for better fitting solutions I suggest you use the key phrase "restricted integer composition". I may give it a shot the next time I feel an urge for some recreational programming. 
Finally here's a function I'm using to calculate the number of combinations with my approach,
Code:
inline long long int bs_binominal(int n, int k) { // the binominal n over k
if (k>n) return 0;
if (k == n) return 1;
long long int f = n;
for (int i = 2; i <= k; ++i) f = (f * (n + 1 - i)) / i;
return f;
}
//
std::cout << "Expected number of combinations: " << bs_binominal(V+D-1, V-1) << std::endl;
Well, that was my contribution for now. Good luck!
Last edited by tiliavirga; February 29th, 2016 at 06:50 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|