Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're looking for the <a href="http://en.wikipedia.org/wiki/Stars_and_bars_%28combinatorics%29" rel="nofollow">Stars and Bars</a> formula. Positions of array A are the bins; into each position the corresponding element from A and any number of elements from B will be inserted. (Which elements are chosen is already determined by their original order, so we treat them as indistinguishable.) If there are (n-1) elements in A (add a bin to one end) and k elements in B, the formula gives is the binomial coefficient</p> <pre><code> n + k - 1 ( ) k = ( (n+k-1)! / ( k! (n-1)! ) = (a.size() + b.size())! / (a.size()! * b.size()!) </code></pre> <p>which is the same as templatetypedef's answer :)</p> <p>Actually <em>calculating</em> the quotient of such large numbers is another issue. In most languages, the numerator will commonly produce integer overflow. One simple strategy, not necessarily optimal, in C++ would be</p> <pre><code>unsigned long long gcd( unsigned long long &amp;a, unsigned long long &amp;b ) { return b? gcd( b, a % b ) : a; } std::vector&lt; std::size_t &gt; numerator( a.size() ); // factors of (a+b)!/b! std::iota( numerator.begin(), numerator.end(), b.size()+1 ); for ( std::size_t afactor = 2; afactor != a.size()+1; ++ afactor ) { std::size_t reduced = afactor; for ( auto &amp;&amp;nfactor : numerator ) { auto common = gcd( afactor, nfactor ); nfactor /= common; reduced /= common; if ( reduced == 1 ) goto next_afactor; } throw std::logic_error( "Fractional combinations" ); next_afactor: ; } return std::accumulate( numerator.begin(), numerator.end(), 1, std::multiplies&lt; std::size_t &gt;() ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload