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).
Examples:
Input : N = 2, FN = 6
Output:3
Explanation:
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
Output: 500000005
Explanation:
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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long power( long long x,
long long N)
{
long long res = 1;
while (N > 0) {
if (N & 1) {
res = (res * x) % M;
}
x = (x * x) % M;
N = N >> 1;
}
return res;
}
long long moduloInverse( long long X)
{
return power(X, M - 2);
}
long long F_1( long long N,
long long F_N)
{
long long P_2 = power(2, N - 1);
long long modInv = moduloInverse(P_2);
long long res;
res = ((modInv % M) * (F_N % M)) % M;
return res;
}
int main()
{
long long N = 3;
long long F_N = 6;
cout << F_1(N, F_N);
}
|
Java
import java.util.*;
class GFG{
static final int M = 1000000007 ;
static long power( long x,
long N)
{
long res = 1 ;
while (N > 0 )
{
if (N % 2 == 1 )
{
res = (res * x) % M;
}
x = (x * x) % M;
N = N >> 1 ;
}
return res;
}
static long moduloInverse( long X)
{
return power(X, M - 2 );
}
static long F_1( long N,
long F_N)
{
long P_2 = power( 2 , N - 1 );
long modInv = moduloInverse(P_2);
long res;
res = ((modInv % M) *
(F_N % M)) % M;
return res;
}
public static void main(String[] args)
{
long N = 3 ;
long F_N = 6 ;
System.out.print(F_1(N, F_N));
}
}
|
Python3
M = 1000000007
def power(x, N):
res = 1
while (N > 0 ):
if (N & 1 ):
res = (res * x) % M
x = (x * x) % M
N = N >> 1
return res
def moduloInverse(X):
return power(X, M - 2 )
def F_1(N, F_N):
P_2 = power( 2 , N - 1 )
modInv = moduloInverse(P_2)
res = 0
res = ((modInv % M) * (F_N % M)) % M
return res
if __name__ = = '__main__' :
N = 3
F_N = 6
print (F_1(N, F_N))
|
C#
using System;
class GFG{
static readonly int M = 1000000007;
static long power( long x,
long N)
{
long res = 1;
while (N > 0)
{
if (N % 2 == 1)
{
res = (res * x) % M;
}
x = (x * x) % M;
N = N >> 1;
}
return res;
}
static long moduloInverse( long X)
{
return power(X, M - 2);
}
static long F_1( long N,
long F_N)
{
long P_2 = power(2, N - 1);
long modInv = moduloInverse(P_2);
long res;
res = ((modInv % M) *
(F_N % M)) % M;
return res;
}
public static void Main(String[] args)
{
long N = 3;
long F_N = 6;
Console.Write(F_1(N, F_N));
}
}
|
Javascript
<script>
var M = 100000007;
function power(x, N)
{
var res = 1;
while (N > 0)
{
if (N % 2 == 1)
{
res = (res * x) % M;
}
x = (x * x) % M;
N = N >> 1;
}
return res;
}
function moduloInverse( X)
{
return power(X, M - 2);
}
function F_1( N, F_N)
{
var P_2 = power(2, N - 1);
var modInv = moduloInverse(P_2);
var res;
res = ((modInv % M) *
(F_N % M)) % M;
return res;
}
var N = 3;
var F_N = 6;
document.write(F_1(N, F_N));
</script>
|
Time Complexity: O(log2N) since in the power function in every call the value of n is divided by 2 until it reaches 1 thus the algorithm takes logarithmic time to execute.
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant