Fibonacci with GCD
Practice
2 (1 votes)
Grammar Verified
Open
Recruit
Approved
Grammar Verified
Open
Recruit
Approved
Grammar Verified
Segment trees
Hard
Advanced data structures
Data structures
Mathematics
Segment tree
Problem
52% Success 173 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code
You are given \(N\) integers \(A_1,A_2, ... ,A_N\) and \(Q\) queries. In each query, you are given two integers \(L\) and \(R\) (\(1 ≤ L ≤ R ≤ N\)).
For each query, write a program to find the value of the following expression:
\( GCD( F(A_L), F(A_{L+1}), F(A_{L+2}) ...... F(A_R) ) \% 10^9+7\)
Input format
- First line: Two space-separated integers \(N\) and \(Q\)
- Second line: \(N\) space-separated integers denoting the elements of array \(A\)
- Next \(Q\) lines: Two space-separated integers \(L\) and \(R\)
Output format
For each query, print the value of the expression.
Note
-
The Fibonacci series is defined as:
\( F(N) \) = \( F (N-1) + F(N-2) \) \(\forall \space N \ge 3 \)
\( F(1) =1\) , \(F(2) = 1 \) -
The GCD of two numbers is the greatest common divisor of those numbers. For example, \(GCD(2, 4)\ =\ 2\).
Constraints
\(1 \le N \le 10^5\)
\(1 \le Q \le 10^5\)
\(1 \le A_i \le 10^9\)
\(1 \le L \le R \le N\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
AlgorithmsDynamic ProgrammingMedium
Points:30
562 votes
Tags:
Binary search algorithmAd-HocEasy-MediumReadyDepth-first searchApproved
Points:30
7 votes
Tags:
Easy
Editorial