#include #include unsigned long int E ( unsigned long int m , /* To eat m Idlis */ unsigned long int i , /* from Day i */ unsigned long int n , /* to Day n */ unsigned long int B ) /* with max B Idlis per Day, B = 0 means no bound */ { unsigned long int sum, k; if (i == n) { /* Possibilities for the last day */ if (m == 0) return 0; /* No Idlis left for the last day */ if (B == 0) return 1; /* Eat all the remaining Idlis */ if (m > B) return 0; /* Too many Idlis left for the last day */ return 1; /* Eat all the remaining Idlis */ } /* Error conditions */ if (i > n) return 0; /* No eating beyond Day n */ if (m+i <= n) return 0; /* Not enough Idlis left for the remaining days */ sum = 0; /* Initialize accumulating sum */ if (B == 0) { /* No bound on the number of Idlis per day */ /* Eat k Idlis on Day i */ for (k = 1; k <= m+i-n; ++k) sum += E(m-k,i+1,n,0); } else { /* Eat k Idlis on Day i */ for (k = 1; (k <= m+i-n) && (k <= B); ++k) sum += E(m-k,i+1,n,B); } return sum; } int main () { unsigned long int m, n, B; printf("Number of Idlis (m) : "); scanf("%lu", &m); printf("Number of days (n) : "); scanf("%lu", &n); /*** Part 1 ***/ printf("Part 1: Number of ways = %lu\n", E(m,1,n,0)); /*** Part 2 ***/ printf("Max Idlis per day (B) : "); scanf("%lu", &B); printf("Part 2: Number of ways = %lu\n", E(m,1,n,B)); exit(0); }