Maximum subset which has no sum of two divisible by K

后端 未结 5 871
栀梦
栀梦 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:15

    formula is

    |N/K| * |(K-1)/2| + ost 
    
    ost =
    if n

    where |a/b| for example |9/2| = 4 |7/2| = 3

    example n = 30 , k =7 ;

    1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30

    1 2 3 |4| 5 6 7. - is first line . 8 9 10 |11| 12 13 14 - second line if we getting first 3 number in each line we may get size of this subset. also we may adding one number from ( 7 14 28)

    getting first 3 number (1 2 3) is a number |(k-1)/2| . a number of this line is |n/k| . if there is not residue we may add one number (for example last number). if residue < |(k-1)/2| we get all number in last line else getting |(K-1)/2|.

    thanks for exception case. ost = 0 if k>n

提交回复
热议问题