/* Program to compute the number of distinct sequences of length m that can be formed from 2 different letters, with n1 available copies of the first letter and n2 copies of the second. Put N=n1+n2 and assume that 0 #include static int count; /* Accumulate the answer here */ void usage(char *name) { fprintf(stderr, "Usage: %s m\n where\n\t m=sequence length.\n", name); exit(1); } void deduct2(int m, int n1, int n2) { if(m==0) { /* last letter chosen; count the sequence */ ++count; } else { /* ...else choose another available letter */ if(n1>0) deduct2 ( m-1, n1-1, n2 ); if(n2>0) deduct2 ( m-1, n1, n2-1 ); } return; } int main(int argc, char **argv) { int m, n1, n2; n1=3; /* Number of first letters available */ n2=2; /* Number of second letters available */ if(argc==2) m=atoi(argv[1]); /* Length is read from command line */ else usage(argv[0]); /* ...crash on bad command line */ deduct2(m,n1, n2); /* Recursive counting */ printf("%d\n", count); /* Output */ return 0; }