/
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
N 個のケーキがあり、 i 個目のケーキの大きさは A_i です。これらのケーキを M 人に配ろうとしています。
ケーキの大きさの総和 A_1+\dots+A_N を S とします。 ここで、 S は M の倍数であることが保証されます。
あなたは、以下の操作を好きな順序で好きな回数( 0 回でもよい)行うことができます。
- ケーキを 1 つ選び、それを自由に 2 つに切り分ける(大きさが整数である必要はない)。
操作後、ケーキを M 人に配ります。どの人がもらうケーキの大きさの合計も \frac{S}{M} となるようにするための、操作回数の最小値を求めてください。
より厳密な問題文は以下の通りです。
- 列 A=(A_1,\dots,A_N) と正整数 M が与えられる。S=\sum_i A_i と定める。
- 以下の操作を好きな回数行う。
- 1\leq i \leq |A| を満たす整数 i と 0 < x < A_i を満たす実数 x を自由に選ぶ。A の末尾に x,A_i-x を追加し、 i 番目の要素を削除した長さ |A|+1 の列で A を置き換える。
- 以下の条件を全て満たす \mathcal{P} が存在するための最小の操作回数を求めよ。
- \mathcal{P} は \{1,2,\dots,|A|\} の分割である。即ち、以下の 3 条件を満たす集合である。
- \mathcal{P} の各要素は \{1,2,\dots,|A|\} の部分集合である。
- X,Y \in \mathcal{P} かつ X\neq Y ならば、X \cap Y = \emptyset である。
- \bigcup_{X \in \mathcal{P}}X=\{1,2,\dots,|A|\} が成り立つ。
- すべての X\in \mathcal{P} について、\sum_{i\in X}A_i=\frac{S}{M} が成り立つ。
- \mathcal{P} は \{1,2,\dots,|A|\} の分割である。即ち、以下の 3 条件を満たす集合である。
制約
- 1 \leq N \leq 20
- 1 \leq M \leq 100
- 1 \leq A_i \leq 10^9
- A_1+\dots+A_N は M の倍数
- 与えられる数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
2 3 7 23
出力例 1
2
次のように 2 回の操作を行うのが最小です。
- 大きさ 23 のケーキを大きさ 10,13 に切り分ける。ケーキの大きさはそれぞれ (7,10,13) になる。
- 大きさ 13 のケーキを大きさ 10,3 に切り分ける。ケーキの大きさはそれぞれ (7,10,10,3) になる。
- 「大きさ 7 のケーキと大きさ 3 のケーキ」「大きさ 10 のケーキ」「大きさ 10 のケーキ」をそれぞれの人に与える。
入力例 2
4 2 1234 123 12 1
出力例 2
1
次のように 1 回の操作を行うのが最小です。
- 大きさ 1234 のケーキを大きさ 685,549 に切り分ける。ケーキの大きさはそれぞれ (123,12,1,685,549) になる。
- 「大きさ 123,12,1,549 の 4 個のケーキ」「大きさ 685 のケーキ」をそれぞれの人に与える。
Score : 525 points
Problem Statement
There are N cakes, and the size of the i-th cake is A_i. You are going to distribute these cakes to M people.
Let S be the total size of the cakes, A_1+\dots+A_N. Here, it is guaranteed that S is a multiple of M.
You can perform the following operation any number of times (possibly zero) in any order.
- Choose one cake and cut it into 2 pieces in any way you like (the sizes need not be integers).
After the operations, you distribute the cakes to the M people. Find the minimum number of operations required so that the total size of the cakes each person receives is \frac{S}{M}.
A more formal statement is as follows.
- You are given a sequence A=(A_1,\dots,A_N) and a positive integer M. Let S=\sum_i A_i.
- You can perform the following operation any number of times.
- Choose an integer i with 1\leq i \leq |A| and a real number x with 0 < x < A_i. Replace A with the sequence of length |A|+1 obtained by appending x,A_i-x to the end of A and removing the i-th element.
- Find the minimum number of operations required for there to exist a \mathcal{P} satisfying all of the following conditions.
- \mathcal{P} is a partition of \{1,2,\dots,|A|\}; that is, it is a set satisfying the following 3 conditions.
- Each element of \mathcal{P} is a subset of \{1,2,\dots,|A|\}.
- If X,Y \in \mathcal{P} and X\neq Y, then X \cap Y = \emptyset.
- \bigcup_{X \in \mathcal{P}}X=\{1,2,\dots,|A|\} holds.
- \sum_{i\in X}A_i=\frac{S}{M} holds for every X\in \mathcal{P}.
- \mathcal{P} is a partition of \{1,2,\dots,|A|\}; that is, it is a set satisfying the following 3 conditions.
Constraints
- 1 \leq N \leq 20
- 1 \leq M \leq 100
- 1 \leq A_i \leq 10^9
- A_1+\dots+A_N is a multiple of M.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
2 3 7 23
Sample Output 1
2
The minimum is achieved by performing 2 operations as follows.
- Cut the cake of size 23 into pieces of sizes 10 and 13. The cake sizes become (7,10,13).
- Cut the cake of size 13 into pieces of sizes 10 and 3. The cake sizes become (7,10,10,3).
- Give the cakes of sizes 7 and 3 to one person, a cake of size 10 to another person, and the remaining cake of size 10 to the last person.
Sample Input 2
4 2 1234 123 12 1
Sample Output 2
1
The minimum is achieved by performing 1 operation as follows.
- Cut the cake of size 1234 into pieces of sizes 685 and 549. The cake sizes become (123,12,1,685,549).
- Give the 4 cakes of sizes 123,12,1,549 to one person and the cake of size 685 to the other.