Fibonacci with GCD

4.1

14 votes
Approved, Data Structures, Math, Medium, Recruit, Segment Trees
Problem

Monk is really fond of Recurrence Relations, and he likes to study their characteristics in any possible manner. As you might know, his favorite one among all such recurrences is the famous Fibonacci series. For those of you who haven't,

Fibonacci series is defined as:

F(N) = F(N1)+F(N2)  N3

F(1)=1 , F(2)=1.

Now, in addition to such sequences, Number Theory is a really interesting concept, and Monk loves solving problems of those kinds too. GCD is a nice concept within the scope of this topic, and is defined to be :

The GCD of two numbers is the greatest common divisor of those numbers. Eg: GCD(2,4)=2. Here, he has challenged you to the following task as he feels that this one is amazingly challenging . So, this is how it goes :

You are given N integers, A1,A2,...,AN and Q queries. In each query, you are given two integers L and R(1LRN). For each query, you have to find the value of:

GCD(F(AL),F(AL+1),F(AL+2)......F(AR))%109+7

Can you do it ?

Constraints:
1N105
1Q105
1Ai109
1LRN

Format of the input file:
First line : Two integers N and Q.
Second line : N space separated integers denoting array A.
Next Q lines : Two space separated integers L and R.

Format of the output file:
Output the result of each query in a separate line.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For query 1: GCD(F(2) , F(4), F(8))= GCD(1,3,21)=1 For query 2: GCD(F(4), F(8))= GCD(3,21)=3

Editor Image

?