1715. A piece of cake
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
This problem is very simple, i.e. just a piece of cake for you, excellent programmers. You just need to calculate and output how many ways to put n different balls into m different boxes so that each box has at least k balls.
Input
Input contains several test cases. Each of the test cases contains three integers in one line, n, m, k (1 <= n, m <= 15, 0 <= k <= 15). Input is terminated by three 0s, which should not be processed.
Output
For each case, just print the result in one line. Heading zeros are forbidden. For example, 12 is legal output but 012 is not, 0 is legal but 00 is not, and so on.
Sample Input
3 3 1 2 4 1 3 2 0 0 0 0
Sample Output
6 08
// Problem#: 1715 // Submission#: 3585085 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <stdio.h> int n, m, k; long long mat[60][60]; long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } long long c(int a, int b) { long long x = 1, y = 1; long long r; int i; for (i = 0; i < b; i++) { x *= (a - i); y *= (b - i); r = gcd(x, y); x /= r; y /= r; } return x / y; } void process() { int i, j, u; for (i = 0; i <= n; i++) for (j = 0; j <= m; j++) mat[i][j] = 0; for (i = 0; i <= n; i++) mat[i][0] = 0; mat[0][0] = 1; for (i = 1; i <= m; i++) for (j = k * i; j <= n; j++) { mat[j][i] = 0; for (u = k; u <= j - (i - 1) * k; u++) { long long temp = c(j, u); mat[j][i] = mat[j][i] + temp * mat[j - u][i - 1]; } } printf("%lld\n", mat[n][m]); } int main() { while (scanf("%d%d%d", &n, &m, &k) == 3) { if (n == 0) break; process(); } return 0; }