The Combinatorics of Transistor
CJam (56 bytes)
q~4@:Nm*:$_&{:+1$\-N),&},f{1$1$:+-\0-:(_e`0f=+++:m!:/}:+
Online demo
This is an optimised version of the reference implementation I wrote for the sandbox. Note: I use N
in the code because in a Real Combinatorics Question™ the parameters are \$n\$ and \$k\$, not m
and n
, but I'll use \$M\$ and \$N\$ in the explanation to avoid further confusion.
Suppose that we assign \$X\$ functions to the passive slots (where obviously \$0 \le X \le M\$). Then we have \$N-X\$ functions left for the active + upgrade slots (and we can choose those functions in \$\frac{N!}{X!(N-X)!}\$ ways). Because the order of the slot constellations is irrelevant, we want to consider each partition of \$N-X\$ into at most \$M\$ parts each of at most \$3\$ exactly once.
Take partition \$λ_0, λ_1, \ldots, λ_k\$. We can assign \$λ_0\$ parts to the first slot constellation, \$λ_1\$ to the second, etc. in \$\frac{(N-X)!}{λ_0! λ_1! ... λ_k!}\$ ways (multinomial), but we need to compensate for two factors. Firstly, if we have \$λ_i = λ_j\$ then the constellations are interchangeable, so we further divide by the factorials of the multiplicities of each part, \$μ_i\$. (E.g. the partition 3 2 2 1
has \$μ_3 = 1\$, \$μ_2 = 2\$, \$μ_1 = 1\$). Secondly, we have a designated "active" slot in each constellation, so for each \$λ_i\$ we multiply by \$λ_i\$ ways to select the active function.
So for each partition the number of distributions of functions is
$$\frac{N!}{X! (λ_0-1)! \ldots (λ_k-1)! μ_1! μ_2! μ_3!}$$
The code above computes the partitions using the same approach as Dennis (it's obvious and short, although not very scalable) and then processes each partition into an array similar to [N X λ_0-1 ... λ_k-1 μ_1 μ_2 μ_3]
over which it can lift the factorial function and then fold division.
CJam, 74 67 bytes
q~4@:Mm*:$L|{:+W$\-M),&},f{0-:F1@@{_m!\I-:Nm!/I(m!/*N}fI;Fm!Fe=/}:+
I've verified all test cases on my desktop computer using the Java interpreter. This took 2.2 seconds for 1 ≤ M ≤ 8 and 3.5 minutes for M = 10.
Try this fiddle in the CJam interpreter or verify the first 84 test cases at once.
Idea
In principle, we can fill each active slot and its corresponding upgrade slots with 0, 1, 2 or 3 functions. For 4M slots in total, we take all vectors V of {0, 1, 2, 3}M and filter out those for which sum(V) > N (more functions in non-passive slots than total functions available) or sum(V) + M < N (not enough passive slots for non-active functions). We sort and deduplicate all kept vectors, since the order of the slot families in not important.
With N functions and V = (x1, …, xM) functions in the non-passive parts of the slot families, we calculate the number of combinations as follows:
If x1 = 0, there is only one possibility for that slot family.
If x1 = 1, there are N possibilities, since we have N functions, and the function must go in the active slot.
If x1 = 2, we must place one function in the active slot and another in an upgrade slot (it doesn't matter which). There are N choices for the active slot and N-1 remaining choices for the upgrade slot, giving a total of N(N-1) combinations.
If x1 = 3, there are N choices for the active slot, N - 1 remaining choices for the first upgrade slot and N - 2 for the second upgrade slot. Since the upgrade slots have no order, this counts every combination twice, so there are N(N - 1)(N - 2) unique combinations.
In any case, there are N! / ((N - x1)! × (x1 - 1)! combinations for this family.
We have used up x1 functions, so set N := N - x1 and repeat step 1 for x2, then x3, etc.
If V contains duplicates, the product of the above results will have counted all combinations multiple times. For each unique element of V, if it occurs r times in V, there are r! equivalent ways to arrange these slot families, so the result from above must be devided by r!.
The final result is the number of unique combinations for that V.
To calculate the total number of unique combinations, all that's left to do is to compute the sum of the results for each V.
Code
q~ e# Read an evaluate all input from STDIN. Pushes M and N.
4@:M e# Push 4, rotate the M, and formally save it in M.
m* e# Push {0, 1, 2, 3}^M.
:$ e# Sort each vector.
L| e# Perform set union with the empty list (deduplicates).
{ e# For each sorted vector:
:+ e# Compute the sum of its coordinates.
W$\- e# Subtract the sum from N (bottom of the stack).
M),& e# Push [0 ... M] and intersect.
}, e# If the intersection was not empty, keep the vector.
f{ e# For each kept vector, push N and V; then:
0-:F e# Save the non-zero elements of V in F.
1@@ e# Push 1 (accumulator), and rotate N and F on top of it.
{ e# For each I in F:
_m! e# Push I and push factorial(I).
\I-:N e# Subtract I from N and update N.
m!/ e# Divide factorial(N) (original N) by factorial(N) (updated N).
I(m!/ e# Divide the quotient by factorial(I - 1).
* e# Multiply the accumulator by the resulting quotient.
N e# Push N for the next iteration.
}fI e#
; e# Pop N.
Fm! e# Push all non-unique permutations of F.
Fe= e# Count the number of times F appears.
/ e# Divide the accumulator by the result.
} e#
:+ e# Add all resulting quotients.