#P16852. [GKS 2021 #D] Primes and Queries

[GKS 2021 #D] Primes and Queries

Problem Description

You are given a prime number PP.

Let's define V(x)V(x) as the degree of PP in the prime factorization of xx. To be clearer, if V(x)=yV(x) = y then xx is divisible by PyP^y, but not divisible by Py+1P^{y+1}.

Also we define V(0)=0V(0) = 0.

For example, when P=3P = 3, and x=45x = 45, since 45=53245 = 5 \cdot 3^2, therefore V(45)=2V(45) = 2.

You are also given an array AA with NN elements. You need to process QQ queries of 22 types on this array:

  • type 11 query: 11 pospos valval - assign a value valval to the element at pospos, i.e. Apos:=valA_{pos} := val
  • type 22 query: 22 SS LL RR - print i=LRV(AiS(AimodP)S)\sum_{i=L}^{R} V(A_i^S - (A_i \bmod P)^S).

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains 33 space separated positive integers NN, QQ and PP - the number of elements in the array, the number of queries and a prime number.

The next line contains NN positive integers A1,A2,,ANA_1, A_2, \ldots, A_N representing elements of array AA.

Each of the next QQ lines describes a query, and contains either

  • 33 space separated positive integers: 11 pospos valval
  • or 44 space separated positive integers: 22 SS LL RR

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is a list of the answers for each query of type 22.

2
5 5 2
16 94 62 67 91
2 3 3 4
1 1 69
2 3 1 4
2 1 1 1
2 3 2 2
5 5 5
1 2 3 4 5
2 1 1 5
1 3 98
2 3 2 4
1 5 3
2 2 1 5
Case #1: 4 9 2 3
Case #2: 1 1 1

Hint

In Sample Case #11

The first query is a query of type 22, where S=3S = 3, L=3L = 3, R=4R = 4. Let's calculate the result for this query:

$$\begin{aligned} i &= 3,\ V(62^3 - (62 \bmod 2)^3) = 3 \\ i &= 4,\ V(67^3 - (67 \bmod 2)^3) = 1 \\ &\sum_{i=3}^{4} V(A_i^3 - (A_i \bmod P)^3) = 3 + 1 = 4 \end{aligned}$$

The second query is of type 11, where we need to assign 6969 to A1A_1, so our array AA now becomes: 69 94 62 67 9169\ 94\ 62\ 67\ 91.

Limits

1T1001 \le T \le 100

2P1092 \le P \le 10^9

PP is a prime number.

1posN1 \le pos \le N

1LRN1 \le L \le R \le N

For at most 1010 cases:

1N5×1051 \le N \le 5 \times 10^5

1Q1051 \le Q \le 10^5

For the remaining test cases:

1N1031 \le N \le 10^3

1Q1031 \le Q \le 10^3

There will always be at least one query of type 22.

Test Set 11

1S41 \le S \le 4

1Ai1031 \le A_i \le 10^3

1val1031 \le val \le 10^3

Test Set 22

1S1091 \le S \le 10^9

1Ai10181 \le A_i \le 10^{18}

1val10181 \le val \le 10^{18}