Number of combinations with restrictions So ideally I would want a general solution (or explanation of how to get there) for the following problem, which I will circumscribe in a context for easier accessability.
Consider $n$ indistinguishable jars, $a$ indistinguishable marbles labeled 'A' and $b$ indistinguishable marbles labeled 'B'. The numbers $a\geq 0$ and $b\geq 0$ themselves already come restricted to meet the following requirements:
$$a -b \geq 0 \quad \text{and} \quad 3b-a+1\geq 0 $$
The marbles can be distributed over the $n$ jars with two restrictions for the final number of 'A's ($x$) and 'B's ($y$) ending up in the same jar:
$$ x-y \geq 0\quad\text{and}\quad 3y-x+1 \geq 0 $$
I need a general expression for the number $m$ of possible combinations
$$m=f(n,a,b)$$
or if thats not possible, I need at least the specific solutions for $n=3$ and $n=4$.
Also, it would be great if there was a calculative way to label the $n$ jars according to their 'content' for each combination $m$, e.g.
$$ \begin{Bmatrix}
[x_{11}][y_{11}] & [x_{12}][y_{12}] & ... & [x_{1n}][y_{1n}] \\
[x_{21}][y_{21}] & [x_{22}][y_{22}] & ... & [x_{2n}][y_{2n}] \\
...& ...& ... & ... \\
[x_{m1}][y_{m1}] & [x_{m2}][y_{m2}] & ... & [x_{mn}][y_{mn}]
\end{Bmatrix}$$
 so that each $\alpha_{ij}=f(o,p,a,b)$ and $\beta_{ij}=f(o,p,a,b)$ with $o=1,2,...,m$ and $p=1,2,...,n$.
I'm a chemist and don't have very deep knowledge of mathematics, so forgive any incompetence I might be showing. My biggest problem right now is how to implement the restrictions into the combinatorial calculations. In case there is no analytic way to derive a general solution, any hints towards an algorithmic procedure that could do the job for lets say $a,b<50$ would also be appreciated.
 A: I made a program which appears to work. It starts getting sluggish when input numbers approach $50$ and it thus took over a minute to find that $f(15,40,30)= 25,228,180$. But then I haven't optimized and am just using Visual Basic. 
Anyway, the basic algorithm is the following...
Step 1: Generate the next partition of $a$ with $n$ or fewer parts
As no jar can have more $B$ marbles than $A$ marbles, the maximum number of jars possible is $a$. Given $n \le a$ jars, the total number of ways to distribute $a$ marbles among $n$ or fewer jars is just the number of partitions of $a$ with $n$ or fewer parts. Algorithms for determining such partitions are available on the net. The one I used is given here.
Step 2: Allocate the minimum $B$ marbles to each jar
We can calculate the minimum number of $B$ marbles that each jar with $A$ marbles must at least contain. Distribute the necessary $B$ marbles and reduce $b$ by the total distributed. When $b$ is small, it may not be possible to distribute even the minimum. In that case, no solution is possible for that partition, and we go to Step 1. 
Step 3: Distribute any excess $B$ marbles to the partition
This is the tough step, so I've split it into sub-steps. Before proceeding, I should mention that if there are no excess $B$ marbles, the partition only has one solution, and we go to Step 1.
Step 3a: Group jars with the same number of $A$ marbles
If we have two jars with four $A$ marbles each (and hence one minimum $B$ marble each), we don't want to count combinations where the first jar recieves two excess $B$ marbles and the second recieves one as different from combinations where the first jar recieves one and the other two. We therefore need to group jars with the same number of $A$ marbles into one group, before we distribute $B$ marbles. The maximum of each group will be "Number of jars with equal $A$ marbles" $\times$ ("Maximum $B$ marbles in a jar with $x$ $A$ marbles" minus "Minimum $B$ marbles in a jar with $x$ $A$ marbles")
Step 3b: Generate each possible distribution of the excess $E$ to the groups
If we have $k$ groups, each with maximum capacity $C_i$, where $i \le k$, and $x_i$ is the number of $B$ marbles allocated to group $i$, we want an algorithm which generates all possible solutions to $x_1+x_2+...+x_k=E$. It took me a while to find such an algorithm. The algorithm is basically this:


*

*Fill each group with the maximum (if possible), starting from group $1$ (the left). 

*At the group you ended at, move one marble to the right, if possible. That's a new combination. And you ended at a new group. Go to $2$ 

*If it's not possible to move a marble to the right, go backwards until you find a group where it is possible to move an additional marble to the right or until you can't move backwards any more. 


Below is an example with $E=3$ and four groups with the capacities $4,3,3,1$:

Step 3c: For each distribution, determine in how many ways the $B$ marbles allocated to each group can be distributed among the jars of that group. Multiply each group's number and add to the total.
When you have a distribution of the excess marbles among the groups, you need to determine in how many ways each allocation of $B$ marbles to a group can actually be distributed among the members of the group, without repetitions. Thus, if you had $4$ menbers of a group, where the maximum capacity of each group member was $4$ and you had allocated $8$ excess marbles to the group, the possible distributions would be

We see that this algorithm is just a modified version of the one in $3b$. It is just that the maximum capacity of each member is the same and the criteria for moving a marble to the right is not just that there is sufficient capacity to the right, but that the resulting number of $B$ marbles to the right is less than or equal to the number to the left.
Step 4: Go to Step 1 until the last partition has been handled
I feel I haven't explained the algorithm well, so feel free to ask questions. I can post the code if you wish, but I warn you that it isn't commented. 
