-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2992-NumberOfSelfDivisiblePermutations.go
More file actions
153 lines (138 loc) · 5.53 KB
/
2992-NumberOfSelfDivisiblePermutations.go
File metadata and controls
153 lines (138 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
// 2992. Number of Self-Divisible Permutations
// Given an integer n, return the number of permutations of the 1-indexed array nums = [1, 2, ..., n],
// such that it's self-divisible.
// A 1-indexed array a of length n is self-divisible if for every 1 <= i <= n, gcd(a[i], i) == 1.
// A permutation of an array is a rearrangement of the elements of that array,
// for example here are all of the permutations of the array [1, 2, 3]:
// [1, 2, 3]
// [1, 3, 2]
// [2, 1, 3]
// [2, 3, 1]
// [3, 1, 2]
// [3, 2, 1]
// Example 1:
// Input: n = 1
// Output: 1
// Explanation: The array [1] has only 1 permutation which is self-divisible.
// Example 2:
// Input: n = 2
// Output: 1
// Explanation: The array [1,2] has 2 permutations and only one of them is self-divisible:
// nums = [1,2]: This is not self-divisible since gcd(nums[2], 2) != 1.
// nums = [2,1]: This is self-divisible since gcd(nums[1], 1) == 1 and gcd(nums[2], 2) == 1.
// Example 3:
// Input: n = 3
// Output: 3
// Explanation: The array [1,2,3] has 3 self-divisble permutations: [1,3,2], [3,1,2], [2,3,1].
// It can be shown that the other 3 permutations are not self-divisible. Hence the answer is 3.
// Constraints:
// 1 <= n <= 12
import "fmt"
import "math/bits"
func selfDivisiblePermutationCount(n int) int {
var gcd func(a, b int) int
gcd = func(a, b int) int {
mx, mn := a, b
if mx < mn {
mx, mn = b, a
}
k := mx % mn
if k == 0 { return mn }
return gcd(k, mn)
}
var dfs func(visited []bool, index, count int) int
dfs = func(visited []bool, index, count int) int {
if index >= len(visited) {
return count + 1
}
for i := 0; i < len(visited); i++ {
if !visited[i] && gcd(i + 1, index + 1) == 1 {
visited[i] = true
count = dfs(visited, index + 1, count)
visited[i] = false
}
}
return count
}
return dfs(make([]bool, n), 0, 0)
}
func selfDivisiblePermutationCount1(n int) int {
memo := make([]int, 1 << n)
for i := range memo {
memo[i] = -1
}
gcd := func (x, y int) int { for y != 0 { x, y = y, x % y; }; return x; }
var dfs func(int) int
dfs = func(mask int) int {
index := bits.OnesCount(uint(mask))
if index == n { return 1 }
if memo[mask] != -1 { return memo[mask] }
memo[mask] = 0
for j := 0; j < n; j++ {
if 1 << j & mask == 0 && gcd(j+1, index+1) == 1 {
memo[mask] += dfs(1 << j | mask)
}
}
return memo[mask]
}
return dfs(0)
}
// table trick
func selfDivisiblePermutationCount2(n int) int {
table := []int{0,1,1,3,4,28,16,256,324,3600,3600,129744,63504}
return table[n]
}
func main() {
// Example 1:
// Input: n = 1
// Output: 1
// Explanation: The array [1] has only 1 permutation which is self-divisible.
fmt.Println(selfDivisiblePermutationCount(1)) // 1
// Example 2:
// Input: n = 2
// Output: 1
// Explanation: The array [1,2] has 2 permutations and only one of them is self-divisible:
// nums = [1,2]: This is not self-divisible since gcd(nums[2], 2) != 1.
// nums = [2,1]: This is self-divisible since gcd(nums[1], 1) == 1 and gcd(nums[2], 2) == 1.
fmt.Println(selfDivisiblePermutationCount(2)) // 1
// Example 3:
// Input: n = 3
// Output: 3
// Explanation: The array [1,2,3] has 3 self-divisble permutations: [1,3,2], [3,1,2], [2,3,1].
// It can be shown that the other 3 permutations are not self-divisible. Hence the answer is 3.
fmt.Println(selfDivisiblePermutationCount(3)) // 3
fmt.Println(selfDivisiblePermutationCount(4)) // 4
fmt.Println(selfDivisiblePermutationCount(5)) // 28
fmt.Println(selfDivisiblePermutationCount(6)) // 16
fmt.Println(selfDivisiblePermutationCount(7)) // 256
fmt.Println(selfDivisiblePermutationCount(8)) // 324
fmt.Println(selfDivisiblePermutationCount(9)) // 3600
fmt.Println(selfDivisiblePermutationCount(10)) // 3600
fmt.Println(selfDivisiblePermutationCount(11)) // 129744
fmt.Println(selfDivisiblePermutationCount(12)) // 63504
fmt.Println(selfDivisiblePermutationCount1(1)) // 1
fmt.Println(selfDivisiblePermutationCount1(2)) // 1
fmt.Println(selfDivisiblePermutationCount1(3)) // 3
fmt.Println(selfDivisiblePermutationCount1(4)) // 4
fmt.Println(selfDivisiblePermutationCount1(5)) // 28
fmt.Println(selfDivisiblePermutationCount1(6)) // 16
fmt.Println(selfDivisiblePermutationCount1(7)) // 256
fmt.Println(selfDivisiblePermutationCount1(8)) // 324
fmt.Println(selfDivisiblePermutationCount1(9)) // 3600
fmt.Println(selfDivisiblePermutationCount1(10)) // 3600
fmt.Println(selfDivisiblePermutationCount1(11)) // 129744
fmt.Println(selfDivisiblePermutationCount1(12)) // 63504
fmt.Println(selfDivisiblePermutationCount2(1)) // 1
fmt.Println(selfDivisiblePermutationCount2(2)) // 1
fmt.Println(selfDivisiblePermutationCount2(3)) // 3
fmt.Println(selfDivisiblePermutationCount2(4)) // 4
fmt.Println(selfDivisiblePermutationCount2(5)) // 28
fmt.Println(selfDivisiblePermutationCount2(6)) // 16
fmt.Println(selfDivisiblePermutationCount2(7)) // 256
fmt.Println(selfDivisiblePermutationCount2(8)) // 324
fmt.Println(selfDivisiblePermutationCount2(9)) // 3600
fmt.Println(selfDivisiblePermutationCount2(10)) // 3600
fmt.Println(selfDivisiblePermutationCount2(11)) // 129744
fmt.Println(selfDivisiblePermutationCount2(12)) // 63504
}