Python 3Python 3.8 (pre-release)
deffrom Cmath import comb C=lambda n,k:comb(n,k): # binomial coefficients if n<0 or k<0:return 0 p=q=1 for in>=0 inand range(k):p*=n-i;q*=i+1 k>=0 returnelse p//q 0 deff=lambda f(n,v,x,y,z): # solution return C(n,v)*C(n-v,x-v)*C(n-x,y-v)*C(n-x-y+v,z-v)*4**(n-x-y-z+2*v) assert f(2,0,1,1,1)==0 assert f(2,1,1,1,1)==8 assert f(2,2,1,1,1)==0 assert f(8,0,2,3,3)==560 assert f(8,1,2,3,3)==80640 assert f(8,2,2,3,3)==215040 assert f(8,3,2,3,3)==0 We fill the Venn diagram's compartments one by one, and we multiply the numbers of options we have at each step:
C(v,n) - to fill the common intersection A∩B∩C, we need to choose v elements out of a total of n. After making that choice, we are left with n-v elements.
C(n-v,x-v) - we fill the x-v compartment from those n-v, and we're left with n-v-(x-v) = n-x elements
C(n-x,y-v) - we fill y-v, with n-x-y+v left
C(n-x-y+v,z-v) - we fill z-v
For each of the remaining n-x-y-z+2*v elements, there are four possibilities - either belong to exactly one of A, B, or C, or to none of them. Those choices are independent, so we multiply by 4**(n-x-y-z+2*v).
