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.

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$.
