CF2057A.MEX Table

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

MEX Table

One day, the schoolboy Mark misbehaved, so the teacher Sasha called him to the whiteboard.

Sasha gave Mark a table with nn rows and mm columns. His task is to arrange the numbers 0,1,,nm10, 1, \ldots, n \cdot m - 1 in the table (each number must be used exactly once) in such a way as to maximize the sum of MEX^{\text{∗}} across all rows and columns. More formally, he needs to maximize $$ um\limits_{i = 1}^{n} \operatorname{mex}({a_{i,1}, a_{i,2}, \ldots, a_{i,m}}) + um\limits_{j = 1}^{m} \operatorname{mex}({a_{1,j}, a_{2,j}, \ldots, a_{n,j}}),$$wherea_i,ja\_{i,j}is the number in theii-th row andjj-th column.

Sasha is not interested in how Mark arranges the numbers, so he only asks him to state one number — the maximum sum of MEX across all rows and columns that can be achieved.

^{\text{∗}}The minimum excluded (MEX) of a collection of integersc_1,c_2,,c_kc\_1, c\_2, \ldots, c\_kis defined as the smallest non-negative integerxxwhich does not occur in the collectioncc.

For example:

  • mex([2,2,1])=0\operatorname{mex}([2,2,1])= 0, since00does not belong to the array.
  • mex([3,1,0,1])=2\operatorname{mex}([3,1,0,1]) = 2, since00and11belong to the array, but22does not.
  • mex([0,3,1,2])=4\operatorname{mex}([0,3,1,2]) = 4, since00,11,22, and33belong to the array, but44 does not.</p>

    Input

    Each test contains multiple test cases. The first line contains a single integer tt (1t10001 \le t \le 1000) — the number of test cases. The description of the test cases follows.

    The first line of each test case contains two integers nn and mm (1n,m1091 \le n, m \le 10^9) — the number of rows and columns in the table, respectively.

    Output

    For each test case, output the maximum possible sum of mex\operatorname{mex} across all rows and columns.

    Note

    In the first test case, the only element is 00, and the sum of the mex\operatorname{mex} of the numbers in the first row and the mex\operatorname{mex} of the numbers in the first column is $\operatorname{mex}(\{0\}) + \operatorname{mex}(\{0\}) = 1 + 1 = 2$.

    In the second test case, the optimal table may look as follows:

    33 00
    22 11

    Then $um\limits_{i = 1}^{n} \operatorname{mex}(\{a_{i,1}, a_{i,2}, \ldots, a_{i,m}\}) + um\limits_{j = 1}^{m} \operatorname{mex}(\{a_{1,j}, a_{2,j}, \ldots, a_{n,j}\}) = \operatorname{mex}(\{3, 0\}) + \operatorname{mex}(\{2, 1\})$ $+ \operatorname{mex}(\{3, 2\}) + \operatorname{mex}(\{0, 1\}) = 1 + 0 + 0 + 2 = 3$.

    Samples

    3
    1 1
    2 2
    3 5
    
    2
    3
    6
    

在线编程 IDE

建议全屏模式获得最佳体验