<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p>

<ul>
	<li><code>perm[i]</code> is divisible by <code>i</code>.</li>
	<li><code>i</code> is divisible by <code>perm[i]</code>.</li>
</ul>

<p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 2
<b>Explanation:</b> 
The first beautiful arrangement is [1,2]:
    - perm[1] = 1 is divisible by i = 1
    - perm[2] = 2 is divisible by i = 2
The second beautiful arrangement is [2,1]:
    - perm[1] = 2 is divisible by i = 1
    - i = 2 is divisible by perm[2] = 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 15</code></li>
</ul>

class Solution:
    # def countArrangement(self, n: int) -> int:
    #     memo = {}
    #     nums = tuple(list(range(1, n+1)))
    #     print(nums)
    #     return self.dfs(nums, memo, n)

    # def dfs(self, nums: List[int], memo: Dict, pos: int) -> int:
    #     if pos == 1:
    #         return 1
    #     if nums in memo:
    #         return memo[nums]
    #     cnt = 0
    #     for i in range(len(nums)):
    #         if nums[i] % pos != 0 and pos % nums[i] != 0:
    #             continue
    #         cnt += self.dfs(nums[:i]+nums[i+1:], memo, pos-1)
    #     memo[nums] = cnt
    #     return cnt

    def countArrangement(self, n: int) -> int:
        memo = {}
        nums = list(range(1, n+1))
        return self.dfs(nums, n, memo)
    
    def dfs(self, nums: List[int], pos: int, memo: Dict) -> int:
        if pos == 1: return 1
        if tuple(nums) in memo:
            return memo[tuple(nums)]
        cnt = 0
        for i in range(len(nums)):
            if nums[i] % pos != 0 and pos % nums[i] != 0:
                continue 
            cnt += self.dfs(nums[:i]+nums[i+1:], pos-1, memo)
        memo[tuple(nums)] = cnt
        return cnt


    # def countArrangement(self, n: int) -> int:
    #     # relatively slower compared with memoization approach
    #     nums = list(range(1, n+1))
    #     return self.dfs(nums, set())

    # def dfs(self, nums: List[int], visited: Set[int]) -> int:
    #     if len(visited) == len(nums):
    #         return 1
    #     cnt = 0
    #     for num in nums:
    #         if num in visited:
    #             continue 
    #         if num % (len(visited)+1) != 0 and \
    #             (len(visited)+1) % num != 0:
    #             continue 
    #         visited.add(num)
    #         cnt += self.dfs(nums, visited)
    #         visited.remove(num)
    #     return cnt
17400