Easily implemented using Trie
\nclass Solution:\n    def countDistinct(self, nums: List[int], k: int, p: int) -> int:\n        trie = {}\n        cnt = 0\n        for i in range(len(nums)):\
```\nclass Solution:\n    def countDistinct(self, nums: List[int], k: int, p: int) -> int:\n        trie = {}\n        cnt = 0\n        for i in range(len(nums)):\n            count = 0\n            divis = 0    #contain count of element in array divisible by p\n            d = trie\n            for j in range(i,len(nums)):\n                if nums[j] % p == 0:\n                    divis += 1\n                if divis > k:\n                    break\n                if nums[j] not in d:\n                    d[nums[j]] = {}\n                    count += 1\n                d = d[nums[j]]\n            cnt += count   #count contain all subarrays that can be made from nums[i:j]\n        return cnt```
['Trie', 'Python', 'Python3']