Advertisement
nikunjsoni

526

Apr 25th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. class Solution {
  2. int ans=0;
  3. int mask = 0;
  4. public:
  5.     int countArrangement(int n) {
  6.         dfs(n, 1);
  7.         return ans;
  8.     }
  9.    
  10.     void dfs(int n, int pos){
  11.         if(pos > n) ans++;
  12.         for(int i=1; i<=n; i++){
  13.             bool isSet = (mask & (1<<i));
  14.             if(!isSet && (i%pos == 0 || pos%i == 0)){
  15.                 mask |= (1<<i);
  16.                 dfs(n, pos+1);
  17.                 mask ^= (1<<i);
  18.             }
  19.         }
  20.     }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement