CF1361B.Johnny and Grandmaster

普及/提高-

通过率:0%

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 pkip^{k_i} problems from ii -th category ( pp 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 nn numbers pkip^{k_i} , 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+710^{9}+7 .

输入格式

Input consists of multiple test cases. The first line contains one integer tt (1t105)(1 \leq t \leq 10^5) — the number of test cases. Each test case is described as follows:

The first line contains two integers nn and pp (1n,p106)(1 \leq n, p \leq 10^6) . The second line contains nn integers kik_i (0ki106)(0 \leq k_i \leq 10^6) .

The sum of nn over all test cases doesn't exceed 10610^6 .

输出格式

Output one integer — the reminder of division the answer by 10000000071\,000\,000\,007 .

输入输出样例

  • 输入#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 22 , but there is also a distribution where the difference is 109+810^9 + 8 , then the answer is 22 , not 11 .

In the first test case of the example, there're the following numbers: 44 , 88 , 1616 , 1616 , and 88 . We can divide them into such two sets: 4,8,16{4, 8, 16} and 8,16{8, 16} . Then the difference between the sums of numbers in sets would be 44 .

首页