//////////////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2003 Jaap Spies, jaapspies@gmail.com // // Distributed under the terms of the GNU General Public License (GPL): // // http://www.gnu.org/licenses/ // // Quick and dirty implementation of NAW problem 29. Author: Jaap Spies // Less dirty by adding more comments 20041010 //////////////////////////////////////////////////////////////////////////////////////// #include main() { int n, h; printf("Enter n > 0: "); scanf("%d", &n); printf("Enter h >= 0: "); scanf("%d", &h); problem29(n, h); } problem29(int n, int h) { int i, j; int *c; // this algorithm generates all possible n-subsets of the set {1,2,...n+h) // it is based on algorithm L from Knuth's taocp part 4: 7.2.1.3 p. 4 // to be publiced (see his homepage) // Lexicographic combinations c = (int *) malloc((n+3)*sizeof(int)); // L1. Initialize for(j=1; j<=n;j++) c[j] = j-1; c[n+1] = n+h; c[n+2] = 0; j = 1; while(j <= n) { // L2. Visit proces(c ,n ,h); // L3. Find j j = 1; while(c[j]+1 == c[j+1]) { c[j] = j-1; j++; } // L5. Increase c[j] c[j] += 1; } } proces(int *c, int n, int h) { int i; int *A, *B, *genB(); // we adjust the input for genB(), the matrix generator A = (int *) malloc(n*sizeof(int)); for(i=0; i 0) { // M2. Visit sum += Q(B, n, x); // M3. Prepare to add 1 j = n; // M4. Carry if necessary while( *(x+j) == 1) { *(x+j) = 0; j--; } // M5. Increase, unless done *(x+j) += 1; } // final division by 2^n see theorem 1 for(i=0;i