Consider 4 counters looping the digits 0, 1, 2 to form the various "configurations", like in :
Config 0: 0 0 0 0
Config 1: 1 0 0 0
Config 2: 2 0 0 0
Config 3: 0 1 0 0
Config 4: 1 1 0 0
Config 5: 2 1 0 0
Config 6: 0 2 0 0
Config 7: 1 2 0 0
Config 8: 2 2 0 0
... and so on.
(The "faster loop" is assumed to be the one on the left.)
Consider the 2 topics:
A) Total number of configurations:
We have 3 (number of digits) to the power of 4 (number of counters) configurations in total.
B) Find a configuration given its serial number (0, 1, 2, 3, 4, 5,...) :
We can proceed by successive divisions of the serial number by 3 (number of digits) and take the sequence of remainders.
For instance, for "serial" 7, we have:
7 / 3 = 2 with remainder 1
2 / 3 = 0 with remainder 2
0 / 3 = 0 with remainder 0
0 / 3 = 0 with remainder 0
so we get 1 2 0 0 as in
Config 7: 1 2 0 0
Same for any serial.
My question.
Assume the above nested loop is "constrained" as to have only configurations with a non increasing sequence of digits: d(j) >= d(any index larger than j)
(like, for instance, in a bi-dimensional loop, you consider only the upper or lower triangular part of a matrix of configurations)
Example:
Config 0: 0 0 0 0
Config 1: 1 0 0 0
Config 2: 2 0 0 0
Config 3: 1 1 0 0
Config 4: 2 1 0 0
Config 5: 2 2 0 0
Config 6: 2 2 1 0
... and so on.
How do we find:
A) Total number of configurations
B) The configuration from its serial number
(Clearly, I am interested to the general case of N loops and K digits.)
The amount of such configurations is $\binom{n+k-1}{n}$.
A simple combinatorial argument is that when an extra digit ($n+1$) is added, the total is comprised of all "old" configurations (all counters are less than $n+1$) and all "new" configurations (the first counter is fixed at $n+1$); in other words $F(n+1, k) = F(n, k) + F(n+1, k-1)$. Trivial boundary conditions secure the result.
I have no idea on your second question.