#P9034. 「KDOI-04」Again Counting Set
「KDOI-04」Again Counting Set
Background

Problem Description
Little S does not like sets, does not like natural numbers, does not like summation, does not like products, does not like minimum values, does not like maximum values, and does not like , so this problem exists.
Given , find how many multisets of integers satisfy:
- .
- For any , .
- $\displaystyle{\prod_{x \in S} x = \min_{x \in S} x}$.
- $\displaystyle{\sum_{x \in S} x = \min_{x \in S} x + \max_{x \in S} x + {\operatorname{mex}}(S)}$.
Note: is the smallest natural number that does not appear in the set.
Input Format
This problem contains multiple test cases.
The first line of input contains a positive integer , which denotes the number of test cases.
For each test case, the input contains one line with two positive integers .
Output Format
For each test case, output one line with one integer representing the answer.
7
1 4
2 4
5 3
2 100
3 8
20 50
499122178 4
1
2
0
3
5
39
998244353
Hint
Additional Notes.
To help contestants understand the statement better, here are several examples of valid/invalid sets:
- .
This set satisfies the requirements, because $0 \times 1 \times 2 \times 2 = 0 = \min\{0, 1, 2, 2\}$, and , $\min\{0, 1, 2, 2\} + \max\{0, 1, 2, 2\} + \operatorname{mex}\{0, 1, 2, 2\} = 0 + 2 + 3 = 5$.
- .
This set does not satisfy the requirements, because although , $\min\{3, 5\} + \max\{3, 5\} + \operatorname{mex}\{3, 5\} = 3 + 5 + 0 = 8$, we have .
- .
This set does not satisfy the requirements, because although $1 \times 9 \times 1 \times 9 \times 8 \times 1 \times 0 = 0 = \min\{1, 9, 1, 9, 8, 1, 0\}$, its sum is , not .
Constraints
For of the testdata, it is guaranteed that , .
| Test Point ID | Score | |||
|---|---|---|---|---|
Translated by ChatGPT 5