- Each person must work at least a day.
- Only one person can work in a given day.
Example: 2 people, 3 days -> 6 -> (1,1,2), (1,2,1), (1,2,2), (2,1,1), (2,1,2), (2,2,1)
Example: 2 people, 3 days -> 6 -> (1,1,2), (1,2,1), (1,2,2), (2,1,1), (2,1,2), (2,2,1)
What you want is ${N \brace K}K!$. Where the brackets denote the Stirling number of the second kind.
This is because, first you must split the $N$ days into $K$ non-empty groups (this can be done in $N\brace K$ ways ). And then, these $K$ groups must be distributed among the $K$ persons, this can be done in $K!$ ways.
Calculating the Stirling numbers of the second kind on a computer is easy with a recursion, just as easy as calculating binomial coefficients, the important recursion is ${N\brace K}=K{N-1\brace K}+{N-1\brace K-1}$.
The following C++ code calculates ${ N \brace K}$ with the previous method:
There is another way which does not use recursion (assuming you calculate the binomial coefficients otherwise).
It is the formula ${N \brace K} = \frac{1}{K!}\sum\limits_{i=0}^K(-1)^{K-i}\binom{K}{i}i^n$.
So your final answer is also $\sum\limits_{i=0}^K(-1)^{K-i}\binom{K}{i}i^n$. (the proof of this is simple via inclusion/exclusion)
This formula allows you to calculate each query in linear time (with respect to $K$).
The other method allows you to precalculate all of the queries in time $K\cdot N$, but it answers each query in constant time.