Product Game

2.5

2 votes
Prefix, Math, Algorithms, Multiplicative Inverse, Multiplicative inverse, Queries, Modulo arithmetic, Number Theory
Problem

Bob Plays an exciting game known as "Product Game" In the Game, Bob has been provided an array of numbers of size N along with Qqueries. For each query, he is provided with integers L and R and he has to find the product of all elements in the array between this range inclusively. Bob's task is to find the difference between the maximum and minimum solutions among all the queries.

Note: 

  • Since the value of product can be large, print the answer modulo 1000000007.
  • Compute the answer to each query as PQ1 modulo 1000000007, where Q1 denotes the multiplicative inverse of Q modulo 1000000007
  • Compute overall answer modulo 1000000007.

Task:

Find the difference between the maximum and minimum solutions among all the queries.

Function Description:

Complete the function solve provided in the editor. This function takes the following two parameters and returns the required answer:

T:  Test Cases

N:Size of Array

A: Array of Numbers

Q: Number of queries

queries: 2-D Array of queries containing [L,R] 

L:LowerIndex

R:UpperIndex

Input Format:

The first line contains an Integer  T which is the number of the test cases. 

Each Test Case will have the following :

The first line contain an integer N

the second line contains the array A

The third line contain an Integer Q

Next, Q lines contain two integers  L  and R in each lin

Output Format:

For each test case, print answer in a new line

Constraints

1T10

1N105

1A[i]105

1Q105

1[LR]N

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

 T=1

N=5

A=[2,4,5,6,8]

Q=3

queries=[[1,4],[2,5],[3,3]]

For 1 query L = 1 & R = 4

Product of 1 query  =2456=240

For 2 query L = 2 & R = 5

Product of  2 query  =4568=960

Similarly Product of the Third query =5

So the difference between the maximum and minimum query =9605=955

Editor Image

?