C++ |  Trie Solution | O(N^2)
We will be using TRIE, considering each subarray as a string and only inserting subarrays with total number of elements divisible by \'P\' is less than \'K\'.\n
We will be using TRIE, considering each subarray as a string and only inserting subarrays with total number of elements divisible by \'P\' is less than \'K\'.\n\n**How we managed to cope with Duplicacy?**\n\nLets say there are two diffrent equal subarray with total divisible elements less than \'K\', once we insert the first subarray there will be all nodes already allocated for the next subarray once we start inserting it. (Look into code for better undestanding)\n\n```\nclass Solution {\npublic:\n    class trie {\n        public:\n            unordered_map < int , trie* > next;\n            int c;\n            trie() {\n            }\n    };\n    int countDistinct(vector<int>& nums, int k, int p) {\n        trie *root = new trie();\n        int n = nums.size();\n        int ans = 0;\n        for(int i = 0;i<n;i++){\n            \n            // Temp Pointer, which copies the root.\n            trie *temp = root;\n            \n            // Count stores total number divisible by \'P\' so far.\n            int count = 0;\n            for(int j = i;j<n;j++){\n                if(nums[j] % p == 0)\n                    count++;\n                \n                // If count is greater than \'K\' we do not have valid subarray. \n                if(count > k)\n                    break;\n                \n                // Else if we do not have already existing node for new insertion we will create one.\n                // Also increase \'ANS\' by \'1\' as we encountered this position first time.\n                if(!temp->next[nums[j]]){\n                    temp->next[nums[j]] = new trie();\n                    ans++;\n                }\n                temp = temp->next[nums[j]];\n            }\n        }\n        return ans;\n    }\n};\n```
['Trie', 'C++']