<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:        
    # Top down, cache the current array for higher speed, takes around 60 ms 
    def countArrangement(self, n: int) -> int:
        @functools.cache
        def solve(cur):
            if len(cur) == 1:
                return 1
            
            total = 0
            for j in range(len(cur)):
                if cur[j] % len(cur) == 0 or len(cur) % cur[j] == 0:
                    total += solve(cur[:j] + cur[j+1:])
    
            return total
        
        return solve(tuple(range(1, n+1)))
"""     

#"""
# typical backtracking algorithm
class Solution:
    # Top down, cache the current array for higher speed, takes around 60 ms
    def countArrangement(self, n: int) -> int:
        def dfs(level): # pos: position to place a new number
            if level > n: 
                nonlocal res
                res += 1
                return
            for i in range(1, n + 1):
                if i not in visited and (i % level == 0 or level % i == 0):
                    visited.add(i)
                    dfs(level + 1)
                    visited.discard(i)

        res = 0
        visited = set()
        dfs(1)
        return res
#"""

"""
# This is permutation problem
class Solution:        
    # Top down, cache the current array for higher speed, takes around 60 ms 
    def countArrangement(self, n: int) -> int:
        def dfs(pos):
            nonlocal res
            
            if pos > n: 
                res += 1
                return
            
            for num in range(1, n + 1): 
                if num not in visited and (num % pos == 0 or pos % num == 0): 
                    visited.add(num)
                    dfs(pos + 1)
                    visited.discard(num)
            
        res = 0
        visited = set()
        
        dfs(1)
        
        return res
"""
16200