Good Bye 2025
H
Create or Duplicate
Codeforces
3300
原题
← GI →
翻译中…

Santa realized that hand-drawing circles takes too long, so he has turned to magic to meet his production quotas.

翻译中…

There are three types of presents with distinct values $a$, $b$, and $c$. Initially, Santa has exactly one present of each type.

翻译中…

You are given two integers $m$ and $k$, representing Santa's favorite number and the cost of duplication, respectively. Santa can cast the following two types of spells any number of times (possibly zero):

翻译中…
  1. Create — Choose a type of present and create one additional present of that type. This spell costs $x$ mana, where $x\in \{a,b,c\}$ is the value of the chosen type.
  2. Duplicate — Choose a type of present and duplicate all presents of that type. This spell costs $k$ mana.
翻译中…

Santa wants to perform a sequence of spells such that the sum of the values of all presents is a multiple of $m$ after the spells.

翻译中…

Determine the minimum amount of mana Santa needs to achieve this. It can be shown that, under the given constraints, such a sequence of spells always exists.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The only line of each test case contains five integers $a$, $b$, $c$, $m$, and $k$ ($1\leq a \lt b \lt c \lt m\leq 5\cdot 10^5$, $1\leq k\leq 5\cdot 10^5$).

翻译中…

It is guaranteed that the sum of $m$ over all test cases does not exceed $5\cdot 10^5$.

翻译中…

It is guaranteed that the sum of $k$ over all test cases does not exceed $5\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the minimum amount of mana Santa needs to make the sum of the values of all presents a multiple of $m$.

翻译中…

Note

翻译中…

Let $\ell$ be the list of values of presents and $\operatorname{sum}(\ell)$ be the sum of all elements in $\ell$.

翻译中…

In the first test case, below is an optimal sequence of operations:

翻译中…

#OperationValue$\ell$ after operation$\operatorname{sum}(\ell)$Cost0——$[1, 2, 3]$$6$—1Create$3$$[1, 2, 3, 3]$$9$$3$2Create$3$$[1, 2, 3, 3, 3]$$12$$3$3Duplicate$3$$[1, 2, 3, 3, 3, 3, 3, 3]$$21$$4$$21=7\cdot 3$Total: $10$

翻译中…

In the second test case, it is optimal to not perform any operations because $3+4+5=12$ is already a multiple of $12$.

翻译中…

In the third test case, below is an optimal sequence of operations:

翻译中…

#OperationValue$\ell$ after operation$\operatorname{sum}(\ell)$Cost0——$[3, 12, 14]$$29$—1Duplicate$12$$[3, 12, 12, 14]$$41$$1$2Duplicate$14$$[3, 12, 12, 14, 14]$$55$$1$3Create$14$$[3, 12, 12, 14, 14, 14]$$69$$14$4Duplicate$3$$[3, 3, 12, 12, 14, 14, 14]$$72$$1$$72=18\cdot 4$Total: $17$

翻译中…

样例

Input #1
7
1 2 3 21 4
3 4 5 12 34
3 12 14 18 1
6 7 8 10 3
100 103 282 488 221
307 2000 5096 12018 5764
194093 292793 395323 475619 490151
Output #1
10
0
17
9
1227
35116
7050242
InputCopy OutputCopy
题目信息
题号
2178H
所属比赛
时间限制
6 s
内存限制
1024 MB
标签
位运算
图论
数论
最短路

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。