Skip to main content
python3.8
Source Link
ngn
  • 15.6k
  • 2
  • 45
  • 90

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 

Try it online!Try it online!

Venn diagram

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

Python 3

def C(n,k): # binomial coefficients if n<0 or k<0:return 0 p=q=1  for i in range(k):p*=n-i;q*=i+1  return p//q  def 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 

Try it online!

Venn diagram

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

Python 3.8 (pre-release)

from math import comb C=lambda n,k:comb(n,k) if n>=0 and k>=0 else 0 f=lambda n,v,x,y,z: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) 

Try it online!

Venn diagram

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

deleted 27 characters in body
Source Link
ngn
  • 15.6k
  • 2
  • 45
  • 90

Python 3

def C(n,k): # binomial coefficients if n<0 or k<0:return 0 p=q=1 for i in range(k):p*=n-i;q*=i+1 return p//q def 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 

Try it online!

Venn diagram

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 of each other, so, we multiply by a factor of 4**(n-x-y-z+2*v).

Python 3

def C(n,k): # binomial coefficients if n<0 or k<0:return 0 p=q=1 for i in range(k):p*=n-i;q*=i+1 return p//q def 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 

Try it online!

Venn diagram

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 of each other, so, we multiply by a factor of 4**(n-x-y-z+2*v).

Python 3

def C(n,k): # binomial coefficients if n<0 or k<0:return 0 p=q=1 for i in range(k):p*=n-i;q*=i+1 return p//q def 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 

Try it online!

Venn diagram

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

added 62 characters in body; added 32 characters in body
Source Link
ngn
  • 15.6k
  • 2
  • 45
  • 90

Python 3

def C(n,k): # binomial coefficients if n<0 or k<0:return 0 p=q=1 for i in range(k):p*=n-i;q*=i+1 return p//q def 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 

Try it online!Try it online!

enter image description hereVenn diagram

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. SoThose choices are independent of each other, so, we multiply by a factor of 4**(n-x-y-z+2*v).

Python 3

def C(n,k): if n<0 or k<0:return 0 p=q=1 for i in range(k):p*=n-i;q*=i+1 return p//q def f(n,v,x,y,z): 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 

Try it online!

enter image description here

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, 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. So, we multiply by a factor of 4**(n-x-y-z+2*v).

Python 3

def C(n,k): # binomial coefficients if n<0 or k<0:return 0 p=q=1 for i in range(k):p*=n-i;q*=i+1 return p//q def 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 

Try it online!

Venn diagram

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 of each other, so, we multiply by a factor of 4**(n-x-y-z+2*v).

explanation
Source Link
ngn
  • 15.6k
  • 2
  • 45
  • 90
Loading
Source Link
ngn
  • 15.6k
  • 2
  • 45
  • 90
Loading