CF2178H.Create or Duplicate

NOI/NOI+/CTSC

通过率:0%

时间限制:6.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 aa, bb, and cc. Initially, Santa has exactly one present of each type.

You are given two integers mm and kk, 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 xx mana, where x∈a,b,cx\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 kk mana.

Santa wants to perform a sequence of spells such that the sum of the values of all presents is a multiple of mm 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.

圣诞老人发现手绘圆圈耗时太久,于是转而求助魔法来完成他的生产配额。

共有三种礼物,其价值分别为 aa、bb 和 cc。初始时,圣诞老人恰好拥有每种礼物各一个。

给定两个整数 mm 和 kk,分别表示圣诞老人最钟爱的数字以及复制操作的代价。圣诞老人可以任意次数(包括零次)施放以下两类法术:

  1. 创造 —— 选择一种类型的礼物,并额外生成一个该类型的礼物。此法术消耗 xx 点法力,其中 x∈{a,b,c}x\in \{a,b,c\} 是所选类型礼物的价值。
  2. 复制 —— 选择一种类型的礼物,并将该类型的所有现有礼物全部复制一份(即数量翻倍)。此法术消耗 kk 点法力。

圣诞老人希望执行一系列法术,使得法术后所有礼物的价值总和是 mm 的倍数。

请确定圣诞老人达成目标所需的最小法力值。在本题给定的约束条件下,可以证明这样的法术序列一定存在。

输入格式

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

The only line of each test case contains five integers aa, bb, cc, mm, and kk (1≤a<b<c<m≤5⋅1051\leq a \lt b \lt c \lt m\leq 5\cdot 10^5, 1≤k≤5⋅1051\leq k\leq 5\cdot 10^5).

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

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例仅有一行,包含五个整数 aa、bb、cc、mm 和 kk(1≤a<b<c<m≤5⋅1051\leq a \lt b \lt c \lt m\leq 5\cdot 10^5,1≤k≤5⋅1051\leq k\leq 5\cdot 10^5)。

保证所有测试用例中 mm 的总和不超过 5⋅1055\cdot 10^5。

保证所有测试用例中 kk 的总和不超过 5⋅1055\cdot 10^5。

输出格式

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 mm.

对于每个测试用例,输出一个整数——圣诞老人使所有礼物价值之和为 mm 的倍数所需的最少法力值。

输入输出样例

  • 输入#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

    输出#1

    10
    0
    17
    9
    1227
    35116
    7050242

说明/提示

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

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

Operation

Value

ℓ\ell after operation

sum⁡(ℓ)\operatorname{sum}(\ell)

Cost

0

—

—

[1,2,3][1, 2, 3]

66

—

1

Create

33

[1,2,3,3][1, 2, 3, 3]

99

33

2

Create

33

[1,2,3,3,3][1, 2, 3, 3, 3]

1212

33

3

Duplicate

33

[1,2,3,3,3,3,3,3][1, 2, 3, 3, 3, 3, 3, 3]

2121

44

21=7⋅321=7\cdot 3

Total: 1010

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

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

Operation

Value

ℓ\ell after operation

sum⁡(ℓ)\operatorname{sum}(\ell)

Cost

0

—

—

[3,12,14][3, 12, 14]

2929

—

1

Duplicate

1212

[3,12,12,14][3, 12, 12, 14]

4141

11

2

Duplicate

1414

[3,12,12,14,14][3, 12, 12, 14, 14]

5555

11

3

Create

1414

[3,12,12,14,14,14][3, 12, 12, 14, 14, 14]

6969

1414

4

Duplicate

33

[3,3,12,12,14,14,14][3, 3, 12, 12, 14, 14, 14]

7272

11

72=18⋅472=18\cdot 4

Total: 1717

设 ℓ\ell 为礼物价值的列表,sum⁡(ℓ)\operatorname{sum}(\ell) 表示 ℓ\ell 中所有元素的和。

在第一个测试用例中,以下是一种最优的操作序列:

操作

值

操作后的 ℓ\ell

sum⁡(ℓ)\operatorname{sum}(\ell)

代价

0

—

—

[1,2,3][1, 2, 3]

66

—

1

创建

33

[1,2,3,3][1, 2, 3, 3]

99

33

2

创建

33

[1,2,3,3,3][1, 2, 3, 3, 3]

1212

33

3

复制

33

[1,2,3,3,3,3,3,3][1, 2, 3, 3, 3, 3, 3, 3]

2121

44

21=7⋅321=7\cdot 3

总计:1010

在第二个测试用例中,最优策略是不执行任何操作,因为 3+4+5=123+4+5=12 已经是 1212 的倍数。

在第三个测试用例中,以下是一种最优的操作序列:

操作

值

操作后的 ℓ\ell

sum⁡(ℓ)\operatorname{sum}(\ell)

代价

0

—

—

[3,12,14][3, 12, 14]

2929

—

1

复制

1212

[3,12,12,14][3, 12, 12, 14]

4141

11

2

复制

1414

[3,12,12,14,14][3, 12, 12, 14, 14]

5555

11

3

创建

1414

[3,12,12,14,14,14][3, 12, 12, 14, 14, 14]

6969

1414

4

复制

33

[3,3,12,12,14,14,14][3, 3, 12, 12, 14, 14, 14]

7272

11

72=18⋅472=18\cdot 4

总计:1717

输入解题思路,AI测评打分。不知道怎么写?

首页