Can anyone please explain me how pigeonhole principle is used in this question: https://www.spoj.com/SZTE11T1/problems/HALLOW/
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 154 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 146 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 135 |
| 9 | maroonrk | 133 |
| 10 | cry | 130 |
Can anyone please explain me how pigeonhole principle is used in this question: https://www.spoj.com/SZTE11T1/problems/HALLOW/
| Название |
|---|



We have
nnumbers, from which we should choose a subset of numbers such that there sum modulocis 0.Let's compute cumulative sums of these
nnumbers and store in arrays, hences[i] = (a[0] + a[1] + .. a[i]).The first observation is that
n >= cand we have onlycdistinct possible values of remainders upon dividing by c ([0, c-1]). This means that if we consider modulocvalues for allnnumbers of arrays, 2 possibilities are there-There is some
s[i]for whichs[i]%c = 0.If the above condition does not hold then, we are left with only
c - 1distinct possibilities of remainders and greater than or equal tocnumbers (sincen >= c). With pigeon-hole analogy, we havec-1holes and at leastcpigeons. So there will be at least one hole with 2 or more pigeons, that is at least one remainder will be repeated.In the first case, the answer is trivial, just return the first
iindexes such thats[i] % c = 0.For the second case, let's say
i, j, (i < j)are the two indexes such thats[i] % c = s[j] % c. Now consider all indexes fromi + 1toj. Sum of all numbers fromi + 1tojis given bys[j] - s[i]. We can see that(s[j] - s[i]) % c = 0.thanks! got it.
Can you please explain the line "consider all the numbers between 2 numbers which have same modulo c (both numbers inclusive). Sum of all these numbers modulo c will be 0"?I can not understand how we reach to this and how to prove it.
I missed a very important part in the previous comment, please see the updated comment. :)
Whenever a number is divided by c there are only c possible remainders,
0 , 1 , 2 , 3 , .... , c-2 , c-1.Let array of n numbers be
a1, a2, a3 .... an, simply make a cumulative sum array S ,S[i] = (a[i] + S[i - 1]) % c.Given condition
n >= c, Now there aren ( >= c)sum elements(S.length)and c possible values so by pigeonhole principle ....there will be at least a 0 present, in that case answer is all the elements till that index (when cumulative sum is 0).
Or there will be two sum elements Si and Sj such that
Si == Sj, in that case answer will be the subarray from i+1 to j (since(Sj - Si + c) % c = 0) .Hence whenever
n >= canswer is always possible else we will have to use dp to calculate it (for which the constraint is too large).thanks that was a great help!