title: Distributing balls to baskets with limited size
body: <p>I've been trying to find a solution to this problem for a few hours now: I have <em>L</em> balls and <em>n</em> baskets labelled X1, X2, ... Xn  , and each container can hold <em>LXn</em> balls. Now I want to calculate the number of ways I can distribute those <em>L</em> balls into the baskets.</p>
  
  <p><strong>An example:</strong></p>
  
  <p>There are 3 baskets X1, X2 and X3, the capacity of X1 and X3 is <em>2 balls</em>, X2 has a capacity of <em>3 balls</em>. Now <em>5 balls</em> are distributed into those baskets:</p>
  
  <p>X1 X2 X3</p>
  
  <p>0  3  2<br>
  1  2  2<br>
  1  3  1<br>
  2  1  2<br>
  2  2  1<br>
  2  3  0</p>
  
  <p>I want to know how many ways there are to distribute the balls with given values.</p>
  
  <p><strong>The mathematical solution:</strong></p>
  
  <p>p_n(x) = x^0 + x^1 + .. + x^(LXn)<br>
  P(x) = p_1(x) * ... * p_N(x)</p>
  
  <p>The key is that the coefficient before <em>x^L</em> (see next equation) indicates the number of ways to distribute the given <em>L</em> balls into the <em>n</em> baskets.  </p>
  
  <p>With the example above this would result in<br>
  P(x) = (1+x+x^2) * (1+x+x^2+x^3) * (1+x+x^2) = 1 + 3x + 6x^2 + 8x^3 + 8x^4 + 6x^5 + 3x^6 + x^7<br>
  So for <em>L=5</em> it reads <em>6 different arrangements</em></p>
  
  <p><em>(Source: <a href="http://narkive.com/9a0CvErb.2" rel="nofollow">http://narkive.com/9a0CvErb.2</a> (german))</em></p>
  
  <p><strong>Now for my actual question:</strong> Are there ways of doing this, or something similar, in java?</p>
  
  <p>Thanks!</p>