Geeky Friendship Gift

0

0 votes
Mos, Hard, Persistant Segment Trees
Problem

On friendship day , Thundercracker gifted Tihor an array A of N positive integers instead of chocolates. In order to get the chocolates, Tihor has to solve the following problem given by Thundercracker efficiently:

Thundercracker asks Tihor Q queries. In each query, you are given two integers L and R. You have to compute and print the value of below expression:

(PPd|(A[L]..A[R])ϕ(P))%(109+7)

  • P must be prime.
  • (A[L]..A[R]) denotes a subarray of A consisting of elements A[L], A[L+1], ..., A[R].
  • Pd (d>0) divides subarray (A[L]..A[R]) if it divides atleast one element in that subarray.
  • ϕ() is the Euler's totient function.

Tihor is worried and find this problem very difficult. Will you help him?

Input format

The first line contains N and Q, number of elements in an array A and number of queries respectively.
Second line contains N space separated integers, ith of them denotes A[i].
Third line contains four space separated integers α, β, γ, δ.

Query generation

You need to generate Q queries in the following way.
Let Ansi denote the answer for ith query and Ans0=0. Then for ith(i1) query,

Li = 1+(αAnsi1+iβ)%N.
Ri = Li+(γAnsi1+iδ)%(NLi+1).

Output format

For each query, print the required answer in a singe line.

Constraints

1N105
1Q106
1A[i]105
0α,β,γ,δ109

Subtask Points
α=γ=0 20%
1N,Q,A[i]103 20%
Original Constraints 60%
Time Limit: 4
Memory Limit: 256
Source Limit:
Explanation

The queries are as follows:

For first query, [2,2]- the only prime power which divides subarray [2,2] is 191. So the answer is ϕ(19)=18.

For second query, [1,3]- the prime powers which divides the subarray are 21, 31, 32, 51 and 191. So the answer is 288.

Editor Image

?