Can anyone please explain me how pigeonhole principle is used in this question: https://www.spoj.com/SZTE11T1/problems/HALLOW/
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | adamant | 152 |
| 3 | Proof_by_QED | 146 |
| 3 | Um_nik | 146 |
| 5 | Dominater069 | 144 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 9 | TheScrasse | 134 |
Can anyone please explain me how pigeonhole principle is used in this question: https://www.spoj.com/SZTE11T1/problems/HALLOW/
| Name |
|---|



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!