Editorial for JOI '19 Open P2 - Remittance
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1
We can solve Subtask 1 if we search all possible ways of remittance. In fact, if , it can be proved that the
amount of money in every house does not exceed 
 yen if we use the remittance service in any way. Since 
,
there are at most 
 possible states of the amount of money in each house. We can search all of them in time.
Subtasks 2 and 3
In order to solve Subtask 2, we shall solve equations. Let us consider the process where the amount of money
in a house becomes  yen. Let 
 be the amount of money sent by House 
. House 
 pays 
 yen, and receives
 yen. Here we put 
. Then we have the equation
Let us solve it.
Taking the sum of the equation times  for every House 
, we get
From this, we get . Then, solving the equation for each House 
 in order, we get 
.
Each  must be a non-negative integer. If this condition is not satisfied, the answer is 
No. In fact, if every 
is a non-negative integer and at least one of 
 is non-zero, the answer is 
Yes. We shall prove this.
We consider the operations in the opposite direction. We shall transform  into 
.
If some entry of the sequence  is non-zero and the sequence 
 is different from 
, we have 
 and 
for some 
. In fact, if this condition is not satisfied, we have 
 for some 
 with 
. Since 
, we
have 
 by the above equation. Hence we have 
 for every 
, and 
, which is absurd.
For such , we apply the operation which is opposite to sending 
 yen from House 
 to House 
 to the
sequence 
. Then 
 decreases by 
, 
 increases by 
, and 
 decreases by 
. Repeating this operation, we can
transform 
 into 
.
Therefore, if all entries of  are 
, the answer is 
Yes if all entries of  are 
. Otherwise, the answer is 
No.
In Subtask 2, the value of
can be calculated as a 64 bit integer. In Subtask 3, this value can be very large. Even in such cases, we can solve
the task in  time using multiple precision integers.
Another solution
In fact, we can solve this task by the following simulations.
For each House , if it has 
 yen, send 
 yen if 
. Repeat this for each 
. We increase 
 by 
 in
each step. When 
 becomes 
, we return to 
. Note that the amount of money in the House can be 
 in this
process.
If the answer is Yes, after repeating this process in  steps, the amount of money in every
House becomes 
. Let us prove this fact. During this simulation, the amount of money sent from House 
 does not
exceed 
 yen. Thus, from the intermediate status, there is a way to make the amount of money in every House
be 
. After one cycle of 
, the amount of money in every House does not exceed 
 except possibly for one House.
Moreover, the excess for that House becomes half after each step. Therefore, after 
 steps, the
amount of money in every House becomes 
.
By the above argument, we can also show that the answer is Yes if we can make the amount of money in every
House be  and the sequence 
 contains a non-zero value.
Comments