# Yet Another LIS Problem Codechef Solution | APRIL CHALLENGE Share:

Yet Another LIS Problem Codechef Solution

Nayra doesn’t like stories of people receiving random arrays as birthday presents, but this time she received random arrays as presents for her own birthday! After struggling for a day trying to figure out what to do with the array, she asked Aryan for help. He gave her the following problem.

You are given 33 integers MM, PP, and NN.

For an array AA, let f(A)f(A) be defined as the length of the longest strictly increasing subsequence of AA and let g(A)=(f(A))Pg(A)=(f(A))P.

Consider all distinct arrays AA of length NN such that 1≤Ai≤M1≤Ai≤M for all (1≤i≤N)(1≤i≤N). Calculate the sum of g(A)g(A) over all such MNMN arrays. Since the answer can be huge, print it modulo 754974721754974721.

Each test case has QQ queries. For all QQ queries, MM and PP remain the same, only NN varies.

### Input Format

• First line will contain TT, the number of test cases. Then the test cases follow.
• First line of each test case contains three integers M,P,M,P, and QQ.
• Second line of each test case contains QQ integers, values of NN for each query.

### Output Format

For each testcase, output in a single line containing QQ space-separated integers, answers for each query modulo 754974721754974721.

### Constraints

• 1≤T≤41≤T≤4
• 1≤M≤141≤M≤14
• 1≤P≤1061≤P≤106
• 1≤N≤1091≤N≤109
• 1≤Q≤501≤Q≤50

### Sample Input 1

``````4
1 1 2
1 2
2 1 2
1 2
2 2 2
1 2
2 1000000 2
1 2
``````

### Sample Output 1

``````1 1
2 5
2 7
2 392816544
``````

### Explanation

Test case 11: In the first query there are 11=111=1 distinct arrays. The array is : .

• f()=f()= LIS of =1=1. Also, g()=(f())1=11=1g()=(f())1=11=1.

Sum of g(A)g(A) is 11.

In the second query there are 12=112=1 distinct arrays. The array is : [1,1][1,1].

• f([1,1])=f([1,1])= LIS of [1,1]=1[1,1]=1. Also, g([1,1])=(f([1,1]))1=11=1g([1,1])=(f([1,1]))1=11=1.

Sum of g(A)g(A) is 11.

Test case 22: In the first query there are 21=221=2 distinct arrays. The arrays are :  and .

• f()=f()= LIS of =1=1. Also, g()=(f())1=11=1g()=(f())1=11=1.
• f()=f()= LIS of =1=1. Also, g()=(f())1=11=1g()=(f())1=11=1. Sum of g(A)g(A) is 1+1=21+1=2.

In the second query there are 22=422=4 distinct arrays. The arrays are : [1,1],[1,2],[2,1][1,1],[1,2],[2,1] and [2,2][2,2].

• f([1,1])=f([1,1])= LIS of [1,1]=1[1,1]=1. Also, g([1,1])=(f([1,1]))1=11=1g([1,1])=(f([1,1]))1=11=1.
• f([1,2])=f([1,2])= LIS of [1,2]=2[1,2]=2. Also, g([1,2])=(f([1,2]))1=21=2g([1,2])=(f([1,2]))1=21=2.
• f([2,1])=f([2,1])= LIS of [2,1]=1[2,1]=1. Also, g([2,1])=(f([2,1]))1=11=1g([2,1])=(f([2,1]))1=11=1.
• f([2,2])=f([2,2])= LIS of [2,2]=1[2,2]=1. Also, g([2,2])=(f([2,2]))1=11=1g([2,2])=(f([2,2]))1=11=1.

Sum of g(A)g(A) is 1+2+1+1=51+2+1+1=5.