Category: combionatrics

Count of ways to empty given String by recursively removing all adjacent duplicates

#include using namespace std;  int dp[505][505], choose[502][502];  int calc(int l, int r, string& s){          if (abs(r – l) % 2 == 0) {        return 0;    }      if (l > r) {        return dp[l][r] = 1;    }          if (dp[l][r] != -1) {        return dp[l][r];    }          if ((r – l) == 1) {        if (s[l] == s[r]) {            dp[l][r] = 1;        }        else {            dp[l][r] = 0;        }        return dp[l][r];    }          int ans = 0;    for (int k = […]

Continue Reading

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 += […]

Continue Reading