98% Faster || 96% Less Space || Hash || Hash table
\nclass Solution {\npublic:\n    #define FastRead        ios_base::sync_with_stdio(0);cin.tie(0)\n    #define ULL             unsigned long long\n    #define LL
```\nclass Solution {\npublic:\n    #define FastRead        ios_base::sync_with_stdio(0);cin.tie(0)\n    #define ULL             unsigned long long\n    #define LL              long long\n    #define eps             1e-9\n    #define inf             0x3f3f3f3f\n    #define INF             2e18\n    #define all(a)          a.begin(),a.end()\n    #define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())\n\n    #define Cube(a) ((a)*(a)*(a))\n    #define Sqr(a)  ((a)*(a))\n    ULL hs = 3797;\n    vector <ULL> table[1 << 16];\n    \n    bool Insert(ULL y) {\n        unsigned short x = y;\n        for (int i = 0; i < table[x].size(); i++) if (y == table[x][i]) return false;\n        table[x].push_back(y);\n        return true;\n    }\n        \n    int countDistinct(vector<int>& nums, int k, int p) {\n        int n = nums.size();\n        int res = 0;\n        for (int i = 0; i < n; i++) {\n            int have = 0;\n            ULL hs_val = 0;\n            for (int j = i; j < n; j++) {\n                hs_val = hs_val * hs + nums[j];\n                if (nums[j] % p == 0) {\n                    if (++have > k) break;\n                }    \n                if (Insert(hs_val)) {\n                    res++;\n                }\n            }\n        }\n        \n        return res;\n    }\n};\n```
[]