307475: CF1361B. Johnny and Grandmaster
Memory Limit:256 MB
Time Limit:2 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Johnny and Grandmaster
题意翻译
给定 $n,p$ 和 $n$ 个整数形如 $p^{k_i}$,要求将这 $n$ 个数分为两个集合,最小化两个集合的各自的和的差的绝对值,答案对 $10^9+7$ 取模。 $\sum n\le 10^6,1\le p\le 10^6,0\le k_i\le 10^6$。 多组数据。题目描述
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 $ p^{k_i} $ 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 $ p^{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 $ 10^{9}+7 $ .输入输出格式
输入格式
Input consists of multiple test cases. The first line contains one integer $ t $ $ (1 \leq t \leq 10^5) $ — the number of test cases. Each test case is described as follows: The first line contains two integers $ n $ and $ p $ $ (1 \leq n, p \leq 10^6) $ . The second line contains $ n $ integers $ k_i $ $ (0 \leq k_i \leq 10^6) $ . The sum of $ n $ over all test cases doesn't exceed $ 10^6 $ .
输出格式
Output one integer — the reminder of division the answer by $ 1\,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