#P6511. [QkOI#R1] Quark and Equations

[QkOI#R1] Quark and Equations

Description

Given n,mn, m, find how many pairs of positive integers (i,j)(i, j) satisfy the following system of equations:

$$\begin{cases} i+j=n \\ \lfloor\frac{i}{j}\rfloor+\lceil\frac{j}{i}\rceil=m \end{cases}$$

In the above, x\lfloor x\rfloor denotes rounding xx down, and x\lceil x\rceil denotes rounding xx up.

Input Format

This problem contains multiple test cases in a single input file.

The first line contains an integer TT, indicating the number of test cases.

The next TT lines each contain two integers n,mn, m, with the meaning as described in the statement.

Output Format

Your output should contain TT lines.

For each test case, output one integer per line representing your answer.

2
2 2
2 1

1
0
1
6 2

2

Hint

Sample Explanation

When n=m=2n=m=2, only (1,1)(1,1) satisfies the conditions.
When n=2,m=1n=2,m=1, there is no solution.
When n=6,m=2n=6,m=2, only (2,4)(2,4) and (3,3)(3,3) satisfy the conditions.


Constraints

This problem uses bundled testdata.

  • Subtask 1 (10 pts): T,n,m500T, n, m\le 500.
  • Subtask 2 (40 pts): T,n,m5000T, n, m\le 5000.
  • Subtask 3 (5 pts): m=1m=1.
  • Subtask 4 (5 pts): m>nm>n.
  • Subtask 5 (5 pts): m[n1,n]m\in[n-1,n].
  • Subtask 6 (35 pts): no special constraints.

For 100%100\% of the testdata, 1T1051\le T\le 10^5, 1n,m1071\le n,m\le 10^{7}.

Translated by ChatGPT 5