Sicily 1715. A piece of cake

本文介绍了一个简单的计数问题:如何计算将n个不同的球放入m个盒子中,每个盒子至少包含k个球的不同方式的数量。通过使用组合数学的方法,文章提供了一种有效的算法实现方案。

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
0

8

// 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;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值