atcoder#ARC126E. [ARC126E] Infinite Operations
[ARC126E] Infinite Operations
Score : points
Problem Statement
Given are a sequence of positive integers and queries. The -th query is as follows:
- given integers and (where ), change to .
Each time the query changes the sequence, find the answer to the following problem modulo (see Notes).
Let be the maximum total number of points when doing the following operation times on .
- Choose such that and choose non-negative real number such that .
- Add to and subtract from .
- Gain points.
It can be proved that the limit exists. Find this limit.
Notes
We can prove that the limit in question is always a rational number. Additionally, under the Constraints of this problem, when that number is represented as using two coprime integers and , we can prove that there is a unique integer such that and . Find this .
Constraints
Input
Input is given from Standard Input in the following format:
Output
Print lines; the -th line should contain the answer to the problem just after the change by the -th query.
3 4
7 5 5
1 5
2 6
1 7
3 5
0
1
2
2
The first query changes the sequence to . Here, we have for any , so the answer is .
The second query changes the sequence to . Here, one possible way to do the operations is as follows.
- Choose , changing the sequence to and gaining points.
- Choose , changing the sequence to and gaining points.
The above two operations gain points, from which we can see that .
We can prove that it is impossible to gain more than point in total, and that the total number of points that can be gained by the optimal way to do the operations tends to as the number of operations increases. Thus, we have .
2 4
1 2
2 5
1 3
1 2
2 3
2
1
499122178
499122177