Guest User

Untitled

a guest
Mar 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. 一个圆盘,分成N块,有M种涂色方案,要求相邻两块不可同色,有多少种涂色方案。
  2.  
  3. A(N)可以看成第一块M种和剩下的M-1块又N-1种涂色方案,但是包含了第一块和最后一块同色的情况,即A(N-1)
  4. A(N) = M * (M-1)^N-1 - A(N-1)
  5.  
  6. ```
  7. import math
  8.  
  9.  
  10. def vender(N, M): # N块, M种涂色方案
  11. if N == 1:
  12. return M
  13. if N == 2:
  14. return M * (M-1)
  15. return int(M * math.pow(M-1, N-1) - vender(N-1, M))
  16.  
  17.  
  18. while True:
  19. a = []
  20. line = input()
  21. for x in line.split():
  22. a.append(int(x))
  23. print(vender(a[0], a[1]))
  24.  
  25. ```
Add Comment
Please, Sign In to add comment