[Python3] Backtracking
\nclass Solution:\n    def countDistinct(self, nums: List[int], k: int, p: int) -> int:\n        def dfs(idx, k, path):\n            nonlocal res, visited\n    
```\nclass Solution:\n    def countDistinct(self, nums: List[int], k: int, p: int) -> int:\n        def dfs(idx, k, path):\n            nonlocal res, visited\n            if idx == len(nums):\n                visited.add(tuple(path[:]))\n                return\n            \n            if nums[idx] % p == 0 and k > 0:\n                path.append(nums[idx])\n                if tuple(path) not in visited:\n                    visited.add(tuple(path))\n                    res += 1\n                dfs(idx + 1, k - 1, path)\n            elif nums[idx] % p != 0:\n                path.append(nums[idx])\n                if tuple(path) not in visited:\n                    visited.add(tuple(path))\n                    res += 1\n                dfs(idx + 1, k, path)\n            \n        res = 0\n        visited = set()\n        for i in range(len(nums)):\n            dfs(i, k, [])\n        return res\n```
['Backtracking', 'Python3']