/* file : countingCards.c */ /* author : Oscar MirĂ³ (o.miro.i.lopez.feliu@student.rug.nl) */ /* Description: * */ #include // func checks if an array is sorted in ascending order. Returns false if arr[n] > arr[n+1] // returns true if its either ascending or they have the same value int isAscending (int size, int arr[]) { for (int i = 0; i < size - 1; i++) { if (arr[i] > arr[i+1]) { return 0; } } return 1; } // main recursive function int check (int size, int dist[], int type, int found, int arr[]) { // checks if the hand passed is full if (found == size) { // checks if the hand is unique. For every combination of cards, say ABC, ACB BCA, // if you only consider the ones that are in ascending order you avoid repeating it if (isAscending(size, arr)) { return 1; } // if hand not not ascending, looks for another hand return 0; } else if (!isAscending(found, arr)) { return 0; } // if the hand is not full add another card int count = 0; // iterates for every card type, adds a card for (int i = 0; i < type; i++) { // checks that in the card distribution there are enough cards to add one if (dist[i] > 0) { arr[found] = i; // removes a one from the type of card added when passing it dist[i]--; // recalls func. adds another card till hand is full, then returns 1 if // hand is unique. 1 is added to count, when all combinations are checked // returns count which is later output count += check (size, dist, type, found+1, arr); // adds it again after passing it, for other iterations dist[i]++; } } return count; } int main (int argc, char *argv[]) { // declaration and scanning int size, type, arr[10000], found = 0; scanf("%d %d", &size, &type); // arr dist holds the number of cards for each type int dist[type]; for (int i = 0; i < type; i++) { scanf("%d", dist + i); } // finds the combination of cards printf("%d\n", check(size, dist, type, found, arr)); }