Maximum subset which has no sum of two divisible by K

后端 未结 5 852
栀梦
栀梦 2021-02-14 16:49

I am given the set {1, 2, 3, ... ,N}. I have to find the maximum size of a subset of the given set so that the sum of any 2 numbers from the subset is not divisible by a given n

5条回答
  •  误落风尘
    2021-02-14 17:23

    I'm assuming that the set of numbers is always 1 through N for some N.

    Consider the first N-(N mod K) numbers. The form floor(N/K) sequences of K consecutive numbers, with reductions mod K from 0 through K-1. For each group, floor(K/2) have to be dropped for having a reduction mod K that is the negation mod K of another subset of floor(K/2). You can keep ceiling(K/2) from each set of K consecutive numbers.

    Now consider the remaining N mod K numbers. They have reductions mod K starting at 1. I have not worked out the exact limits, but if N mod K is less than about K/2 you will be able to keep all of them. If not, you will be able to keep about the first ceiling(K/2) of them.

    ==========================================================================

    I believe the concept here is correct, but I have not yet worked out all the details.

    ==========================================================================

    Here is my analysis of the problem and answer. In what follows |x| is floor(x). This solution is similar to the one in @Constantine's answer, but differs in a few cases.

    Consider the first K*|N/K| elements. They consist of |N/K| repeats of the reductions modulo K.

    In general, we can include |N/K| elements that are k modulo K subject to the following limits:

    If (k+k)%K is zero, we can include only one element that is k modulo K. That is the case for k=0 and k=(K/2)%K, which can only happen for even K.

    That means we get |N/K| * |(K-1)/2| elements from the repeats.

    We need to correct for the omitted elements. If N >= K we need to add 1 for the 0 mod K elements. If K is even and N>=K/2 we also need to add 1 for the (K/2)%K elements.

    Finally, if M(N)!=0 we need to add a partial or complete copy of the repeat elements, min(N%K,|(K-1)/2|).

    The final formula is:

    |N/K| * |(K-1)/2| +
    (N>=K ? 1 : 0) +
    ((N>=K/2 && (K%2)==0) ? 1 : 0) +
    min(N%K,|(K-1)/2|)
    

    This differs from @Constantine's version in some cases involving even K. For example, consider N=4, K=6. The correct answer is 3, the size of the set {1, 2, 3}. @Constantine's formula gives |(6-1)/2| = |5/2| = 2. The formula above gets 0 for each of the first two lines, 1 from the third line, and 2 from the final line, giving the correct answer.

提交回复
热议问题