Title: Divide a circle into a small, n-shaped fan that marks these sectors as three-to-three,..., N. Each sector is now painted with M-color, and each sector is painted in one color, and the adjacent fan color is different, how many different kinds of coating are asked? (n≥1,m≥3)
Ref: 79529355
Recursive resolution:
When N=1 is, f (1,m) = m
When N=2 is, f (1,m) = m (m-1)
When N=3 is, f (1,m) = m (m-1) (m-2)
When N=4, f (4,m) = m (m-1) (m-2) (m-2) + M (m-1) (m-1) = m (m-1) (m^2-3m+3)
When N=5, f (5,m) = m (m-1) (m-2) (m-2) + M (m-1) (m-1) (m-1) = m (m-1) (m^2-3m+3) (m-1) = f (4,m) (m-1)
Can eject relationship f (n,m) = f (n-1,m) (m-1)
def calculate(n,m): """ :param n: n个扇形 :param m: m种颜色 :return: 涂法的种类 """ if n == 1: return m if n == 2: return m * (m-1) if n == 3: return m * (m-1) * (m-2) if n == 4: return m * (m-1) * (m-2) * (m-2) + m * (m-1) * (m-1) return calculate(n-1,m) * (m-1)print(calculate(4,4))
Coloring question (Python)