#P8447. 「FAOI-R1」完美的平方数

「FAOI-R1」完美的平方数

Problem Description

Given a positive integer mm. There are QQ queries. In each query, a positive integer nn is given. You need to pick some numbers from 1,4,9,16,,m21,4,9,16,\ldots,m^2 (the same number may be picked multiple times) such that their sum is exactly nn. Find the minimum number of picked numbers. (If there is no solution, output 1-1.)

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, representing the number of test cases.

For each test case:

The first line contains two positive integers m,Qm, Q.

The next QQ lines each contain one positive integer nn.

Output Format

For each query in each test case, output one line with one positive integer, which is the answer.

5
1 5
1
2
3
4
5
5 5
8
20
25
37
49
11 1
179
13 1
507
19 1
841
1
2
3
4
5
2
2
1
4
4
3
3
4

Hint

Sample explanation:

For the first test case, obviously the answer is nn, because you can only pick 11.

For the second test case:

  • 8=22+228 = 2^2 + 2^2;
  • 20=42+2220 = 4^2 + 2^2;
  • 25=5225 = 5^2;
  • 37=52+22+22+2237 = 5^2 + 2^2 + 2^2 + 2^2; (or 37=42+42+22+1237 = 4^2 + 4^2 + 2^2 + 1^2)
  • 49=52+42+22+2249 = 5^2 + 4^2 + 2^2 + 2^2; (or 49=42+42+42+1249 = 4^2 + 4^2 + 4^2 + 1^2)
  • Note that 37=62+1237 = 6^2 + 1^2 and 49=7249 = 7^2 are both invalid, because in this test case m=5m = 5.

Test Point ID mm \le nn \le Score
11 3030 10410^4 4040
232 \sim 3 101810^{18} 15×215 \times 2
494 \sim 9 500500 5×65 \times 6

For 100%100\% of the testdata, 1T301 \le T \le 30, 1Q1041 \le Q \le 10^4, 1m5001 \le m \le 500, 1n10181 \le n \le 10^{18}. In a single test point, the sum of mm over all test cases (m\sum m) satisfies 1m5001 \le \sum m \le 500.

Translated by ChatGPT 5