Correctly calculating permutations and combinations without duplicate patterns Given 16 balls each numbered 1 through 16, and 5 glass tubes numbered 1 through 5; how many ways are there to slot all 16 balls into the glass tubes, selected one at a time, with the only condition that each slot should always have at least 1 ball? The ball and glass numbers matter.
 A: We assume that order of balls in the tubes matters. 
Line up the $16$ balls in some order.  There are $16!$ ways to do this. There are $15$ interball gaps. We choose $4$ of them to place a separator into in the usual Stars and Bars style. This can be done in $\binom{15}{4}$ ways.
Thus the total number of ways is $16!\binom{15}{4}$. The idea generalizes.
Remark: Numerically, this gives the same result as the recursion of dREaM.
A: We can obtain a recursion.
How many ways are there to place balls $1,2,3\dots n$ in tubes $1,2,3\dots k$ so that every tube is not empty? We call this number $f(n,k)$
If we look at a proper arrangement of the balls $1,2,3\dots n-1$ then there are exactly $n+k-1$ places into which we can place the $n$'th ball (on top of any of the other $n-1$ other balls or on the bottom of any of the tubes).
This gives us $(n+k-1)f(n-1,k)$ arrangements.
The other arrangements we are missing are the arrangements in which ball $n$ is alone inside its tube, there are $k$ ways to select the tube in which it is and then $f(n-1,k-1)$ ways to fill the other tubes properly.
We have obtained the recursion $f(n,k)=(n+k-1)f(n-1,k)+kf(n-1,k-1)$.
We can use this recursion to build the following code in c++:
#include <cstdio>
long long factorial[17];
long long f(int n,int k){
  if(n==k){
    return(factorial[k]);
  }
  if(k==1){
    return(factorial[n]);
  }
  return((n+k-1)*f(n-1,k)+k*f(n-1,k-1));
}
int main(){
  int a;
  factorial[0]=1;
  for(a=1;a<17;a++){
    factorial[a]=factorial[a-1]*a;
  }
  printf("%lld\n",f(16,5));
}

It gives $28559608197120000$.
