-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy path2992-number-of-self-divisible-permutations.js
More file actions
59 lines (52 loc) · 1.42 KB
/
2992-number-of-self-divisible-permutations.js
File metadata and controls
59 lines (52 loc) · 1.42 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
/**
* 2992. Number of Self-Divisible Permutations
* https://leetcode.com/problems/number-of-self-divisible-permutations/
* Difficulty: Medium
*
* 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]
*/
/**
* @param {number} n
* @return {number}
*/
var selfDivisiblePermutationCount = function(n) {
const graph = new Array(n + 1).fill().map(() => []);
for (let position = 1; position <= n; position++) {
for (let value = 1; value <= n; value++) {
if (gcd(value, position) === 1) {
graph[position].push(value);
}
}
}
return countPermutations(1, 0);
function countPermutations(position, usedMask) {
if (position > n) return 1;
let count = 0;
for (const value of graph[position]) {
if ((usedMask & (1 << value)) === 0) {
count += countPermutations(position + 1, usedMask | (1 << value));
}
}
return count;
}
function gcd(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
};