- Backtracking, Combinatorial, combionatrics, Dynamic Programming, Mathematical, Memoization, Recursion

Count of valid arrays of size P with elements in range [1, N] having duplicates at least M distance apart

#include using namespace std;  int calculate(int position, int used, int unused, int P,              int M, vector& dp){        if (position == P) {                        return unused == 0 ? 1 : 0;    }              if (dp[position][used][unused] != -1)        return dp[position][used][unused];          int result = 0;              if (unused > 0) {                        result += calculate(position + 1, used + 1,                            unused – 1, P, M, dp)                  * unused;    }              if (used > M) {                        result += calculate(position + 1,                            used, unused, P,                            M, dp)                  * (used – M);    }          return dp[position][used][unused] = result;}  int solve(int N, int P, int M){                    vector dp(        101,        vector(101,                             vector(101, -1)));      return calculate(0, 0, N, P, M, dp);}int main(){    int N = 2, M = 0, P = 3;    cout