#P9552. 「CROI · R1」浣熊的小溪

「CROI · R1」浣熊的小溪

Background

"Coming from the sun, and walking into the sunlight; spreading bear paws, embracing the wind, yet at farewell, lowering the head to sing to oneself."
That playful growth by the Dream Maple shore, that unrestrained belief under the sun, has grown stronger with time, echoing in the hearts of countless raccoons.
Sadly, along the upper Ling Stream, one person’s will and various constructions have torn apart the innocent years, carving a lonely barrier.
That clear little stream, those happy days of the past—will they still make one stop in remembrance...

Problem Description

The forest of Raccoon Ridge can be viewed as an n×mn \times m grid. Wastewater discharged by a factory has polluted the Dream Maple Stream (a straight line) that runs through the forest, making the regions it passes through harmful to raccoons. The little raccoon CleverRaccoon, in order to study the harm caused by the wastewater, seeks your help.

Let f(n,m)f(n,m) denote the maximum number of cells that a straight line can pass through in an n×mn \times m grid.

The little raccoon has two kinds of questions to ask you:

  1. Given n,mn,m, find f(n,m)f(n,m).
  2. Given n,m,Qn,m,Q, you need to find nn,mmn'\ge n, m'\ge m such that f(n,m)Qf(n',m')\ge Q, and n×mn'\times m' is as small as possible. Output the minimum value of nmnmn'm'-nm modulo 998244353998244353. The testdata guarantees that f(n,m)<Qf(n,m)<Q.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, indicating that there are TT queries.

For each query:

The first line contains a positive integer opop, indicating the type of the question.

The second line: if op=1op=1, input 22 positive integers nn and mm; if op=2op=2, input 33 positive integers nn, mm, and QQ.

Output Format

Output TT lines. For each query, output one positive integer on a single line, representing the answer to the corresponding question.

2
1
2 3
2
2 3 10
4
12

Hint

Sample Explanation #1

For the first query:

The situation shown in the figure below is an optimal construction. When the Dream Maple Stream passes through a 2×32 \times 3 grid forest, it can pass through at most 44 small cells (the yellow parts are the cells it passes through, and the gray parts are the cells it does not pass through).

The construction below is not optimal. The Dream Maple Stream passes through a vertex between two green cells, so neither of the two green regions is counted as being passed through. Therefore, the Dream Maple Stream passes through only 33 small cells.

For the second query:

As shown in the figure below, only when n=2n'=2, m=9m'=9 can the added number of cells nmnmn'm'-nm be minimized while making the Dream Maple Stream pass through 1010 cells (the left side of the red dashed line is the original forest, and the right side is the added part).

Constraints

This problem uses bundled Subtask testdata.

Subtask nn mm QQ Special Property Score
00 1018\le10^{18} =1=1 2×1018\le2 \times 10^{18} op=1op=1 55
11 op=2op=2
22 1018\le10^{18} op=1op=1 2525
33 op=2op=2
44 109\le10^9 2×109\le2 \times 10^9 No special property 3030
55 1018\le10^{18} 2×1018\le2 \times 10^{18} 1010

For 100%100\% of the data, it is guaranteed that 1T101 \le T \le 10, op{1,2}op \in \{1,2\}, 1n,m10181 \le n,m \le 10^{18}, 2n+mQ2×10182 \le n+m \le Q \le 2 \times 10^{18}.

Translated by ChatGPT 5