We are given a process: K chambers labeled 0..K-1. Initially all zero. We bombard A particles one at a time. After each bombardment, we do the following reaction: For each chamber, if it has more than N particles, then a reaction occurs: 1 particle moves to the immediate next chamber (i.e., chamber i+1) and all particles in the current chamber are destroyed. The reactions continue until no chamber has more than N particles. Reactions propagate: The movement may cause overflow in subsequent chambers. The description: "and same continues till no chamber has number of particles greater than N." So it's like a cascading overflow process. Also, if a particle moves from the last chamber (K-1) to the next, it is lost.
We need final distribution after A particles bombarded one by one with this overflow process after each.
Constraints: A up to 1e9, N up to 100, K up to 100.
We cannot simulate each particle as A large.
Observation: This process resembles a base representation? Let's analyze.
We have chambers with capacity N. Actually, the rule: If a chamber has > N particles, it triggers: one particle moves to next chamber, and all particles in current chamber are destroyed (set to 0). That is reminiscent of a "carry" in a mixed radix number system where each digit's base is (N+1)? Not exactly: In standard base representation, when a digit exceeds its maximum, we subtract (base) and carry 1. Here, the resetting to 0 is more extreme: when overflow occurs, the current chamber becomes 0 (not just subtract N+1) and we add 1 to the next. That sounds like digits in a base (N+1) but with the twist that when a carry occurs, the digit resets to 0, and we add 1 to next. However, in standard base, we subtract the base (N+1) and add 1 to next; here the reset to 0 means we subtract the current count (which could be > N+1, but note that after a bombardment, we only have one particle added, so the count can become N+1 at most? Actually, could become > N+1? Let's examine.
We add one particle to chamber 0. Then we check for overflows: If chamber 0 > N, we trigger: remove all particles from chamber 0 (destroy) and add 1 to chamber 1. After this, chamber 0 becomes 0, and chamber 1 increases by 1. That could cause chamber 1 > N, then it triggers similarly. So the cascade continues. The key is that after each cascade step, the chamber that overflowed becomes 0. So the system essentially implements a representation of the total number of particles in a mixed-radix system where each digit can be from 0 to N inclusive? But note: The maximum number of particles that can be stored in a chamber without overflow is N. However, after a cascade, the count in a chamber is always ≤ N, because we resolve all overflows. Also, when a cascade occurs, the current chamber resets to 0, not to (old_count - (N+1)). But the condition is > N, so if count = N+1, then it overflows, resulting in 0 and carry 1. If count = N+2? Could that happen? Let's think: After bombardment, we add exactly 1. So initially all chambers are ≤ N. So the only way a chamber can get count > N+1 is if it receives a carry while it already had N? For instance, suppose chamber i has N particles (full). Then a carry arrives from chamber i-1: add 1 => it becomes N+1, which is > N. Then it overflows: it sends a carry to i+1 and becomes 0. So it never goes above N+1. So maximum before overflow check is N+1. So the reset to 0 and carry 1 is exactly like: we have base = N+1; when digit becomes N+1, it overflows, digit becomes 0, and we increment next digit. That is exactly base (N+1) addition. However, we need to consider that after each particle addition, the entire cascade resolves, which is just incrementing a base (N+1) number represented across K digits (with the highest digit possibly truncated if overflow beyond K-1). Indeed, if we treat the state of chambers as a number in base (N+1), where each chamber i holds digit d_i ∈ [0,N], and the whole number (in base (N+1)) is sum_{i=0}^{K-1} d_i * (N+1)^i. Then adding a particle to chamber 0 corresponds to adding 1 to the number. Then we perform carry propagation: if any digit exceeds N, subtract (N+1) from it and add 1 to the next digit. That's exactly standard base addition. In our rule, when a digit goes to N+1 (or >N), we set it to 0 and carry 1. But what if due to multiple carries, a digit could be incremented by more than 1? In standard base, if we add 1, only a single cascade occurs. However, if we add 1, at most one digit might exceed N (the one we increment), and the carry might cause the next digit to become N+1, etc. So it's exactly increment in base (N+1). The key difference: The problem says: "if at any time, there are more than N particles in a chamber, a reaction will cause 1 particle to move to the immediate next chamber, and all the particles in the current chamber will be destroyed and same continues till no chamber has number of particles greater than N." That's exactly carry propagation. Also, the particles destroyed correspond to the subtraction of (N+1) from the digit (since before overflow, the digit had N+1 particles; after overflow, it becomes 0, so the N+1 particles are "destroyed" but one is sent forward). So the process is precisely incrementing a base (N+1) counter with digits in chambers 0..K-1, and if overflow beyond chamber K-1, the particle is lost (i.e., the most significant digit is discarded). Starting from 0.
Therefore, after bombarding A particles one at a time, the state is simply the representation of A in base (N+1) but with only K digits (least significant K digits), because we truncate any overflow beyond chamber K-1.
Thus final distribution: For i = 0 to K-1, the number of particles in chamber i is the i-th digit of A in base (N+1) (least significant digit i=0). That is, compute A in base B = N+1, get digits d0,d1,... up to K-1. If K exceeds the number of digits, remaining are 0. If K is less, then we only keep the lower K digits (i.e., overflow lost). So we need to output d0, d1, ..., d_{K-1}.
Check with example: A=3, N=1 => B=2 (base 2). Represent 3 in base 2: 3 = binary 11 => digits: d0=1, d1=1, d2=0 (since K=3). Output "1 1 0" matches.
Let's test another: A=5, N=2 => B=3. 5 in base 3: 12_3 => d0=2, d1=1, d2=0. So chambers: 2,1,0. Simulate? K? but okay.
But need to verify the process matches exactly. Consider N=0. Then B=1. Base 1 representation? That's degenerate; digits would be? If N=0, capacity is 0, meaning any particle in a chamber causes overflow? Actually N=0 means maximum allowed particles is 0. So if a chamber gets >0, it overflows. After bombardment of first particle to chamber 0, it becomes 1 >0, so it triggers: one particle moves to next, and chamber 0 destroyed => becomes 0. So after first particle, chambers: 0 -> 0, chamber 1 gets 1. If K=1, then the particle moves out from last chamber (since K-1=0 is last? Actually if K=1, chamber 0 is last; then when overflow from chamber 0, it sends one particle to chamber 1 (which doesn't exist) so it's lost. So after first particle, chamber 0 becomes 0. So state always 0. So with N=0, B=1, representation of A in base 1 would be? In base 1, numbers are represented with digit 1? Actually unary uses only digit 1 and each position weight 1^i? Standard base-1 is not typical; but we can treat it as: any number A yields digit 0? Actually if B=1, then any digit can be from 0 to 0 inclusive? That doesn't make sense. Let's think: In our carry rule, if N=0, the maximum digit is 0. If a digit becomes 1, we carry to next and set to 0. So effectively each digit is either 0 or (when we have a carry) resets to 0. The state after increment behaves like a binary (or base something) but digits are only 0. Actually it's like we cannot store any particles; any particle immediately cascades through chambers until lost. So the final state after A particles would be all zeros, regardless of K? Not exactly: Consider K>1. With N=0, after first particle: chamber0 gets 1 -> overflow: chamber0 becomes 0, chamber1 gets 1. Now chamber1=1>0 -> overflow: chamber1 becomes 0, chamber2 gets 1, and so on. Eventually, if K is large enough, the particle will travel down the chambers, and if it reaches chamber K-1 and overflows, it gets lost. But after the cascade, the particle will be in some chamber? Let's simulate step by step: Starting all zeros. Add particle to chamber0: chamber0=1, check >0? yes -> reaction: move one to chamber1, destroy chamber0 => chamber0=0, chamber1=1. Now check chamber1>0? yes -> reaction: move one to chamber2, destroy chamber1 => chamber1=0, chamber2=1. This continues until maybe chamber K-1? When it reaches chamber K-1, if chamber K-1 becomes 1, since it's >0, reaction triggers: move one to chamber K (non-existent) -> lost, and chamber K-1 destroyed => becomes 0. So after cascade, all chambers become 0. So indeed final state all zeros. So for N=0, the final distribution should be all zeros. Our base representation method: B=N+1=1. Represent A in base 1? If we treat base 1, the digits are only 0? But we need to produce K digits. Typically, base 1 representation would be a string of A copies of 1? That's unary. But unary does not fit the digit system with fixed base >1. Actually standard base representation requires base ≥2 because digits range from 0 to base-1. For base 1, base-1 = 0, so digits can only be 0, which cannot represent any positive number. So our mapping fails for N=0. But maybe N is at least 0, and the process for N=0 indeed yields all zeros. Could we treat it as base 1 with an infinite string of 1's? That would not be correct. Let's examine more closely: The carry rule: when count > N, we do: count = 0, next += 1. For N=0, any positive count leads to carry. After a carry, the chamber becomes 0. So after each particle, the state is all zeros, regardless of K? Let's test with K=2, A=1: as above, after cascade, both 0. A=2: after first particle, state all 0. After second particle, add to chamber0=1, cascade: chamber0->0, chamber1=1; chamber1=1>0 -> cascade: chamber1->0, send to chamber2 (lost). So final all 0. Yes, always all 0. So for N=0, final distribution is all zeros. For N=0, our base B=1 approach would require interpretation: the digit range is 0..0, so any number would be represented as all zeros if we consider only digits within range? But then how do we represent A? In a mixed-radix system where each digit has max 0, the only representable number is 0. So indeed the state after A increments might not correspond to A's representation because overflow beyond K digits discards the higher part. But if base is 1, the "overflow" is immediate: any increment causes infinite cascade? Actually with base 1, the weight of each digit is 1^i? Let's formalize.
We need to find state after A increments starting from zero, where each increment is like adding 1 to a base B = N+1 counter, but with only K digits, and higher digits are discarded.
The transition is exactly: state = (previous_state + 1) mod (B^K) in base B arithmetic? Actually careful: If we treat the state as an integer X = sum_{i=0}^{K-1} d_i * B^i, where each d_i ∈ [0, B-1], then adding 1 increments X by 1. But we have a twist: when we add 1, we only add to chamber 0, which corresponds to incrementing X. Then we apply carry propagation across the K digits, and if there is a carry out of the most significant digit (i=K-1), it's lost. That is precisely X := (X + 1) mod (B^K). Because if X + 1 >= B^K, the overflow beyond the K digits is discarded, and we keep the remainder modulo B^K. So the final state after A increments is X = A mod (B^K) represented in base B.
But is this correct for all N? Let's verify with N=0: B=1, B^K = 1^K = 1. Then X = A mod 1 = 0. So the integer X = 0. Representing 0 in base 1? But base 1 digits? Actually representation of 0 in any base is all zeros. So we get all zeros. That matches simulation. So it works if we treat the state as integer modulo B^K. So we can compute A mod (B^K) and then convert that integer to base B to get the digits for the K chambers. That is more robust.
Now note: If B=1, B^K =1, so A mod 1 =0. Then digits: For each i, digit = floor( (X / B^i) ) mod B. Since B=1, division by 1^i = 1; mod 1 always 0. So we get zeros. So that's fine.
So algorithm: Let B = N + 1. Compute M = B^K. But B could be 1, then M=1. But B^K might be huge if B>1 and K up to 100. B max = 101 (since N up to 100). So B^K could be astronomically large: 101^100 is enormous (~10^200), cannot compute as integer in standard languages (but Python supports big integers arbitrarily large, but we can also avoid computing B^K directly if we compute digits via modulo and division using big integers? However Python can handle big integers but might be heavy but okay given constraints. But we need to compute A mod (B^K). That is A modulo a huge number. However we can compute digits of A in base B directly, because we only need the lower K digits. Essentially, we need to compute the representation of A in base B, and then take the least significant K digits, padding with zeros if necessary.
So we can simply convert A to base B repeatedly dividing by B, collecting remainders, until we have K digits, or until A becomes 0. That's easy and avoids big exponentiation. Since K <= 100 and B <= 101, we can compute digits by repeated division. However A can be up to 1e9, which is small enough for Python ints. Actually A up to 1e9, which is not huge. So we can easily compute base conversion. But note: A can be up to 1e9, but if B is 1, division by 1 is infinite loop? Actually B=1, base conversion by division by 1 is degenerate: all remainders would be 0? But the process: while A>0: digit = A % B; A //= B. If B=1, then A % 1 = 0, A//=1 = A, infinite loop. So we need to treat B=1 as a special case: just output K zeros.
Thus algorithm:
Read A, N, K.
Compute base = N + 1.
If base == 1:
output "0 "*K? Actually K numbers separated by spaces, trailing newline. Print "0 " repeated K times, but careful with trailing space. Or build list of "0" and join.
Else:
digits = [] # will hold digits from least significant to most.
x = A
for i in range(K):
if x == 0:
digits.append(0)
else:
digits.append(x % base)
x //= base
# After loop, we have K digits (least first). We need to output from chamber 0 to K-1, which corresponds to digits[0] to digits[K-1] in order.
# But note: The digits list we built in order of increasing i (i=0 first). Actually we want output: d0 d1 ... d_{K-1}. So we can just output digits in the order they were appended? But careful: In the loop, for i=0 we get d0, i=1 get d1, etc. So yes, digits[i] = d_i.
# However, the division reduces x. That's correct.
# So final answer = digits list (size K) in the same order.
# Print them separated by space.
But is this correct? Let's test with A=3, N=1, K=3: base=2. Convert A=3 to base 2: remainders: 3%2=1 -> d0=1; x=1; 1%2=1 -> d1=1; x=0; then for i=2: x=0 -> d2=0. So output 1 1 0. Good.
Test with A=5, N=2, K=2: base=3. Convert 5 to base 3: 5%3=2 -> d0=2; x=1; 1%3=1 -> d1=1; So digits: [2,1]. Output: "2 1". Check simulation? Let's simulate: K=2, N=2. Start 0,0.
Particle1: add to chamber0 => 1, no overflow => (1,0)
P2: chamber0=2 => (2,0)
P3: chamber0=3 >2 => overflow: chamber0 ->0, chamber1+=1 => (0,1) then chamber1=1 <=2 ok.
P4: add to chamber0 => (1,1)
P5: add to chamber0 => (2,1)
So final (2,1). Yes matches.
Test with N=0, K=3, A=5: base=1 special case output 0 0 0. Simulate: each particle causes cascade until lost; after each, all zeros. So final zeros. Good.
Test with large K, small A: A less than base^K, digits beyond computed will be zero. For example, A=10, N=9 (base=10), K=5: digits: 10 in base 10 => d0=0, d1=1, rest 0: output 0 1 0 0 0. But should that be correct? Simulate: N=9, K=5. After each addition, it's like base 10. Starting from 0, after 10 particles, we have digits: 0,1,0,0,0? Actually base 10 representation of 10 is (0,1) meaning 0 ones, 1 ten. But chambers are 0 (ones), 1 (tens). So yes chamber0=0, chamber1=1. Simulation would be: after 9 particles: (9,0,...). After 10th: add to chamber0 => 10>9 => overflow: chamber0=0, chamber1=1. So distribution (0,1,0,0,0). So correct.
Test with overflow beyond K-1: A=12, N=1 (base=2), K=2. Base representation of 12 in binary is 1100. Lower 2 bits: 00. So digits: d0=0, d1=0. Output 0 0. Simulate? K=2, N=1. Starting from 0,0. After each addition: it's like a 2-bit counter modulo 4. After 12 particles, 12 mod 4 = 0, so state 0,0. Good. So using modulo base^K works. Our method of taking the least K digits of base representation of A effectively computes A mod base^K. Yes.
Now, we must ensure that for base>1, we correctly handle A up to 1e9, which is fine. However, what about A up to 1e9 and base small, like base=2, K=100. Then base^K is huge, A mod that is just A. So conversion is straightforward. Repeated division K times is okay: we need K digits, and we can just iterate K times: for i in range(K): digit = A % base; A = A // base; then store digit. This works even if A becomes zero early: we pad zeros. This directly yields the lower K digits. So we don't even need to convert the whole A to base and then take K digits; we just repeatedly divide and reduce A, but after K steps we have the digits. However, note: if we do this, after dividing K times, we have extracted the K least significant digits, but the order of extraction is d0, d1, ..., d_{K-1}. That's exactly what we want. So algorithm:
if base == 1:
print(' '.join(['0']*K))
else:
result = []
temp = A
for i in range(K):
result.append(str(temp % base))
temp //= base
# Output result in same order (since first appended is d0)
print(' '.join(result))
But careful: The above modifies temp. After first iteration, temp becomes floor(A / base). For next digit, we get floor(A/base) % base, which is d1. So correct.
Now, verify with A=3, N=1, K=3: temp starts 3; i=0: 3%2=1 -> result[0]=1; temp=1; i=1: 1%2=1 -> result[1]=1; temp=0; i=2: 0%2=0 -> result[2]=0; output "1 1 0". Good.
Now, test with A=0: Output all zeros. For base>1: loop: temp=0 => all digits 0.
Thus solution is trivial.
But wait: Is the process truly equivalent to base (N+1) addition? Let's re-express the rules: "if at any time, there are more than N particles in a chamber, a reaction will cause 1 particle to move to the immediate next chamber, and all the particles in the current chamber will be destroyed and same continues till no chamber has number of particles greater than N." Note: The reaction says "1 particle moves to the immediate next chamber, and all the particles in the current chamber will be destroyed." It doesn't say that the one particle moving is taken from the particles present; it says it causes 1 particle to move, and all particles in current chamber are destroyed. This could be interpreted as: when the reaction occurs, you lose all particles, but one new particle appears in the next chamber. That is effectively: count_i becomes 0, and count_{i+1} increments by 1. So that's exactly a carry operation.
Now, what about the timing: "After one particle is bombarded, the set of reactions, as described, take place. After all reactions are over, the next particle is bombarded." So after each particle, we resolve overflows until stable. That's exactly like performing increment and then cascading carries.
Thus the equivalence holds.
One nuance: Could there be a scenario where a chamber receives a carry from previous while it is itself also overflow? But since reactions happen sequentially (maybe simultaneously?), the description says "and same continues till no chamber has number of particles greater than N." That implies we iteratively check and react. But regardless of order, the final stable state is unique and matches the base representation.
Therefore solution is correct.
Now, test with more examples.
Example: A=6, N=2, K=3: base=3. Compute digits: 6 in base 3 is 20 (2*3+0). Lower three digits: d0=0, d1=2, d2=0? Wait 6 /3 =2 remainder 0; then 2/3 =0 remainder2. So digits: [0,2]. For K=3: [0,2,0]. Output: 0 2 0. Simulate: K=3, N=2. Starting zeros. Particle 1: (1,0,0). Particle2: (2,0,0). Particle3: (3>2) -> overflow: (0,1,0). Particle4: add => (1,1,0). Particle5: (2,1,0). Particle6: (3,1,0) -> overflow chamber0: (0,2,0). Check chamber1: 2 <=2 ok. So final (0,2,0). Good.
Example with N=0, K=5, A=10: base=1 special -> all zeros. Simulate: each particle cascades through all chambers and lost; final zeros.
Now, ensure that if K is 1, we treat appropriately. With base>1, we just get d0 = A % base. Because after each addition, if chamber 0 exceeds N, we overflow: chamber0 becomes 0 and sends particle to next (which is lost). So effectively it's a modulo base counter. So digit is A % base. Our method: for i in range(K) => only i=0: result = A % base, then // base done but we don't care. So correct.
Now, consider N=100, K=100, A=1e9. base=101. The repeated division 100 times of a number ~1e9 is trivial.
Thus solution is O(K) time.
Now, about input: The line contains three numbers A,N,K separated by spaces. All integers. Output K numbers separated by spaces, newline.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
A = int(data[0]); N = int(data[1]); K = int(data[2])
B = N + 1
if B == 1:
res = ['0'] * K
else:
temp = A
res = []
for i in range(K):
res.append(str(temp % B))
temp //= B
sys.stdout.write(' '.join(res) + '\n')
That's it.
But let's double-check if there is any tricky detail: The problem statement says: "Particles are bombarded one at a time. After one particle is bombarded, the set of reactions, as described, take place. After all reactions are over, the next particle is bombarded." So indeed, sequential increments.
Now, is the base representation valid when N=0? Our special case handles it. Good.
Now, I want to test with A large up to 1e9 and B small: e.g., B=2, K=100, A=1e9. Then we compute 100 digits. The division chain will produce 100 digits. That's fine.
Now, could there be any misinterpretation about the reaction: "1 particle to move to the immediate next chamber, and all the particles in the current chamber will be destroyed." It doesn't say we subtract 1 and then destroy? No, it says "a reaction will cause 1 particle to move to the immediate next chamber, and all the particles in the current chamber will be destroyed". So the particle that moves is not part of the destroyed ones; it's separate. So net effect: count_i becomes 0, count_{i+1} += 1.
Yes.
Now, consider if count_i is N+2? As argued, that cannot happen because we only ever add 1 at a time, and after each cascade, counts are ≤ N. The only way to get more than N+1 is if we have a cascade that increments a chamber that already has N, and then before that chamber's overflow is processed, we might have multiple increments from simultaneous overflows? But the process is sequential: after a reaction, we continue checking. The description: "and same continues till no chamber has number of particles greater than N." It suggests that we repeatedly scan for any chamber > N, and when we find one, we perform the reaction (move one particle, destroy current). This could be done in any order, but the final stable state is unique. However, could there be a scenario where two chambers both > N at the same time? Possibly, but if we resolve one, it might affect the other. But the final state after stabilization is the same as if we propagated carries from least significant upwards (since carries propagate to the left). For base representation, the order does not matter; the final state after complete stabilization is the representation of the total number modulo base^K. So it's fine.
Thus solution stands.
Now, let's provide explanation and code.
But wait: There is an edge: "If a particle is going out from the last chamber, it has nowhere to go and is lost." So our modulo base^K discards overflow beyond K digits.
All set.
Now, write final answer with solution approach and code.
Given the problem constraints, we can implement as described.
Potential pitfalls: If base > 1 and K is large, we might compute many digits, but that's fine.
One more test: A=1000000000, N=100, K=100. base=101. Compute first 100 digits of A in base 101. That's fine.
Now, any need to use 64-bit? In Python it's big ints.
So final answer.
But the user asked: "There are K nuclear reactor chambers ...". The solution is to output the base (N+1) digits.
I'll produce the answer with code in Python.
But as ChatGPT, the final answer should be the solution? The user likely expects code. The problem statement is from some coding challenge. We'll provide a solution explanation and code.
Thus final. The process of bombarding particles one at a time, followed by cascading reactions whenever a chamber exceeds capacity N, is exactly equivalent to incrementing a base‑(N+1) counter with K digits.
After A increments, the state of the chambers is the representation of A modulo (N+1)^K in base N+1.
Thus we can compute the final distribution by extracting the K least‑significant digits of A in base B = N+1.
If B == 1 (i.e., N == 0) the chambers can never hold any particle, so all values are 0.
Algorithm
- Read
A, N, K.
- Set
base = N + 1.
- If
base == 1 → output K zeros.
- Otherwise, repeat
K times:
- Take
A % base as the current digit (chamber count).
- Update
A = A // base.
- Output the collected digits in the same order (chamber 0 first).
Complexity
- Time: O(K) – each iteration does constant‑time arithmetic.
- Space: O(K) for the output.
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
A = int(data[0])
N = int(data[1])
K = int(data[2])
base = N + 1
if base == 1:
res = ['0'] * K
else:
temp = A
res = []
for _ in range(K):
res.append(str(temp % base))
temp //= base
sys.stdout.write(' '.join(res) + '\n')
if __name__ == "__main__":
solve()