luogu#P9560. [SDCPC2023] Math Problem
[SDCPC2023] Math Problem
题目描述
Given two positive integers and , you can perform the following two types of operations any number of times (including zero times):
- Choose an integer which satisfies , and change into . It will cost you coins to perform this operation once. The integer you choose each time can be different.
- Change into . It will cost you coins to perform this operation once. Note that is the largest integer which is less than or equal to .
Given a positive integer , calculate the minimum number of coins needed to change into a multiple of . Please note that is a multiple of any positive integer.
输入格式
There are multiple test cases. The first line of the input contains an integer () indicating the number of test cases. For each test case:
The first line contains five integers , , , , (, ).
输出格式
For each test case output one line containing one integer, indicating the minimum number of coins needed to change into a multiple of . If this goal cannot be achieved, output instead.
题目大意
【题目描述】
给定两个正整数 和 ,您可以进行以下两种操作任意次(包括零次):
- 选择一个整数 满足 ,将 变为 。该操作每次花费 枚金币。每次选择的整数 可以不同。
- 将 变为 。该操作每次花费 枚金币。其中 表示小于等于 的最大整数。
给定正整数 ,求将 变为 的倍数最少需要花费几枚金币。请注意: 是任何正整数的倍数。
【输入格式】
有多组测试数据。第一行输入一个整数 ()表示测试数据组数。对于每组测试数据:
第一行输入五个正整数 ,,,,(,)。
【输出格式】
每组数据输出一行一个整数,代表将 变为 的倍数最少需要花费几枚金币。如果无法完成该目标,输出 。
【样例解释】
对于第一组样例数据,一开始 ,最优操作如下:
- 首先进行一次第二种操作,将 变为 ,花费 枚金币。
- 接下来进行一次第一种操作,选择 ,将 变为 ,花费 枚金币。
- 接下来进行一次第一种操作,选择 ,将 变为 ,花费 枚金币。
- 此时 ,满足 是 的倍数。共花费 枚金币。
对于第二组样例数据,进行两次第二种操作将 变为 。共花费 枚金币。
对于第三组样例数据,因为 已经是 的倍数,因此无需进行任何操作。共花费 枚金币。
4
101 4 207 3 5
8 3 16 100 1
114 514 19 19 810
1 1 3 1 1
11
2
0
-1
提示
For the first sample test case, initially . The optimal steps are shown as follows:
- Firstly, perform the second type of operation once. Change into . This step costs coins.
- Then, perform the first type of operation once. Choose and change into . This step costs coins.
- Then, perform the first type of operation once. Choose and change into . This step costs coins.
- As , is a multiple of . The total cost is coins.
For the second sample test case, perform the second type of operation twice will change into . The total cost is coins.
For the third sample test case, as is already a multiple of , no operation is needed. The total cost is coins.