Untitled

 avatar
unknown
plain_text
5 months ago
682 B
2
Indexable
#include <stdio.h>

int f(int n, int k) {
    // Boundary
    if (n == k) return 1;
    else if (k == 1) return 1;
    
    // Recursion
    // Let f(n, k) be the number of ways when there are n balls and k bins. Then we can consider two ways to put the n-th ball:
    // Put it into a bin that have no other ball, then the number of ways to put other balls is f(n - 1, k - 1)
    // Put it into a bin that have already have other balls (which have k choices of bin), the number of ways ot put other balls is f(n - 1, k)
    return f(n-1, k-1) + k * f(n-1, k);
}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    printf("%d\n", f(n, k));
    return 0;
}
Editor is loading...
Leave a Comment