#P9817. ******D

******D

Background

Welcome, new “douyou”

https://www.luogu.com.cn/user/358957

Problem Description

Define a non-empty sequence p1,p2,,pmp_1,p_2,\ldots,p_m of length mm to be chaotic if and only if it satisfies the following two conditions.

  • The sum of all elements does not exceed nn, i.e. i=1mpin\sum_{i=1}^m p_i\le n.
  • For every element pip_i, we have pi=1p_i=1 or pip_i is a prime number.

Define the chaos value of a chaotic sequence p1,p2,,pmp_1,p_2,\ldots,p_m as the sum of squares of each element minus kk, i.e. i=1m(pik)2\sum_{i=1}^m (p_i-k)^2.

In particular, define the chaos value of a non-chaotic sequence as 00.

Now given two positive integers n,kn,k, 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 TT, the number of test cases. Then TT test cases follow.

For each test case, input one line with two positive integers n,kn,k.

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 1,2,3,41,2,3,4 in the samples, one possible sequence achieving the maximum chaos value is (1)(1), (2)(2), (1,3)(1,3), (5)(5), respectively.

Constraints and Notes

Test Point ID TT nn kk Special Property
11 =100=100 10\le 10 10\le 10 None
22 =200=200 30\le 30
33 =300=300 103\le 10^3 5×104\le 5\times 10^4
44 =400=400 105\le 10^5
55 =500=500 107\le 10^7
66 =600=600 109\le 10^9 =1=1 nn is prime
77 =700=700 None
88 =800=800 =44444=44444
99 =900=900 5×104\le 5\times 10^4 nn is prime
1010 =103=10^3 None

For all test points, it is guaranteed that 1T1031\le T\le 10^3, 1n1091\le n\le 10^9, and 1k5×1041\le k\le 5\times 10^4.

Translated by ChatGPT 5