#P11037. 【MX-X3-T4】「RiOI-4」上课
【MX-X3-T4】「RiOI-4」上课
Background
Original problem link: https://oier.team/problems/X3E。
One day, Little M got up in the dorm at , and the morning self-study starts at 。
Problem Description
Given positive integers and intervals 。
There are queries. In each query, an integer is given. Choose an integer in each interval (), so that the sum of all chosen integers equals , and the variance of the chosen sequence is minimized. Output the minimum variance modulo . It is guaranteed that there is at least one valid selection.
For the definition of variance, refer to this Luogu paste. For taking a rational number modulo, refer to [Template] Rational Modulo。
Input Format
The first line contains two positive integers 。
The next lines each contain two natural numbers 。
The last lines each contain an integer , representing one query. It is guaranteed that there exists at least one valid selection。
Output Format
Output lines, each containing one integer: the minimum variance modulo 。
3 3
1 3
2 3
3 5
6
9
11
665496236
0
554580197
4 3
1 4
11 12
3 9
6 10
21
29
26
811073551
811073543
748683272
Hint
Sample Explanation #1
For query 1, a selection with minimum variance is . The minimum variance is . Since $665\,496\,236 \times 3 \equiv 2 \pmod{998\,244\,353}$, output 。
For query 2, a selection with minimum variance is . The minimum variance is . Since , output 。
For query 3, a selection with minimum variance is . The minimum variance is . Since $554\,580\,197 \times 9 \equiv 8 \pmod{998\,244\,353}$, output 。
Constraints
This problem uses bundled testdata.
| Subtask | Score | Special Property | ||
|---|---|---|---|---|
For all testdata, , . For each , it is guaranteed that there exists a valid selection.
Translated by ChatGPT 5