#P10252. 线性变换

    ID: 10944 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>数学洛谷原创O2优化洛谷月赛分类讨论

线性变换

Problem Description

You are given three non-negative integers x,a,bx, a, b. As long as x0x \ge 0, you may perform the following operation on xx any number of times (including 00 times):

  • Change xx to axbax - b.

You need to find the minimum value of xx that you can obtain through these operations.

Input Format

This problem contains multiple test cases.

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

For each test case, one line contains three integers x,a,bx, a, b.

Output Format

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

2
6 2 4
5 3 16
6
-1

Hint

Sample explanation: For the first test case, $x = 6 \rightarrow 8 \rightarrow 12 \rightarrow \cdots$. Obviously, x=6x = 6 is the minimum value.

For the second test case, x=51x = 5 \rightarrow -1, and x=1x = -1 is the minimum value.


Constraints:

For 30%30\% of the testdata, T5T \le 5, and x,a,b100x, a, b \le 100.

For another 20%20\% of the testdata, a>ba > b.

For another 20%20\% of the testdata, b=0b = 0.

For 100%100\% of the testdata, 1T10001 \le T \le 1000, and 0x,a,b1090 \le x, a, b \le 10^{9}.

Translated by ChatGPT 5