Just need to find all the permutations.
In DFS we will pass the index of a specific permutation's number and a visited array to mark which number we have added to the permutation.
Very simple combination and permutations problem.
class Solution {
public:
int cnt = 0;
void dfs(int n, int idx, vector<bool> vis){
if(idx == n + 1){
cnt++;
}
for(int i = 1; i <= n; i++){
if(vis[i])continue;
if(idx % i != 0 && i % idx != 0)continue;
vis[i] = 1;
dfs(n, idx + 1, vis);
vis[i] = 0;
}
}
int countArrangement(int n) {
vector<bool> vis(n + 1, 0);
dfs(n, 1, vis);
return cnt;
}
};