#P9817. ******D
******D
Background
Welcome, new “douyou”
Problem Description
Define a non-empty sequence of length to be chaotic if and only if it satisfies the following two conditions.
- The sum of all elements does not exceed , i.e. .
- For every element , we have or is a prime number.
Define the chaos value of a chaotic sequence as the sum of squares of each element minus , i.e. .
In particular, define the chaos value of a non-chaotic sequence as .
Now given two positive integers , ask: among all sequences, what is the maximum possible chaos value, and output that maximum value.
Input Format
This problem has multiple test cases. The first line contains a positive integer , the number of test cases. Then test cases follow.
For each test case, input one line with two positive integers .
Output Format
For each test case, output one line with one integer, the answer.
5
1 1
2 1
4 1
5 2
10 10
0
1
4
9
810
Hint
Sample Explanation
For test cases in the samples, one possible sequence achieving the maximum chaos value is , , , , respectively.
Constraints and Notes
| Test Point ID | Special Property | |||
|---|---|---|---|---|
| None | ||||
| is prime | ||||
| None | ||||
| is prime | ||||
| None |
For all test points, it is guaranteed that , , and .
Translated by ChatGPT 5