Given an integer N and an integer FN which denotes the Nth term of the linear equation F(N) = (2 * F(N – 1)) % M, where M is 109 + 7, the task is to find the value of F(1).
Input : N = 2, FN = 6
If F(1) = 3, F(2) = (2 * F(1)) % M = (2 * 3) % M = 6.
For F(1) = 3 the given linear equation satisfies the value of F(2).
Therefore, the value of F(1) is 3.
Input : N = 3, FN = 6
If F(1) = 500000005
F(2) = (2 * 500000005) % M = 3
F(3) = (2 * 3) % M = 6
For F(1) = 500000005 the given linear equation satisfies the value of F(3).
Therefore, the value of F(1) is 500000005
Naive Approach: The simplest approach to solve this problem is to try all possible values of F(1) in the range [1, M – 1] and check if any value satisfies the given linear equation or not. If found to be true, then print the value of F(1).
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is based on the following observations:
Given the linear equation, F(N) = 2 * F(N – 1) ——(1)
put the value of F(N – 1) = 2 * F(N – 2) in equation(1)
=>F(N) = 2 * (2 * F(N – 2)) ——–(2)
put the value of F(N – 2) = 2 * F(N – 3) in equation(2)
=>F(N) = 2* (2 * (2 * F(N – 3)))
=>F(N) = 2(N – 1) F(N – (N – 1)) = 2(N – 1) F(1)
=>F(1) = F(N) / 2(N – 1)
Follow the steps below to solve the problem:
- Calculate the modulo multiplicative inverse of 2(N – 1) under modulo M, say modInv.
- Finally, print the value of F(N) * modInv.
Below is the implementation of the above approach:
Time Complexity: O(log2N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Program to print the sum of the given nth term
- Find nth term of a given recurrence relation
- Find Pth term of a GP if Mth and Nth terms are given
- Find the nth term of the given series
- Nth term of a recurrence relation generated by two given arrays
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Finding nth term of any Polynomial Sequence
- Find nth term of the Dragon Curve Sequence
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to print tetrahedral numbers upto Nth term
- Program to print pentatope numbers upto Nth term
- Program to find Nth term of series 9, 23, 45, 75, 113...
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.