Ctrl K

Divisor Chain Orderings

Medium
You are given an array values of distinct positive integers. Arrange all of them in some order. Such an arrangement is a **divisor chain** if, for every two neighbouring numbers, one of them is divisible by the other. Count how many different arrangements are divisor chains. The count can be huge, so return it modulo 10⁹ + 7.
Example 1
values=[4, 5, 20] → 2

[4, 20, 5] and [5, 20, 4] work: 20 is divisible by both 4 and 5, while 4 and 5 cannot stand next to each other.

Example 2
values=[3, 9, 27] → 6

Any two of these numbers divide one another, so all 3! = 6 orderings work.

Example 3
values=[2, 7] → 0

Neither 2 divides 7 nor 7 divides 2, so no ordering works.

Constraints
  • 2 ≤ len(values) ≤ 14
  • 1 ≤ values[i] ≤ 10⁹
  • All numbers in values are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSquare Frame From Sticks MediumSeating by Divisibility MediumCheapest Basket with Bundles MediumGroups With Equal Sums MediumLeast Total Walk to Scooters Medium
loading Python…
Input
values=[4, 5, 20]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 93 tests on the server, hidden ones included.