#P10791. 『SpOI - R1』强大到让你们所有人注视

『SpOI - R1』强大到让你们所有人注视

Problem Description

This problem contains multiple test cases.

You are given an nn-digit base-kk big number.

Let S(l,r)S(l,r) denote the new base-kk number formed by taking the digits from the ll-th to the rr-th digit (from most significant to least significant) of this base-kk number.

You need to compute 1lrnS(l,r)\sum\limits_{1\leq l\leq r\leq n} S(l,r). Note that this summation is also done in base kk.

Since the answer may be very large, let (20070720)10(20070720)_{10} be xx when written in base kk. You only need to output the result of the answer modulo xx.

Reminder again: all summations, operations, and modulo in this problem are done in base kk.

Input Format

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains two positive integers n,kn,k, describing this big number.

The second line of each test case contains a sequence aa, which gives each digit of this base-kk big number from most significant to least significant. It is guaranteed that i[1,n],0ai<k\forall i\in[1,n],0\leq a_i<k.

Output Format

For each test case, output one line: the answer modulo xx. Since it is also a base-kk number, output each digit in order separated by spaces.

Note that your output should not contain leading zeros.

1
3 2
1 1 0
1 1 0 1
1
2 20070721
20070720 1
2

Hint

Explanation for Sample #1

All S(l,r)S(l,r) are: (1)2,(1)2,(0)2,(11)2,(10)2,(110)2(1)_2,(1)_2,(0)_2,(11)_2,(10)_2,(110)_2. Adding them in base 22 gives (1101)2(1101)_2, and then taking modulo (20070720)10=(1001100100100000101000000)2(20070720)_{10}=(1001100100100000101000000)_2 in base 22 yields the answer (1101)2(1101)_2.

Explanation for Sample #2

For this number, S(1,1)S(1,1) is clearly divisible by (20070720)20070721(\overline{20070720})_{20070721}. After dividing S(2,2)S(2,2) and S(1,2)S(1,2) by (20070720)20070721(\overline{20070720})_{20070721}, both remainders are 11. Therefore, the answer after modulo is (2)20070721(2)_{20070721}.

Constraints

This problem uses bundled subtasks and subtask dependencies.

For 100%100\% of the testdata, 1T101\leq T\leq 10, 1n5×1051\leq n\leq 5\times 10^5, 0ai<k1090\leq a_i<k\leq 10^9, 2k1092\leq k\leq 10^9. The base-kk big number may contain leading zeros.

Subtask TT\leq nn\leq Special Properties Score Dependencies
1 1010 100100 None 2525 None
2 11 5×1035\times 10^3 k>20070720k>20070720 2020
3 8×1038\times 10^3 None 2525 1,2
4 55 5×1055\times 10^5 3030 1,2,3

Translated by ChatGPT 5