-1

What is the number of ways to color $m$ distinct objects using $n$ colors such that at least $k$ colors are used?

For example, for $n=4, m=2, k=2$, the answer is

$$\binom{4}{2} \cdot 2! = 12$$

Here it is straight-forward as $k=m$. How do I solve when $k < m$?

Here are special cases: here and here

Mike Earnest
  • 75,930
  • Answer the question for exactly $k$ colors used. Add up the results for each amount of colors used. For exactly $k$, break the $m$ objects into $k$ nonempty groups using Stirling Numbers of the Second Kind in ${m\brace k}$ ways and then pick which $k$ colors were used for each group in $n\frac{k}{~}$ ways (falling factorial). This gives a grand total of $\sum\limits_{i=k}^n{m\brace i}n\frac{i}{~}$ – JMoravitz Jul 18 '22 at 13:00
  • @JMoravitz Thanks it does the job . But can you suggest more efficient computation. Computing stirling2 is $O(n^2)$ both memory and time and looping it makes it $O(n^3)$ time . I can afford O(N) or $O(N∗logX)$ time and $O(N)$ memory. – ishandutta2007 Jul 18 '22 at 14:11
  • @JMoravitz Stirling2 can be done in O(n log n) but that wont suffice. I need the overall complexity after looping to be something like that. – ishandutta2007 Jul 18 '22 at 14:24
  • 5
    @hardmath this is open-ended question. There is no right or wrong approch. $O(n)$ or $O(n.logn)$ or $O(n)$ all are welcome obviously somenone who comes up with O(n) will be apprecated more. JMoravitz 's approach was nice but Mike Earnest's was more efficient and hence appreciated more. – ishandutta2007 Jul 31 '22 at 10:34
  • Open-ended questions are natural. I voted to close because the body of your Question does not contain a problem statement. Your title does a good job of setting up the problem (necessarily in a terse way), and your Comments above mention a goal of complexity. At a minimum you could bring these ideas into the body of the Question. Readers could then more easily contribute responses using your notation and terminology. – hardmath Jul 31 '22 at 17:27
  • 3
    @hardmath comment is not meant to be a goal. but guideline. For any problem there is always a brute force solution. but that doesn't mean posting that as answer will get accepted. That may be accepted uptil there is a better solution. The Tick will always change as better and better solutions get posted. Secondly Will repeating title in the body make you reopen the question ? – ishandutta2007 Aug 01 '22 at 19:53

1 Answers1

2

Let $A_i$ be the set of colorings which do not use the $i^{th}$ color. We want to count the number of colorings which use at least $k$ colors, which means there are at most $n-k$ values of $i$ for which the coloring is in $A_i$. Using a generalization of the inclusion-exclusion principle presented here, equation (8), we get $$ \begin{align} \text{# colorings with at least $k$ colors} &=\text{# colorings in at most $n-k$ of the sets $A_i$} \\&=\sum_{j=0}^n(-1)^{j-(n-k)}\binom{n}{j}\binom{j-1}{n-k}|A_1\cap \dots \cap A_j| \end{align} $$ The case $j=0$ of this sum requires special attention. In this case, we get $(-1)^{n-k}\binom{n}0\binom{-1}{n-k}$ times the size of the empty intersection. The empty intersection is just the whole universe of colorings, of which there are $n^m$. Also, $\binom{-1}{n-k}=(-1)^{n-k}$, so this summand is just $n^m$. Furthermore, for $1\le j \le n-k$, we have $\binom{j-1}{n-k}=0$, so we can omit these. Finally, $|A_1\cap \dots \cap A_j|=(n-j)^m$, since there are $j$ fewer colors available. Therefore, we can write this as $$ \begin{align} \text{# colorings with at least $k$ colors} &=n^m+\sum_{j=n-k+1}^n(-1)^{j-(n-k)}\binom nj\binom{j-1}{n-k}(n-j)^m %\\&=n^m+\sum_{j=0}^{k-1}(-1)^{j-k}\binom nj\binom{n-j-1}{n-k}j^m \end{align} $$ This summation can be computed in $O(n\log m)$ time, as long as you pre-compute all of the binomial coefficients involved, and use exponentiation by squaring to find $(n-j)^m$. To quickly do the pre-computation, these identities are quite helpful: $$ \binom{n}{j+1}=\frac{n-j}{j+1}\binom{n}{j}\qquad \binom{j}{n-k}=\frac{j}{j-(n-k)}\binom{j-1}{n-k} $$ Edit After several edits, this is now correct. This Python code serves as a sanity check.

Mike Earnest
  • 75,930
  • 2
    Thanks . can you help me understand a bit may be by walking through an example. It's not very simple to understand. I will accept the answer once I understand this clearly (and also clean up these comments once done). "which means there are at most n−k values of i for which the coloring is in $A_{i}$"..what does it mean ? to compute $A_{i}$ you are skipping the i-th colour right, so out of remaining (n-1) colours any one color can occur (n-k) times at most right? – ishandutta2007 Jul 19 '22 at 02:52
  • @JackBernardo I don't want to have a conversation in the comments, so let's talk in this chatroom: https://chat.stackexchange.com/rooms/137878/mike-earnest-and-jack-bernardo – Mike Earnest Jul 19 '22 at 03:06