I am not able to understand how people are finding out number of particle by using mod http://www.codechef.com/problems/NUKES Can anyone please explain this logic?

Hi!

As is clearly evident, if you go about solving the problem the naive way, it would create a horrendously messy code. So, thinking it over a bit, you can arrive at a simple solution:

The count of each chamber goes from 0 to 0 once `(n + 1)`

particles get bombarded in it. For the first chamber, incoming number of particles: a, which can be written as

```
a = q * (n + 1) + (a % (n + 1))
```

For the q packets containing `(n + 1)`

particles each, the count in the 1st chamber remains 0. Therefore, finally `a % (n + 1)`

particles reside in the 1st chamber. For, the second chamber, 1st chamber is the source: Let `b`

number of particles comes from 1st chamber to 2nd, then you should have `b % (n + 1)`

particles in 2nd chamber by same logic. Express `b`

in terms of `a`

(now its easy!..its `q`

i.e. `a / (n + 1)`

). For the third chamber, second one is the source and so on.

I had read this before in comments…but this is what me not able to understand…this is fine that

“for q packets containing (n+1) particles each,the count in the 1st chamber remains 0”,but how the left particle that reside in 1st chamber is a%(n+1)particles??

Maybe that helps. You are solving problem for A, N and K, but when you found that chamber with index 0 has A%(N+1) particles, you are solving same problem, but with new A = A/(N+1), N is the same and new K is K-1

Try to understand it this way: a particles are bombarded. First q * ( n + 1) particles come and then a (n + 1) particles come. For the first q * ( n + 1 ) particles the count remains 0. For the next a ( n + 1) particles the count remains a (n + 1) because a (n + 1) < (n + 1) and it takes at least (n + 1) particles for the count of any chamber to fall to 0.

guidance: http://www.youtube.com/watch?v=5sS7w-CMHkU

```
suppose input:
25 4 4
that means A=25(bombarded particles), N=4(capacity of chamber), K=4(number of chambers)
step-by-step illustration of chambers with consisting particles in it:
1: 1 0 0 0
2: 2 0 0 0
3: 3 0 0 0
4: 4 0 0 0
5: 0 1 0 0
6: 1 1 0 0
7: 2 1 0 0
8: 3 1 0 0
9: 4 1 0 0
10: 0 2 0 0
11: 1 2 0 0
12: 2 2 0 0
13: 3 2 0 0
14: 4 2 0 0
15: 0 3 0 0
16: 1 3 0 0
17: 2 3 0 0
18: 3 3 0 0
19: 4 3 0 0
20: 0 4 0 0
21: 1 4 0 0
22: 2 4 0 0
23: 3 4 0 0
24: 4 4 0 0
25: 0 0 1 0
```