#P9560. [SDCPC2023] Math Problem

[SDCPC2023] Math Problem

题目描述

Given two positive integers nn and kk, you can perform the following two types of operations any number of times (including zero times):

  • Choose an integer xx which satisfies 0x<k0 \leq x < k, and change nn into kn+xk\cdot n+x. It will cost you aa coins to perform this operation once. The integer xx you choose each time can be different.
  • Change nn into nk\lfloor \frac{n}{k} \rfloor. It will cost you bb coins to perform this operation once. Note that nk\lfloor \frac{n}{k} \rfloor is the largest integer which is less than or equal to nk\frac{n}{k}.

Given a positive integer mm, calculate the minimum number of coins needed to change nn into a multiple of mm. Please note that 00 is a multiple of any positive integer.

输入格式

There are multiple test cases. The first line of the input contains an integer TT (1T1051\leq T\leq 10^5) indicating the number of test cases. For each test case:

The first line contains five integers nn, kk, mm, aa, bb (1n10181\leq n\leq 10^{18}, 1k,m,a,b1091\leq k, m, a, b\leq 10^9).

输出格式

For each test case output one line containing one integer, indicating the minimum number of coins needed to change nn into a multiple of mm. If this goal cannot be achieved, output 1-1 instead.

题目大意

【题目描述】

给定两个正整数 nnkk,您可以进行以下两种操作任意次(包括零次):

  • 选择一个整数 xx 满足 0x<k0 \leq x < k,将 nn 变为 kn+xk\cdot n+x。该操作每次花费 aa 枚金币。每次选择的整数 xx 可以不同。
  • nn 变为 nk\lfloor \frac{n}{k} \rfloor。该操作每次花费 bb 枚金币。其中 nk\lfloor \frac{n}{k} \rfloor 表示小于等于 nk\frac{n}{k} 的最大整数。

给定正整数 mm,求将 nn 变为 mm 的倍数最少需要花费几枚金币。请注意:00 是任何正整数的倍数。

【输入格式】

有多组测试数据。第一行输入一个整数 TT1T1051\leq T\leq 10^5)表示测试数据组数。对于每组测试数据:

第一行输入五个正整数 nnkkmmaabb1n10181\leq n\leq 10^{18}1k,m,a,b1091\leq k, m, a, b\leq 10^9)。

【输出格式】

每组数据输出一行一个整数,代表将 nn 变为 mm 的倍数最少需要花费几枚金币。如果无法完成该目标,输出 1-1

【样例解释】

对于第一组样例数据,一开始 n=101n=101,最优操作如下:

  • 首先进行一次第二种操作,将 nn 变为 n4=25\lfloor \frac{n}{4} \rfloor=25,花费 55 枚金币。
  • 接下来进行一次第一种操作,选择 x=3x = 3,将 nn 变为 4n+3=1034\cdot n+3=103,花费 33 枚金币。
  • 接下来进行一次第一种操作,选择 x=2x = 2,将 nn 变为 4n+2=4144\cdot n+2=414,花费 33 枚金币。
  • 此时 414=2×207414=2 \times 207,满足 nnmm 的倍数。共花费 5+3+3=115+3+3=11 枚金币。

对于第二组样例数据,进行两次第二种操作将 nn 变为 00。共花费 1+1=21 + 1 = 2 枚金币。

对于第三组样例数据,因为 n=114=6×19n = 114 = 6 \times 19 已经是 mm 的倍数,因此无需进行任何操作。共花费 00 枚金币。

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 n=101n=101. The optimal steps are shown as follows:

  • Firstly, perform the second type of operation once. Change nn into n4=25\lfloor \frac{n}{4} \rfloor=25. This step costs 55 coins.
  • Then, perform the first type of operation once. Choose x=3x = 3 and change nn into 4n+3=1034\cdot n+3=103. This step costs 33 coins.
  • Then, perform the first type of operation once. Choose x=2x = 2 and change nn into 4n+2=4144\cdot n+2=414. This step costs 33 coins.
  • As 414=2×207414=2 \times 207, nn is a multiple of mm. The total cost is 5+3+3=115+3+3=11 coins.

For the second sample test case, perform the second type of operation twice will change nn into 00. The total cost is 1+1=21 + 1 = 2 coins.

For the third sample test case, as n=114=6×19n = 114 = 6 \times 19 is already a multiple of mm, no operation is needed. The total cost is 00 coins.