310227: CF1798F. Gifts from Grandfather Ahmed

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Gifts from Grandfather Ahmed

题意翻译

你有 $n$ 个盒子,第 $i$ 个盒子里面有 $a_i$ 个礼物。你要把盒子送给 $k$ 个班级,第 $i$ 个班级有 $s_i$ 个学生。保证 $\sum_{i=1}^k s_i=n+1$。 你需要再准备一个盒子(里面的礼物数量由你自己决定),使得存在一种划分盒子的方案,让第 $i$ 个班级恰好收到 $s_i$ 个盒子且其中的礼物总数是 $s_i$ 的倍数。 若无解,输出 `-1`。 若有解,第一行输出一个数 $s$,表示需要准备的盒子中的礼物个数($1\leq s\leq 10^6$)。接下来 $k$ 行,第 $i$ 行输出 $s_i$ 个数,表示给第 $i$ 个班级的每个盒子中的礼物个数。 $1\leq n,k\leq 200$。

题目描述

Grandfather Ahmed's School has $ n+1 $ students. The students are divided into $ k $ classes, and $ s_i $ students study in the $ i $ -th class. So, $ s_1 + s_2 + \ldots + s_k = n+1 $ . Due to the upcoming April Fools' Day, all students will receive gifts! Grandfather Ahmed planned to order $ n+1 $ boxes of gifts. Each box can contain one or more gifts. He plans to distribute the boxes between classes so that the following conditions are satisfied: 1. Class number $ i $ receives exactly $ s_i $ boxes (so that each student can open exactly one box). 2. The total number of gifts in the boxes received by the $ i $ -th class should be a multiple of $ s_i $ (it should be possible to equally distribute the gifts among the $ s_i $ students of this class). Unfortunately, Grandfather Ahmed ordered only $ n $ boxes with gifts, the $ i $ -th of which contains $ a_i $ gifts. Ahmed has to buy the missing gift box, and the number of gifts in the box should be an integer between $ 1 $ and $ 10^6 $ . Help Ahmed to determine, how many gifts should the missing box contain, and build a suitable distribution of boxes to classes, or report that this is impossible.

输入输出格式

输入格式


The first line of the input contains two integers $ n $ and $ k $ ( $ 1 \le n, k \le 200 $ , $ k \le n + 1 $ ). The second line contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \le a_i \le 10^6 $ ) — the number of gifts in the available boxes. The third line contains $ k $ integers $ s_1, s_2, \ldots, s_k $ ( $ 1 \le s_i \le n+1 $ ) — the number of students in classes. It is guaranteed that $ \sum s_i = n+1 $ .

输出格式


If there is no way to buy the remaining box, output the integer $ -1 $ in a single line. Otherwise, in the first line, output a single integer $ s $ — the number of gifts in the box that Grandfather Ahmed should buy ( $ 1 \le s \le 10^6 $ ). Next, in $ k $ lines, print the distribution of boxes to classes. In the $ i $ -th line print $ s_i $ integers — the sizes of the boxes that should be sent to the $ i $ -th class. If there are multiple solutions, print any of them.

输入输出样例

输入样例 #1

4 2
7 7 7 127
2 3

输出样例 #1

1
7 7 
7 127 1

输入样例 #2

18 4
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
6 1 9 3

输出样例 #2

9
7 1 7 6 5 4 
9 
1 2 3 8 3 2 9 8 9 
6 5 4

说明

In the first test, Grandfather Ahmed can buy a box with just $ 1 $ gift. After that, two boxes with $ 7 $ gifts are sent to the first class. $ 7 + 7 = 14 $ is divisible by $ 2 $ . And the second class gets boxes with $ 1, 7, 127 $ gifts. $ 1 + 7 + 127 = 135 $ is evenly divisible by $ 3 $ . In the second test, the classes have sizes $ 6 $ , $ 1 $ , $ 9 $ , and $ 3 $ . We show that the available boxes are enough to distribute into classes with sizes $ 6 $ , $ 9 $ , $ 3 $ , and in the class with size $ 1 $ , you can buy a box of any size. In class with size $ 6 $ we send boxes with sizes $ 7 $ , $ 1 $ , $ 7 $ , $ 6 $ , $ 5 $ , $ 4 $ . $ 7 + 1 + 7 + 6 + 5 + 4 = 30 $ is divisible by $ 6 $ . In class with size $ 9 $ we send boxes with sizes $ 1 $ , $ 2 $ , $ 3 $ , $ 8 $ , $ 3 $ , $ 2 $ , $ 9 $ , $ 8 $ , $ 9 $ . $ 1 + 2 + 3 + 8 + 3 + 2 + 9 + 8 + 9 = 45 $ is divisible by $ 9 $ . The remaining boxes ( $ 6 $ , $ 5 $ , $ 4 $ ) are sent to the class with size $ 3 $ . $ 6 + 5 + 4 = 15 $ is divisible by $ 3 $ .

Input

题意翻译

你有 $n$ 个盒子,第 $i$ 个盒子里面有 $a_i$ 个礼物。你要把盒子送给 $k$ 个班级,第 $i$ 个班级有 $s_i$ 个学生。保证 $\sum_{i=1}^k s_i=n+1$。 你需要再准备一个盒子(里面的礼物数量由你自己决定),使得存在一种划分盒子的方案,让第 $i$ 个班级恰好收到 $s_i$ 个盒子且其中的礼物总数是 $s_i$ 的倍数。 若无解,输出 `-1`。 若有解,第一行输出一个数 $s$,表示需要准备的盒子中的礼物个数($1\leq s\leq 10^6$)。接下来 $k$ 行,第 $i$ 行输出 $s_i$ 个数,表示给第 $i$ 个班级的每个盒子中的礼物个数。 $1\leq n,k\leq 200$。

Output

**标题:来自祖父艾哈迈德的礼物**

**题意翻译:**
你有 $ n $ 个盒子,第 $ i $ 个盒子里面有 $ a_i $ 个礼物。你要把盒子送给 $ k $ 个班级,第 $ i $ 个班级有 $ s_i $ 个学生。保证 $ \sum_{i=1}^k s_i=n+1 $。

你需要再准备一个盒子(里面的礼物数量由你自己决定),使得存在一种划分盒子的方案,让第 $ i $ 个班级恰好收到 $ s_i $ 个盒子且其中的礼物总数是 $ s_i $ 的倍数。

若无解,输出 `-1`。

若有解,第一行输出一个数 $ s $,表示需要准备的盒子中的礼物个数($ 1\leq s\leq 10^6 $)。接下来 $ k $ 行,第 $ i $ 行输出 $ s_i $ 个数,表示给第 $ i $ 个班级的每个盒子中的礼物个数。

$ 1\leq n,k\leq 200 $。

**题目描述:**
祖父艾哈迈德的学校有 $ n+1 $ 个学生。学生们被分成 $ k $ 个班级,第 $ i $ 个班级有 $ s_i $ 个学生。所以,$ s_1 + s_2 + \ldots + s_k = n+1 $。

由于即将到来的愚人节,所有学生都将收到礼物!

祖父艾哈迈德计划订购 $ n+1 $ 个礼物盒。每个盒子可以包含一个或多个礼物。他计划在班级之间分配盒子,以满足以下条件:

1. 第 $ i $ 个班级恰好收到 $ s_i $ 个盒子(以便每个学生可以恰好打开一个盒子)。
2. 第 $ i $ 个班级收到的盒子中的礼物总数应该是 $ s_i $ 的倍数(应该能够将这些礼物平均分配给这个班级的 $ s_i $ 个学生)。

不幸的是,祖父艾哈迈德只订购了 $ n $ 个带礼物的盒子,第 $ i $ 个盒子里面有 $ a_i $ 个礼物。

艾哈迈德必须购买缺失的礼物盒,盒子中的礼物数量应该是 $ 1 $ 到 $ 10^6 $ 之间的整数。帮助艾哈迈德确定缺失盒子应包含多少礼物,并构建一个合适的盒子分配方案给班级,或者报告这是不可能的。

**输入输出格式:**

**输入格式:**
第一行包含两个整数 $ n $ 和 $ k $ ($ 1 \le n, k \le 200 $, $ k \le n + 1 $)。

第二行包含 $ n $ 个整数 $ a_1, a_2, \ldots, a_n $ ($ 1 \le a_i \le 10^6 $) — 可用盒子中的礼物数量。

第三行包含 $ k $ 个整数 $ s_1, s_2, \ldots, s_k $ ($ 1 \le s_i \le n+1 $) — 各个班级的学生数量。保证 $ \sum s_i = n+1 $。

**输出格式:**
如果没有办法购买剩余的盒子,则在单独一行中输出整数 $ -1 $。

否则,在第一行中输出一个整数 $ s $ — 祖父艾哈迈德应该购买的盒子中的礼物数量 ($ 1 \le s \le 10^6 $)。

接下来,在 $ k $ 行中,打印班级的盒子分配。在第 $ i $ 行打印 $ s_i $ 个整数 — 应该发送给第 $ i $ 个班级的盒子的尺寸。

如果有多个解决方案,请打印其中任何一个。

**输入输出样例:**

**输入样例 #1:**
```
4 2
7 7 7 127
2 3
```
**输出样例 #1:**
```
1
7 7
7 127 1
```

**输入样例 #2:**
```
18 4
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
6 1 9 3
```
**输出样例 #2:**
```
9
7 1 7 6 5 4
9
1 2 3 8 3 2 9 8 9
6 5 4
```

**说明:**
在第一个测试中,祖父艾哈迈德可以购买一个只包含 $ 1 $ 个礼物的盒子。之后,两个包含 $ 7 $ 个礼物的盒子被送到第一个班级。$ 7 + 7 = 14 $ 可以被 $ 2 $ 整除。第二个班级收到包含 $ 1, 7, 127 $ 礼物的盒子**标题:来自祖父艾哈迈德的礼物** **题意翻译:** 你有 $ n $ 个盒子,第 $ i $ 个盒子里面有 $ a_i $ 个礼物。你要把盒子送给 $ k $ 个班级,第 $ i $ 个班级有 $ s_i $ 个学生。保证 $ \sum_{i=1}^k s_i=n+1 $。 你需要再准备一个盒子(里面的礼物数量由你自己决定),使得存在一种划分盒子的方案,让第 $ i $ 个班级恰好收到 $ s_i $ 个盒子且其中的礼物总数是 $ s_i $ 的倍数。 若无解,输出 `-1`。 若有解,第一行输出一个数 $ s $,表示需要准备的盒子中的礼物个数($ 1\leq s\leq 10^6 $)。接下来 $ k $ 行,第 $ i $ 行输出 $ s_i $ 个数,表示给第 $ i $ 个班级的每个盒子中的礼物个数。 $ 1\leq n,k\leq 200 $。 **题目描述:** 祖父艾哈迈德的学校有 $ n+1 $ 个学生。学生们被分成 $ k $ 个班级,第 $ i $ 个班级有 $ s_i $ 个学生。所以,$ s_1 + s_2 + \ldots + s_k = n+1 $。 由于即将到来的愚人节,所有学生都将收到礼物! 祖父艾哈迈德计划订购 $ n+1 $ 个礼物盒。每个盒子可以包含一个或多个礼物。他计划在班级之间分配盒子,以满足以下条件: 1. 第 $ i $ 个班级恰好收到 $ s_i $ 个盒子(以便每个学生可以恰好打开一个盒子)。 2. 第 $ i $ 个班级收到的盒子中的礼物总数应该是 $ s_i $ 的倍数(应该能够将这些礼物平均分配给这个班级的 $ s_i $ 个学生)。 不幸的是,祖父艾哈迈德只订购了 $ n $ 个带礼物的盒子,第 $ i $ 个盒子里面有 $ a_i $ 个礼物。 艾哈迈德必须购买缺失的礼物盒,盒子中的礼物数量应该是 $ 1 $ 到 $ 10^6 $ 之间的整数。帮助艾哈迈德确定缺失盒子应包含多少礼物,并构建一个合适的盒子分配方案给班级,或者报告这是不可能的。 **输入输出格式:** **输入格式:** 第一行包含两个整数 $ n $ 和 $ k $ ($ 1 \le n, k \le 200 $, $ k \le n + 1 $)。 第二行包含 $ n $ 个整数 $ a_1, a_2, \ldots, a_n $ ($ 1 \le a_i \le 10^6 $) — 可用盒子中的礼物数量。 第三行包含 $ k $ 个整数 $ s_1, s_2, \ldots, s_k $ ($ 1 \le s_i \le n+1 $) — 各个班级的学生数量。保证 $ \sum s_i = n+1 $。 **输出格式:** 如果没有办法购买剩余的盒子,则在单独一行中输出整数 $ -1 $。 否则,在第一行中输出一个整数 $ s $ — 祖父艾哈迈德应该购买的盒子中的礼物数量 ($ 1 \le s \le 10^6 $)。 接下来,在 $ k $ 行中,打印班级的盒子分配。在第 $ i $ 行打印 $ s_i $ 个整数 — 应该发送给第 $ i $ 个班级的盒子的尺寸。 如果有多个解决方案,请打印其中任何一个。 **输入输出样例:** **输入样例 #1:** ``` 4 2 7 7 7 127 2 3 ``` **输出样例 #1:** ``` 1 7 7 7 127 1 ``` **输入样例 #2:** ``` 18 4 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 6 1 9 3 ``` **输出样例 #2:** ``` 9 7 1 7 6 5 4 9 1 2 3 8 3 2 9 8 9 6 5 4 ``` **说明:** 在第一个测试中,祖父艾哈迈德可以购买一个只包含 $ 1 $ 个礼物的盒子。之后,两个包含 $ 7 $ 个礼物的盒子被送到第一个班级。$ 7 + 7 = 14 $ 可以被 $ 2 $ 整除。第二个班级收到包含 $ 1, 7, 127 $ 礼物的盒子

加入题单

算法标签: