Generators
Time limit1sMemory limit256 MB
Simulate each bounded generator to find its largest reachable value, then lower one value by the smallest loss that breaks divisibility by k.
- Level
Medium4 of 10
- Topics
- Greedy, Simulation
- Solved
- No attempts yet
Problem
A linear congruential generator (LCG) starts from a non-negative integer , called the seed, and produces an infinite sequence of integers with :
Here , and are non-negative integers and .
You are given generators. The parameters of generator are , , and , and it produces the sequence . Take one term from each of the sequences so that the sum of the taken terms is as large as possible and is not divisible by .
Formally, choose integers for that maximize subject to .
Input
The first line contains two integers and (, ).
Each of the next lines describes one generator with four integers , , and (, ).
Output
If no choice of terms gives a sum that is not divisible by , print a single line containing -1.
Otherwise print the maximum sum on the first line, and on the second line the indices separated by single spaces ().
Several sets of indices can reach the same maximum sum, so print exactly the one this rule produces. Let be the largest value that occurs in sequence , and let .
- If , then and every generator takes the largest value of its own sequence.
- Otherwise, for each generator let be the largest value occurring in sequence with , and let . A generator with no such is left out of this step. If no generator has such a , print -1. Otherwise let be the smallest and let be the smallest index with . Then , generator takes the value , and every other generator takes the largest value of its own sequence.
For each generator, print the smallest index at which the value it takes occurs in its sequence.
Notes
In the first example the first generator produces 1, 2, 3, 4, 5, 0, 1, 2, ... and the second generator produces 2, 3, 2, 3, 2, ....
In the second example the first generator produces 0, 2, 0, 2, 0, ... and the second generator produces 2, 4, 2, 4, 2, ....