- gcd=lambda x,y:gcd(y,x%y)if y else x
- class A:
- def __init__(self,x,y):
- d=gcd(x,y)
- self.x,self.y=x/d,y/d
- def __str__(self):
- return'%d/%d'%(self.x,self.y)
- def __add__(self,n):
- if type(n)==int:return A(self.x+self.y*n,self.y)
- return A(self.x*n.y+self.y*n.x,self.y*n.y)
- def __mul__(self,n):
- if type(n)==int:return A(self.x*n,self.y)
- return A(self.x*n.x,self.y*n.y)
- def __div__(self,n):
- if type(n)==int:return A(self.x,self.y*n)
- return A(self.x*n.y,self.y*n.x)
- __radd__=__add__
- __rmul__=__mul__
- def f(x,y,n):
- if not n:return A(x*y,NN)
- s=0
- s+=f(x,y,n-1)*A(x*(N-y),NN)
- s+=f(x,y,n-1)*A((N-x)*y,NN)
- s+=f(x-1,y-1,n-1)*A(x*y,NN)
- s+=f(x+1,y+1,n-1)*A((N-x)*(N-y),NN)
- return s
- N=12
- NN=N*N
- print f(3,5,2)
- # 36293/248832
- # 0.14585342721193414
复制代码 |