building.go 809 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import "fmt"
  3. const MOD int = 1000000009
  4. var n, L int
  5. func main() {
  6. fmt.Scan(&n, &L)
  7. color := make([]int, n)
  8. for i := range color {
  9. fmt.Scan(&color[i])
  10. }
  11. used := make([]bool, n)
  12. cnt := dfs(color, 0, 0, -1, used)
  13. fmt.Println(cnt)
  14. }
  15. func dfs(color []int, l int, cnt int, hi int, used []bool) int {
  16. if n-l+cnt < L || L < cnt {
  17. return 0
  18. }
  19. if l == n {
  20. if cnt == L {
  21. return 1
  22. }
  23. return 0
  24. }
  25. res := 0
  26. for i := 0; i < n; i++ {
  27. if used[i] {
  28. continue
  29. }
  30. used[i] = true
  31. if hi == -1 {
  32. res += dfs(color, l+1, cnt+1, i, used)
  33. } else if hi < i {
  34. if color[hi] != color[i] {
  35. res += dfs(color, l+1, cnt+1, i, used)
  36. } else {
  37. res += dfs(color, l+1, cnt, i, used)
  38. }
  39. } else {
  40. res += dfs(color, l+1, cnt, hi, used)
  41. }
  42. used[i] = false
  43. }
  44. return res
  45. }