CF1361B.Johnny and Grandmaster
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Johnny has just found the new, great tutorial: "How to become a grandmaster?". The tutorial tells many strange and unexpected for Johnny things, such as you have to be patient or that very important is solving many harder and harder problems.
The boy has found an online judge with tasks divided by topics they cover. He has picked pki problems from i-th category (p is his favorite number). He wants to solve them in two weeks (the patience condition is too hard for Johnny, so for simplicity, he looks only at easy tasks, which can be solved in such a period). Now our future grandmaster has to decide which topics to cover first and which the second week. Help him assign topics in such a way, that workload is balanced.
Formally, given n numbers pki, the boy wants to divide them into two disjoint sets, minimizing the absolute difference between sums of numbers in each set. Find the minimal absolute difference. Output the result modulo 109+7.
Johnny 刚刚发现了一篇全新的、非常棒的教程:“如何成为大师级选手?”。这篇教程向 Johnny 介绍了许多奇怪而意想不到的知识点,例如:你必须具备耐心,以及非常重要的一点是——持续求解难度不断递增的问题。
这位少年找到了一个在线评测系统,其中题目按其所涵盖的主题进行了分类。他从第 i 个主题中挑选了 pki 道题(p 是他最钟爱的数字)。他计划在两周内完成这些题目(由于“耐心”这一条件对 Johnny 来说过于困难,因此为简化问题,他只考虑那些能在该时间段内轻松解决的简单题目)。现在,这位未来的国际象棋大师需要决定:第一周学习哪些主题,第二周学习哪些主题。请帮助他将这些主题分配到两周中,使得两周的工作量尽可能均衡。
形式化地,给定 n 个数 pki,该少年希望将它们划分为两个互不相交的集合,使得这两个集合各自元素之和的绝对差值最小。请找出该最小绝对差值,并将结果对 109+7 取模后输出。
输入格式
Input consists of multiple test cases. The first line contains one integer t (1≤t≤105) — the number of test cases. Each test case is described as follows:
The first line contains two integers n and p (1≤n,p≤106). The second line contains n integers ki (0≤ki≤106).
The sum of n over all test cases doesn't exceed 106.
输入包含多个测试用例。第一行包含一个整数 t(1≤t≤105),表示测试用例的数量。每个测试用例的描述如下:
第一行包含两个整数 n 和 p(1≤n,p≤106)。第二行包含 n 个整数 ki(0≤ki≤106)。
所有测试用例中 n 的总和不超过 106。
输出格式
Output one integer — the reminder of division the answer by 1000000007.
输出一个整数——答案对 1000000007 取模的余数。
输入输出样例
输入#1
4 5 2 2 3 4 4 3 3 1 2 10 1000 4 5 0 1 1 100 1 8 89
输出#1
4 1 146981438 747093407
说明/提示
You have to minimize the difference, not it's remainder. For example, if the minimum difference is equal to 2, but there is also a distribution where the difference is 109+8, then the answer is 2, not 1.
In the first test case of the example, there're the following numbers: 4, 8, 16, 16, and 8. We can divide them into such two sets: 4,8,16 and 8,16. Then the difference between the sums of numbers in sets would be 4.
你需要最小化差值本身,而不是它的余数。例如,若最小差值为 2,但还存在一种分配方式使得差值为 109+8,则答案是 2,而非 1。
在示例的第一个测试用例中,给定的数字为:4、8、16、16 和 8。我们可以将它们划分为如下两个集合:{4,8,16} 和 {8,16}。此时,两集合元素之和的差值为 4。
输入解题思路,AI测评打分。不知道怎么写?