#P9661. Recurrent Sandpile on a Clique

    ID: 22808 Type: Default 1000ms 256MiB

Recurrent Sandpile on a Clique

Recurrent Sandpile on a Clique

The Abelian Sandpile Model is a famous dynamical system showing self‐organized criticality. In this problem you are given a complete graph (clique) with n vertices. Each vertex i initially holds ai chips. A vertex v is unstable if the number of chips at v is at least its degree (dv=n-1). When a vertex fires (or topples), it sends one chip to every other vertex (thus losing n-1 chips). It can be shown that the final state (if it exists) is unique regardless of the firing order. However, it is possible that the process never terminates – in which case the configuration is called recurrent.

If the process terminates (i.e. the configuration can be stabilized so that every vertex holds less than n-1 chips), output the final number of chips on each vertex, separated by a space. Otherwise, output recurrent.

Mathematical formulation:

Let the configuration be represented as \(a=(a_1,a_2,\dots,a_n)\) with total chips \(T=\sum_{i=1}^{n}a_i\). Since each firing conserves the total number of chips, a necessary condition for stabilization is that the final configuration \(b=(b_1,b_2,\dots,b_n)\) must satisfy \[ b_i = a_i + U - n \cdot x_i, \quad 0 \le b_i < n-1, \quad \text{for all } i, \] where \(x_i\) is the number of times vertex \(i\) fires and \(U=\sum_{i=1}^{n} x_i\). It can be shown that a stabilization exists if and only if \(T \le n\,(n-2)\) and additionally a certain congruence condition holds. In our solution we compute a candidate value \(X\) (equal to the total number of firings) by solving the equation \[ X = \sum_{i=1}^{n} \left\lfloor \frac{a_i+X}{n} \right\rfloor. \] Then the final number of chips at vertex \(i\) is \[ b_i = a_i + X - n \cdot \left\lfloor \frac{a_i+X}{n} \right\rfloor. \] Note that because a vertex is unstable when it holds at least \(n-1\) chips, we must have \(b_i \le n-2\) for every \(i\). Hence if for any \(i\) we have \(b_i = n-1\), the process never terminates and the instance is recurrent.

Remark: In this problem, the input always represents a clique (complete graph), i.e. every pair of distinct vertices is connected by an edge.

inputFormat

The first line contains one integer n (the number of vertices).
The second line contains n space‐separated integers: a1, a2, ..., an – the number of chips at each vertex initially.

outputFormat

If the process is recurrent (i.e. the firings never terminate), output a single line with the word recurrent.
Otherwise, output one line with n integers – the final number of chips on vertices 1 through n (separated by a space).

sample

3
0 0 3
1 1 1

</p>